Forums > Dev

printing the first atom of an t_atomarray [More noob stuff]

December 14, 2012 | 4:52 am

(sorry for the batter of bugs. I promise I’m trying hard to figure this stuff out on my own.)

I have this code which some how miraculously works outside a function but once you put it into a function it crashes max. It’s supposed to print the first atom of a t_atomarray. I literally replace the code with a function call that does the exact same thing and get complete different results. Any ideas on trouble shooting this?

Here is is outside the function

t_atom *firstAtom;
        //this is the line that crashes
	atomarray_getindex(text,0,firstAtom);
	long sizeSize =0;
	char *firstAtomText;
	atom_gettext(1,firstAtom,&sizeSize,&firstAtomText,OBEX_UTIL_ATOM_GETTEXT_DEFAULT );
	post("%s",firstAtomText);

Here is is inside a function

void moopants(t_atomarray *moo)
{

	t_atom *firstAtom;
        //this is the line that crashes in a function
	atomarray_getindex(moo,0,firstAtom);
	long sizeSize =0;
	char *firstAtomText;
	atom_gettext(1,firstAtom,&sizeSize,&firstAtomText,OBEX_UTIL_ATOM_GETTEXT_DEFAULT );
	post("%s",firstAtomText);

}

Here’s more info if you need it. In the file I’m I’m getting the t_atomarray from a linked list of t_atomarrays. If I pass the new function a t_linklist like the following, it still crashes. I’ve checked that the pointers are the same inside and outside the function. It’s operating on the exact same place in memory. I’ve double checked that there’s no casting on in the function call by explicitly casting the pointer to the expected type outside the function. still no crash. It seems as though the sheer act of having this code in a separate function makes max crash.

Any clues would be greatly appreciated

void moopants(t_linklist *linesOfAtoms){

	t_atomarray *text = linklist_getindex(linesOfAtoms, 4);	

	t_atom *firstAtom;
	atomarray_getindex(text,0,firstAtom);
	long sizeSize =0;
	char *firstAtomText;
	atom_gettext(1,firstAtom,&sizeSize,&firstAtomText,OBEX_UTIL_ATOM_GETTEXT_DEFAULT );
	post("%s",firstAtomText);

}

December 14, 2012 | 5:47 am

Been trying some stuff. Figured I’d provide as much info as possible.

t_atom *firstAtom = malloc(sizeof(t_atom));  //this causes the code to work;

Although, correct me if I’m wrong but if I assign a different pointer to firstAtom after this line, isn’t that a memory leak?

Also, this code prints ""

void moopants(t_linklist *linesOfAtoms){

	t_atomarray *text = linklist_getindex(linesOfAtoms, 4);

	long one = 1;
	t_atom *firstAtom;
	char *trueornot;
	atom_alloc(&one, &firstAtom, trueornot);
	atomarray_getindex(text,0,firstAtom);
	long sizeSize =0;
	char *firstAtomText;
	atom_gettext(1,firstAtom,&sizeSize,&firstAtomText,OBEX_UTIL_ATOM_GETTEXT_DEFAULT );
	post("%s",firstAtomText);
}

December 14, 2012 | 5:10 pm

Thank you so much for your replies. Guys, I may be in over my head which is depressing because I’m at the end of a class on C.

I honestly didn’t about max’s memory management calls. Now that I read up on it I’m even more confused! I had no clue pointers could have different sizes, I thought pointers were all memory addresses with the same number of bits. While I see the "Memory Allocation" section in the documentation, it explains nearly nothing. I wish I had clearer words of wisdom.

Even worse is that while I see the functional difference between these two lines, I don’t understand how I’m supposed to infer which is appropriate for each argument from the documentation.

char *myChar;
myFuncion(myChar);
char myChar;
myFunction(&myChar);

How am I supposed to know which to pick which of these approaches to take for each argument when the documentation just says this?
atom_gettext (long ac, t_atom *av, long *textsize, char **text, long flags)

Hrmm… I have a lot to learn.


December 14, 2012 | 6:30 pm

Thanks! I’m working my ass off.

As for pointer sizes, from the documentation:

long 	sysmem_ptrsize (void *ptr)
 	Find the size of a pointer.

December 14, 2012 | 6:54 pm

I honestly had no clue you could measure the size of anything in C without the type so I assumed that it was the size of the address. Again, I’m a total noob so this is really nitpicky but I’d have expected something like "return: the number of bytes allocated to the thing the pointer is pointing to" or something like that. To me a pointer is just a single number. I’d think this would be the same as saying sizeof().

Whatever I don’t want to bother you guys more. I’m going to do some reading!!!! :-)


December 14, 2012 | 10:17 pm

Hi.
I think that in most systems the size of a memory block is stored right before the beginning of the block itself – so, sysmem_ptrsize should look more or less like

size_t sysmem_ptrsize(void *ptr)
{
    return ((size_t *)ptr)[-1];
}

(but I might as well be wrong… might do some research, but I’m feeling lazy ;) )
Cheers
aa


December 14, 2012 | 10:19 pm

HUH! I know C++ does some stuff like this, especially in objects and function calls. Didn’t know about raw pointers though.

Enlightening!
Thanks guys!


January 7, 2013 | 3:08 pm

sysmem_newptr() has not always been just a wrapper to malloc(), nor is it guaranteed to always be wrapper of malloc in the future. So, please don’t count on this being a wrapper of malloc(), or you risk inflicting pain and suffering on both yourself and on others.

Thanks!
Tim


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