[PD-dev] Pointers changing/corrupting (used to be "signal nan values")

Andrew Hassall a.r.hassall at gmail.com
Wed Apr 27 18:09:00 CEST 2011


When allocating memory with getbytes how do you refer to it as an
array? I can't seem to set values in the allocated memory using
*(mybytes+index) like normal.

On Wed, Apr 27, 2011 at 1:27 PM, Andrew Hassall <a.r.hassall at gmail.com> wrote:
> oh right didn't realise the bytes would be freed straight away, thank
> you so much!
> by creating variable in this form
> "x->win=getbytes(x->winsize*sizeof(t_sample));" do you still refer to
> them in the same way as arrays e.g. *(win+10) to get value at index
> 10?
>
> Thanks!!!
> Andy
>
> On Wed, Apr 27, 2011 at 12:59 PM, IOhannes m zmoelnig <zmoelnig at iem.at> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> some more comments, to fill in the gaps:
>>
>> On 2011-04-27 13:51, IOhannes m zmoelnig wrote:
>>> a quick glance at lpcanalysis_tilde_new() reveils, that you are
>>> allocating data on the stack (which will be freed as soon as you leave
>>> lpcanalysis_tilde_new()!), store their addresses and re-use them later
>>> (when the memory is already freed).
>>
>> which is not a good idea.
>> instead, you have to allocate on the heap, and manually free once you
>> don't need the data anymore.
>>
>>>
>>> you cannot do:
>>> <ko>
>>> void*lpca_new() {
>>>  //...
>>>  t_sample winlis[2088];
>>>  x->win=winlis;
>>>  //...
>>> }
>>> </ko>
>>>
>>> instead you have to do:
>>> <ok>
>>> void lpca_free(x) {
>>>  freebytes(x->win);
>>
>> which really should read:
>> freebytes(x->win, 2088*sizeof(t_sample));
>>
>>>  //...
>>> }
>>> void*lpca_new() {
>>>  //...
>>>  x->win=getbytes(2088*sizeof(t_sample));
>>>  //...
>>> }
>>> </ok>
>>
>> and of course it would be nice, if 2088 was not hardcoded, but insteadbe
>> settable; e.g.
>> <nice>
>> void lpca_free(x) {
>>  freebytes(x->win, x->winsize*sizeof(t_sample));
>>  //...
>> }
>> void*lpca_new() {
>>  //...
>>  x->winsize=2088;
>>  x->win=getbytes(x->winsize*sizeof(t_sample));
>>  //...
>> }
>> </nice>
>>
>>
>> fgmasdr
>> IOhannes
>>
>>
>> _______________________________________________
>> Pd-dev mailing list
>> Pd-dev at iem.at
>> http://lists.puredata.info/listinfo/pd-dev
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.11 (GNU/Linux)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iEYEARECAAYFAk24BRAACgkQkX2Xpv6ydvRiZwCguQoy5rXGsBSX+5BRrFssYDOi
>> EckAoKyLf08ZtXkFWZnraZF2Y9QOnDpP
>> =1g/W
>> -----END PGP SIGNATURE-----
>>
>>
>> _______________________________________________
>> Pd-dev mailing list
>> Pd-dev at iem.at
>> http://lists.puredata.info/listinfo/pd-dev
>>
>>
>



More information about the Pd-dev mailing list