realloc() only works with memory that has been previously allocated with malloc(), calloc() or realloc()
ProtocolReceiveBuffer = realloc(ProtocolReceiveBuffer, (rows)*sizeof(unsigned int*));
It is not recommended to use the same pointer to reallocate memory. If it fails you have lost the point to that memory creating a memory leak.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
> but if I call malloc every time, it would be a memory leak wouldn't it?
It would actually seem simpler.
If rows and cols are variable each time around the loop, then allocate / do stuff / free might be a lot simpler.
realloc is for when you're say reading a file, and you want 10,20,50,100,1000... items (the size just keeps growing, and you have no idea where to start).
Also, if you have a reasonable upper bound to the sizes, then just allocating the max for everything at the start would save a lot of work in the loop.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953