resizing array, deleting old array terminates program

I am looping this function to extract transaction from the list. it works fine for the first loop, but gets terminated on the second loop for deleting the list. please, help me to figure this out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Transaction TransactionList::extract(){
	if(size > 0){
		--size;
	}
	Transaction tempT = list[0];
	temp = new Transaction [300];
	for (int i = 1; i <=size+1; i++){
		temp[i-1] = list[i];
	}
	delete [] list;
	list = NULL;
	list = temp;
	delete [] temp;
	temp = NULL;
	return tempT;
}


also, tempT doesn't get assigned as correct transaction on the second loop.
Last edited on
The first time this function is called:

1
2
	list = temp;
	delete [] temp;


list contains an invalid address after the first time this function is called.
can you explain a little more about how to solve this? I thought list would always point at the same address as it's a pointer.
1
2
	list = temp;     // list contains the same address temp does
	delete [] temp;  // free the memory pointed to by both temp and list 
thank you, just realized I am not supposed to delete temp as list is now temp.
Topic archived. No new replies allowed.