[PD-dev] Memory reallocation problem

Justin Glenn Smith noisesmith at gmail.com
Tue Oct 13 21:35:00 CEST 2009


Isidro Gonzalez wrote:
> Many thanks for your kind answer.
> I´m surprised because the way I use to allocate memory is the most commonly used.
> Wouldn´t initializing the pointers to NULL destroy all the contents of previous memory they hold, if any...? So, why to use "realloc" this way, then? I would use "free" and "malloc", instead.
> Your advice works fine but when I try to use the memory, the object makes PD to crash.
> Any further ideas? 

The issue is that you need to initialize the data that realloc gives
you, but not touch the data you already had.

Using realloc is common enough, but you cannot count on the value of the
memory returned by realloc, it will be NULL much of the time, but can
also have random data in it. If you pass a NULL pointer to a call to
realloc you get a fresh patch of memory, if you send a random
uninitialized pointer that did not happen to be NULL, that is where you
are getting your segmentation violation.

This should be simple enough to fix if you use an extra variable to
store the previous dimensions of a, and then initialize your loop
variables accordingly.

I also recommend using the pd library functions getbytes and
resizebytes, which work like malloc and realloc except you need to track
the size of objects they return yourself, and I believe they are
designed to prevent hiccups in audio when used realtime.




More information about the Pd-dev mailing list