You MUST free it before you change the pointer, otherwise you have a memory leak.
which is what I've been doing with absolutely no problems whatsoever?
You probably won't run into any issues unless you start leaking very large amount of memory (like >MBs), but if you have lots of your program running or it runs for a long time, it can (and will) add up.
Thanks very much. I only allow one instance of my program, so that's one reason I have had no problems, plus the memory amount is not huge, but nevertheless, I want to do it right, and of course avoid this issue in future programs, so once again, thanks for letting me know.
Since malloc(), calloc(), realloc(), and free() are C functions and I haven't used them for a long time, I can't be sure but I believe that realloc() frees the original buffer if its new size cannot be accommodated in its current position, so I think it is safe to assign the new pointer to bWriteBuffer without freeing the old pointer value.
Visual Studio allows peeking the RAM (probably other IDE's too). Peek the RAM pointed to by bWriteBuffer before the call to realloc() in debug mode. Usually, the debug CRT will change the value of the memory to signal its deletion. If you see this happening, then you are OK without freeing the memory yourself.
Ah, I misunderstood, and I see what you are doing now. Since you have another pointer to the data, you DON'T want to free it, otherwise the other pointer you had will now be pointing at what you just freed (which was probably why you got a segfault).
Okay, that makes sense. That's what I thought was happening after I ran it through the debugger.
Except I don't understand why both variables don't now contain the data. IOW, I have declared...
1 2
BYTE* bWriteBuffer;
void* bScratchVar;
So as you can see from my first example in my first post above, I have reallocated the data from bWriteBuffer into bScratchVar. I then assign bWriteBugger back to bScratchVar with the old plus the new data, so why don't both variaboles contain the data?
Or is the data on the heap and in one spot and the variables are just pointing to it?
The correct way of doing it (if I remember correctly) is this:
1 2
void *bScratchVar = realloc(bWriteBuffer, total + 1);
if (bScratchVar) bWriteBuffer = (BYTE*)bScratchVar;
This should account for the cases where NULL is returned because you are out of memory.
So, if you get a non-null pointer, then just use it and be happy that you won't leak memory through the old pointer as realloc() has taken care of it (so long it was allocated with malloc()).
I first read the data into bWriteBuffer, then I reallocate memory into bScratchVar, as shown in my first post.
So the question is, where is the data IMMEDIATELY after I reassign the old and new data in bScratchVar back to bWriteBuffer?
IOW, do BOTH variables contain data before I set bScratchVar to NULL, or is there just one set of data on the heap and both variables are pointing to that one set of data?
Realloc() will automatically move the data to the potentially new location, and a pointer to the new location is returned and save into bScratchVar. This means that there is no guarantee that, after realloc(), the bWriteBuffer is a valid buffer. It may be, but it may have been freed by realloc(). Remember that allocations have to fit in a single block of memory. If the current block has no room to accommodate the new amount, then a new block is used and the data contained in the old block is copied to the new location automatically.
Important choice of words there. I must say: Both bScratchVar and bWriteBuffer POINT to the same data. You don't have the data duplicated anywhere after realloc() returns.