Malloc Question

Started by
6 comments, last by Fruny 19 years, 2 months ago
I read the post below and I didn't see an answer that worked. I want to declare a Sector for a OpenGl project. And it has to have an array of Triangles in it which is another structure. But I want to make the array of triangles the size that I load from the file. So I was going to use
sector1.triangle = new TRIANGLE[numtriangles];
but You can't use new in Cocoa on mac. triangle in the Sector is a Pointer to the structure Triangles. So how would I make say I load the number 36 make sector1.triangle[36] using maloc? Thanks vbuser
Advertisement
sector1.triangle = malloc( numtriangles * sizeof( TRIANGLE ) );
Ya I tried that but I got this error.
*** malloc: vm_allocate(size=3021615104) failed (error code=3)*** malloc[1376]: error: Can't allocate region

any ideas
Thanks vbuser
Quote:Original post by vbuser1338
*** malloc: vm_allocate(size=3021615104) failed (error code=3)

3021615104 is the size you're trying to allocate in bytes, it seems. I don't think so, that's a hell of a lot of RAM. Your numtriangles value is wrong.
It looks like you are asking it to malloc 3GB of memory. Do you have that much memory on your computer? if not, then you have too many TRIANGLES, or as mentioned above, numTriangles is wrong.

-me
It seems to be trying to allocate 3gig of data, and, well, thats rather huge.

Either sizeof(TRIANGLE) is insanely large, or numtriangles is insanely large.

What is sizeof(TRIANGLE) and numtriangles supposed to be?
Ya I'm stupid when I went to load the num triangles I was going to a point in the file that didn't exist . I was trying to get the number after numPolygons and In my file it is numTriangles so It was putting a weird number into numtriangles. Another stupid mistake. But thanks for the help.
vbuser
Quote:Original post by vbuser1338
but You can't use new in Cocoa on mac.


Mmmmmm, Objective-C...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement