Vertex Dynamic Memory (malloc and free)

Started by
12 comments, last by Dave Hunt 8 years, 2 months ago


Btw, I'm curious why this happened?

Very often the devil's in the ellipsis. It's hard to say what happened without knowing what "..." is.

memory allocation

pMesh->pName = (char*)malloc(sizeof (char) * 33);
fread(pMesh->pName,...);
pMesh->pName[32] = '\0';
Advertisement

I was referring to the "..." in the fread call as well.

However, in your struct you declared cName as an array of 32 characters, then you likely attempted to set cName[32] to '\0'. Valid indices are 0-31. 32 is right out. It worked in your malloc version because you malloced 33 characters.

Dave Hunt, sorry for really late reply. Got so busy this week.

When I used array, I didn't set cName[32] to '\0'. Here is the way I call fread using array.

fread(cName, sizeof (char), 32, pIn);

Definitely my array was out of bound, because I only had 32. Recently, I tried to change the array size to 33. Still crashed.

Using pointer instead of array works fine. Perhaps I'm wrong?

Thank you very much so far, Dave Hunt.

If it works with the allocated pointer and not with the array, then you're doing something wrong. ;-)

We'd need to see your actual function to say for sure what's wrong.

This topic is closed to new replies.

Advertisement