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? Many thanks Isi
--- On Tue, 10/13/09, Justin Glenn Smith noisesmith@gmail.com wrote:
From: Justin Glenn Smith noisesmith@gmail.com Subject: Re: [PD-dev] Memory reallocation problem To: "Isidro Gonzalez" isidro_gonsoy@yahoo.com Cc: pd-dev@iem.at Date: Tuesday, October 13, 2009, 4:10 PM Isidro Gonzalez wrote:
Hi: I am triying to allocate a n*n matrix of a type
defined structure of data.
The pd external object example I'm sending attached is
very simple: each time it receives any message except the "clear" message it increments a counter and reallocate a matrix of n*n members where n is the number stored in the counter (so, 1, 4, 9, 16 and so on). If it receives the "clear" message, it reset the counter to 1 and reallocate the matrix to 1x1.
The fact is that the object crashed when reallocating
and I can't be able to know what am I doing wrong. I use the traditional "realloc" function.
Any help will be welcome.
realloc does no initialization of data.
In the for loop in the dummy_alloc function you are calling realloc on uninitialized pointers. The fact that this ever fails to crash is because you are by chance getting NULL pointers from the realloc when you increase the size of a.
You need to manually set the newly acquired bytes in a to be NULL before you can safely call realloc with them as arguments.