-
realloc problems
I use realloc to find a new memory location assign it to a pointer and copy its old data to the new location. I get NULL. I tryed to use MessageBox and itoa functions to find out the new size in bytes witch is 24bytes. I don't know why it returns NULL. I used malloc previously to allocate a poniters starting memory. Here is the line that returns NULL;
Lines.lpLines =(char **)realloc(Lines.lpLines, sizeof(char **) * Lines.max );
Anyone know what the problem could be?
Thanx in advance!
-
Any prior use of malloc/realloc/free could have messed things up
Any prior use of the memory returned by malloc/realloc could have messed things up here
Look for
- use before alloc
- use after free
- generally getting the size wrong - int *ptr = malloc(10) does NOT allocate space for 10 integers.
Also, your code has the realloc assignment bug
Code:
char **temp = (char **)realloc(Lines.lpLines, sizeof(char **) * Lines.max );
if ( temp != NULL ) {
Lines.lpLines = temp;
} else {
// cannot realloc
// but at least Lines.lpLines points to what we have so far
}
Use a temp pointer to preserve the old pointer, until you know the new one is allocated.
You really should be using new and delete in C++.
-
Thanx. I got it to work properly using new and delete.