moving pointers to pointers
I have a pointer that points to an array of pointers(char **pp). The array of pointers that "pp" points to, points to sections of a string. I want to move the address's that the pointers point to in a string to make room for a new pointer.
Here is my formula using memcpy to copy data.
memcpy(pp + (sizeof(char *) * (Index+1)), pp+(sizeof(char *) * Index), (pplen - Index) * sizeof(char *));
"Index" is an int of where to add the new pointer in memory. This is sopposed to copy data from one spot to another which will make room for a new element in the correct spot for fast searching. The problem is it doesn't work properly. Everything is ok if The corect spot to put the new pointer is at the end because that does not require memcpy. But if it requires to use memcpy or memmove to move data to make room for a new pointer it all messes up.
I get no errors from that line. But because something is wrong in my formula I get errors when searching. How can I move a memory block of pointers to a new location?
Thanx in advance!
Re: moving pointers to pointers
Quote:
Originally posted by Benzakhar
I have a pointer that points to an array of pointers(char **pp). The array of pointers that "pp" points to, points to sections of a string. I want to move the address's that the pointers point to in a string to make room for a new pointer.
That's horribly worded. Let's see if we can sort it out:
Code:
char **pp; /* your array of ponters */
char ***ppp; /* your pointer to the above */
ppp = &pp; /* points to your array now */
Now you want ppp to point to a new array which has more room?
Code:
char **pp2;
pp2 = malloc( some_new_size );
for( x = 0; x < old_array_size; x++ )
pp2[x] = pp[x];
ppp = &pp2;
free( pp ); /* don't free what it points to, just it */
Something like that? Of course, you're on the C++ board, so you probably should use new and delete.
Quzah.