help, program crash when it's returning an object by extract funciton

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
while(!secList.empty()){
		Security se = Security();
		se = secList.extract();  // CRASH
		cout << "Security info: " << se.security << endl;
		cout << "Transaction List: " << endl;
		while(!se.tranList.empty()){
			Transaction tr = Transaction();
			tr = se.tranList.extract();
			cout << "Transaction Date: " << tr.transactionDate;
			cout << ", Action: " << tr.action;
			cout << ", Share Price: " << tr.price;
			cout << ", Share numbers: " << tr.share << endl;
		}
	}


Security SecurityList::extract() {
	if(ssize > 0){
		stemp = new Security [100];
		temp = Security();
		temp = slist[0];
		for (int i = 1; i <=ssize; i++){
			stemp[i-1] = slist[i];
		}
		--ssize;
		delete [] slist;
		slist = stemp;
		return temp;
	}
}

SecurityList::~SecurityList(){
	delete [] slist;
	delete [] stemp;
	slist = NULL;
	stemp = NULL;
}


my program crash when it try to assign the return value of the function to the local value. extract function does return correct value, but it just crash when done executing. any help will be appreciated.
Last edited on
There are a few problems with what you've posted, but I expect we can't get to the root of the problem without seeing the class declaration and assignment and constructors.

If slist == 0, the return from extract() is undefined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//-----------------------------------------------------------------------------
// SecurityList constructor
// this constructor will create SecurityList class with size 0.
SecurityList::SecurityList(){
	ssize = 0;
	slist = new Security[100] ();
}

private:
		Security *slist;
		Security *stemp;
		int ssize;

//-----------------------------------------------------------------------------
// SecurityList insert
// insert the transation to the list
void SecurityList::insert(const Security sec){
	if (ssize < 100){
		slist[ssize] = sec;
		ssize++;
	}
}


I added constructor, insert function and class declaration
Last edited on
what do you mean slist == 0, do you mean ssize == 0?
what do you mean slist == 0, do you mean ssize == 0?
Yes.

Surely, the compiler will have warned you about such things. Are you ignoring compiler warnings?

What does your copy constructor and assignment operator look like?

What does this code mean?
 
Security se = Security();

Shouldn't it be this?
 
Security se;


insert should probably be:
 
void SecurityList::insert(const Security& sec) // pass sec by const ref 
Last edited on
Topic archived. No new replies allowed.