Forums > Dev

Stupid noober crashing max with linklist

July 1, 2008 | 9:07 pm

Hey all.. just learning c here so please don’t murder me when you see this code just politely smack me and tell me whats wrong..

anyway I want to learn to use linklist… and this is crashing max. I put it in main just for testing.

t_linklist * myll = linklist_new();
t_atom * myAtom;
myAtom->a_w.w_float = 4;

linklist_append(myll,myAtom);

t_atom * tempat ;
tempat = linklist_getindex(myll,0) ;
float j = tempat->a_w.w_float;

printf("———-%f",j );


July 1, 2008 | 10:56 pm

Welcome to the delights of memory management in C ;-)

That atom pointer you are declaring has no memory until you allocate some. So when you attempt to stick a float in there, you’re stomping on something else’s memory.

Try this (not tested):

t_linklist *myll = linklist_new();
t_atom *myAtom = null;
t_atom *tempat = null;
float j = null;

// attempt to allocate memory
if(myAtom = getbytes(sizeof(t_atom))){
myAtom->a_w.w_float = 4; // or use SETFLOAT
linklist_append(myll,myAtom);

// no allocation needed here, j points to existing atom
tempat = linklist_getindex(myll,0);
j = tempat->a_w.w_float;
printf("———-%f",j );

// free the memory when you’re done with it
freebytes(myll, sizeof(t_atom));
}
else error("couldn’t allocate memory");


July 1, 2008 | 11:03 pm

Quote: johnpitcairn wrote on Wed, 02 July 2008 10:56
—————————————————-
> // no allocation needed here, j points to existing atom

Uh, that’s "tempat points to existing atom", sorry.


July 1, 2008 | 11:05 pm

Er, and:

// free the memory when you’re done with it
freebytes(myAtom, sizeof(t_atom));

Need coffee I think.


July 2, 2008 | 9:36 am

Quote: johnpitcairn wrote on Wed, 02 July 2008 00:56
—————————————————-
> Welcome to the delights of memory management in C ;-)
—————————————————-

This is not C-specific but is a fundamental fact of life in (almost?) all programming languages. Defining a pointer does not automagically allocate heap space for data. Java, C++, C, Pascal, FORTRAN, Perl… you name it. You want memory, you got to got to got to got to got to allocate it.

The only exceptions I can think of offhand are languages that don’t support dynamic allocation of heap space at all, like (early-ish) BASIC dialects, COBOL, …

– P (w/apols to John Lennon)


July 3, 2008 | 1:44 pm

Welcome? WELCOME? this is not welcoming at all.. Wow… well this does work but I’m starting to rethink my entrance into C. This isn’t music at all is it?

I guess the thing I’m most confused about is (as you said) memory management. When does C do it, when do I do it, when does the max api do it?

I’m going to reread the WritingExternals once. then I might give up.

Anyone know if the documentation for the new SDK will be different?


July 3, 2008 | 7:58 pm

Quote: Matthew Aidekman wrote on Thu, 03 July 2008 14:44
—————————————————-
>
> I guess the thing I’m most confused about is (as you said) memory management. When does C do it, when do I do it, when does the max api do it?
>
> I’m going to reread the WritingExternals once. then I might give up.
>

Hello, I would suggest not giving up. It sounds like you just need to get a little more C experience. So, reading WritingExternals probably won’t help just yet.

In C if you do:

int anInt;

the the compiler allocates you some memory and allows you to refer to that memory as an integer using the variable name "anInt". It also frees the memory when it’s no longer needed.

To allocate memory yourself you need to use "pointers". You need to read a chapter in a good C book really but Max has its own memory allocation and freeing routines so this may not seem immediately relevant.

In your favourite C book you’ll probably see memory allocated using "malloc" and freed using "free":

int* intPointer = malloc(sizeof(int)); // allocate memory
*intPointer = 10; // the memory allocated by malloc now contains 10
free(intPointer); // free the memory when you’re done

Using Max’s memory routines:

int* intPointer = getbytes(sizeof(int)); // allocate memory
*intPointer = 10; // the memory allocated by getbytes now contains 10
freebytes(intPointer, sizeof(int)); // free the memory when you’re done

Max’s routines have some benefits but you can use malloc/free. As you can see the main difference in terms of the code is that freebytes needs to know how big the allocated memory block was when it was allocated with getbytes. free doesn’t need to know how big the memory allocated with malloc was.

BTW: You definitely need to understand pointers very clearly before you start dabbling in data structures like linked lists! :)

Good luck!


July 3, 2008 | 10:58 pm

I found this PDF from Stanford useful as an introduction to pointers and memory:

http://cslibrary.stanford.edu/102/

BTW, you need to free the linklist when you’re done with that too. How you free it depends on what you’ve been storing in it.

linklist_chuck(myll) will free it without attempting to free any of the items it contains. This is probably what you’ll want, if you’re not storing t_objects in it (and possibly even if you are).

There has been plenty written elsewhere on this forum about the use of getbytes/freebytes vs malloc/free, a search should find that.


July 3, 2008 | 11:35 pm

John, I’ve read…wait let me count… 7 pointer tutorials so far. this is by far the best. Thank you

I’ll try and do my best to get through this. you might have to look for one or two more well researched idiotic questions. sometimes it’s hard for me to realize what’s a max question.

the other day I posted a question about custom linked lists and someone said "what are you nuts??? no one codes that by hand anymore. pick an appropriate array length and stick with it" then I found ext_linklist

Thanks guys, I appreciate you enduring my pessimism.


Viewing 9 posts - 1 through 9 (of 9 total)