954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pointer Issues

Hello. I am writing a program with some dynamic arrays of pointers to objects (not arrays of objects). Since there will be A LOT of tacking on new objects to the arrays, for the purposes of efficiency, I am implementing an algorithm to fill up a buffer array first and only tack it on the end of the real array when the buffer reaches the same size as the real array.

The problem: the variables 'liv_s' and 'liv_tmp_s' (see code below) are not updated as they should be, causing to whole thing to mess up. I think 'liv_tmp_s' is the culprit, but I can't be certain. When I run the MSVC10 debugger, it seems to claim that the addresses of liv_s and liv_tmp_s are the same, and yet they have different values!?!?!

I still have some simplification to do, but here's the algorithm. ('Cell' is the class of objects I'm using. 'dest' is the array, 'buf' is the buffer array, 'dest_s': # of elem in dest, 'buf_s': # of elem in buf, cell: pointer to add, 'ret_dest': pointer to the array pointer, 'ret_buf': pointer to the buffer array pointer.)
void add_to_array(Cell **dest, Cell **buf, int *dest_s, int *buf_s, Cell *cell, Cell ***ret_dest, Cell ***ret_buf) {
if (*buf_s < *dest_s) { //if buffer is smaller than array
//add cell to buf
Cell **tmp = new Cell*[*buf_s + 1]; //new buffer array, 1 elem larger
int i;
for (i = 0; i < *buf_s; i++) {
tmp[i] = buf[i]; //copy old elems over
}
tmp[i] = cell; //add the new one
delete [] buf; //delete old buffer
*ret_buf = tmp; //replace the old pointer to the buffer with the new one
*buf_s++; //new size of the buffer <-- issue here?
} else {
//add buf and cell to dest
Cell **tmp = new Cell*[*dest_s + *buf_s + 1]; //new larger array
int i;
for(i = 0; i < *dest_s; i++) {
tmp[i] = dest[i]; //copy old data over
}
for (i; i < *buf_s + *dest_s; i++) {
tmp[i] = buf[i]; //add buffer's data
}
tmp[i] = cell; //add the new elem
delete [] dest; //delete old array
delete [] buf; //delete old buffer
*ret_dest = tmp; //replace old array pointer with new one
*dest_s += (*buf_s + 1); //new array size <-- issue here?
*ret_buf = new Cell*[0]; //empty buffer
*buf_s = 0; //new buffer size <-- issue here?
}
}

my abbreviated program: Cell *newCell;
Cell **living, **living_tmp;
int liv_s, liv_tmp_s;

int main() {
liv_s = 0;
liv_tmp_s = 0;
living = new Cell*[0];
living_tmp = new Cell*[0];
//for various integer values of x, i, and y:
newCell = new Cell(x+i, y);
add_to_array(living, living_tmp, &liv_s, &liv_tmp_s, newCell, &living, &living_tmp);
//
}

So, pointers to liv_s and liv_tmp_s get passed into the function as *dest_s and *buf_s, but liv_s and liv_tmp_s don't seem to be updated properly when the function changes *dest_s or *buf_s.

Suggestions? Thanks.

fougere
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

here's a better add_to_array function that's a little simpler, but still has the same issue:

void add_to_array(Cell *cell, Cell ***dest, Cell ***buf, int *dest_s, int *buf_s) {
	if (*buf_s < *dest_s) {
		Cell **tmp = new Cell*[*buf_s + 1];
		for (int i = 0; i < *buf_s; i++) {
			tmp[i] = *buf[i];
		}
		tmp[*buf_s] = cell;
		delete [] *buf;
		*buf = tmp;
		*buf_s++;
	} else {
		Cell **tmp = new Cell*[*dest_s + *buf_s + 1];
		for (int i = 0; i < *dest_s; i++) {
			tmp[i] = *dest[i];
		}
		for (int i = *dest_s; i < *dest_s + *buf_s; i++) {
			tmp[i] = *buf[i - *dest_s];
		}
		tmp[*dest_s + *buf_s] = cell;
		delete [] *buf;
		delete [] *dest;
		*dest = tmp;
		*buf = new Cell*[0];
		*dest_s += *buf_s + 1;
		*buf_s = 0;
	}
}
fougere
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

have you though about using the STL instead of arrays of pointers. I would suggest a list if you are going to be adding or deleting elements anywhere in the list or a vector if you are only going to be adding or removing elements from the end. from the look of your code i think a list would work well for you.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 
void add_to_array(Cell *cell, Cell ***dest, Cell ***buf, int *dest_s, int *buf_s) {
	if (*buf_s < *dest_s) {
		
	}
}

Are you sure the control is going inside this if condition? Can you put some couts and confirm that?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

@NathanOliver: thanks for the tip, I'll look in to it.

@Agni: I'm in a sort of dilemma because it's a win32 app not a console app, so i've been just going off what the debugger says is happening. It says that control does go inside the if condition.

fougere
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

My bad.. i didn't see that there was an else too. Well I don't see why the values will not be updated properly. I tried your code, removed the references to Cell class, just a simple code, add_to_array was changed to

void add_to_array(int *dest_s, int *buf_s)


and the values were updated properly. May be you can try doing the same and debug and then add the details.I just hope your application is not crashing with all those pointers in there. As mentioned above, using STL might be a good idea.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

ah, i found it! apparently *ptr++ is not the same as (*ptr)++, and *ptr[i] is not the same as (*ptr)[i]. It works fine now, but yeah, i think i'll use STL in the future.
Thanks!

fougere
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: