Unexpected Modification of Variable

Hello!

I have a weird problem and I just can't find the reason.
I am rather new to c++ so I was hoping, that one of you might find the error.

The Problem:
I have written a class that does a bubblesort on a vector<double>.
The algorithm works just fine. But somehow, a couple of "cout"s alter the output of the vector length. Example ...

1
2
3
4
5
6
7
8
9
10
11
12
BubbleSort b1(meinvec);
BubbleSort b2(meinvec);

b1.sort1();
b1.output();

b2.sort1();
b2.output();

cout << "here it comes ..." << endl;
cout << b1.vec->size() << endl;
cout << b2.vec->size() << endl;


Output:


Starting Sort1()
Sort1 got 10 items.
[Out]
16
26
26
38
41
50
59
65
68
74
Starting Sort1()
Sort1 got 10 items.
[Out]
16
26
26
38
41
50
59
65
68
74
here it comes ...
200868154
200868154


The "200868154" should be the size of the vector the sorting class is using (=10).
When I get rid of the "here it comes" cout-line. The output is:


...blablabla...
10
200868154


And when I comment-out the "cout << b1.vec->size() << endl;" the number (10) for the second class-instance comes out correct

10


To be honest - I have never had an error like that.
I would very much appreciate and help/tips.

Thanks

Patrick
This sounds like memory corruption. Can we see the source of the class that does the sorting? The fact that you're using pointers to the vectors scares me. (Not to mention that you have public member variables! eek!)
Hello!

Thanks for the Reply and sorry for not answering more quickly.

BubbleSort.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * BubbleSort.cpp
 *
 *  Created on: Jan 19, 2012
 *      Author: ph
 */

#include <iostream>
#include <vector>
#include <string>

#include "BubbleSort.h"

using namespace std;

BubbleSort::BubbleSort(vector<double> & avec) {
	vector<double> tvec(avec);
	vec = &tvec;
}

void BubbleSort::sort1() {
	cout << "Starting Sort1()" << endl;
	cout << "Sort1 got " << vec->size() << " items." << endl;
	unsigned long i;
	double t;
	for (unsigned long last = vec->size()-1; last>1; last--) {
		for (i=0; i<last; i++) {
			if ((*vec)[i] > (*vec)[i+1]) {
				t = (*vec)[i];
				(*vec)[i] = (*vec)[i+1];
				(*vec)[i+1] = t;
			}
		}
	}
}

void BubbleSort::sort2() {
	cout << "Starting Sort2()" << endl;
	unsigned long last = vec->size()-1;
	cout << "Sort2 got " << vec->size() << " items." << endl;
	unsigned long i=0;
	double t;
	if (last==0) {
		cout << "Only one Item!" << endl;
		return ;
	}
	while (true) {
		if (i<last) {
			if ((*vec)[i] > (*vec)[i+1]) {
				t = (*vec)[i];
				(*vec)[i] = (*vec)[i+1];
				(*vec)[i+1] = t;
			}
			i++;
		} else
		if (i==last && last!=1){
			last--;
			i=0;
		} else
		if (i==last && last==1) {
			break;
		}
	}
}

void BubbleSort::output() {
	for (unsigned long i=0; i<vec->size(); i++) {
		cout << vec->at(i) << endl;
	}

}


I only made the vector public, so I could debug my code more easily.

Oh and here is the class being called:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main(void) {
	vector<double> meinvec;
	srand ( time(NULL) );
	unsigned long x=0;
	double d;
	cout << "[Filling Vector]" << endl;
	while (x<10) {
		d = rand()%100;
		meinvec.push_back(d);
		//cout << d << endl;
		x++;
	}

	BubbleSort b1(meinvec);
	BubbleSort b2(meinvec);

	b1.sort1();
	b1.output();

	b2.sort1();
	b2.output();

	//cout << "here it comes ..." << endl;
	cout << b1.vec->size() << endl;
	cout << b2.vec->size() << endl;

	cout << "SuchenUndFinden ENDE" << endl;
}
LB was right to be scared.

1
2
3
4
BubbleSort::BubbleSort(vector<double> & avec) {
        vector<double> tvec(avec);
	vec = &tvec;
}

This constructs a vector of doubles called tvec, takes its address and stores it in the pointer vec, and then destructs tvec. The pointer vec is a dangling pointer, any further access to it is undefined.

There is no reason at all to use pointers in this program, store the vector as-is, not as a pointer.

(or, for the smallest fix, change vec = &tvec; to vec = &avec; )
Last edited on
Hey Cubbi, Hey LB,

thanks for your help. The Tip with the pointer was the solution.
I am rather new to C++ and I am used to Java being my mother and taking care of things if you know what I mean ;-)

Thanks a bunch!

P
Last edited on
Topic archived. No new replies allowed.