Advertisement

Malloc

Started by March 11, 2000 02:57 AM
4 comments, last by Miraj 25 years ago
Hi again all Okay I know I''m a pest, but things are improving! A couple question whenever someone gets the time. Is the malloc function the same as the C++ keyword ''new'' ? And the second question is: How would I convert this line below to pure C sector1.triangle = new TRIANGLE[numtriangles]; (Taken from NeHe''s tutorials by the way, have to place credit where its due, even for one small line) I know that new allocates dynamic memory, could I do the same thing with malloc? If so how exactly? Thanks again...
-Miraj
*politely bumps the post a notch and scurries off*



-Miraj
-Miraj
Advertisement
My pure C is a little dusty, but I believe this should work:

sector1.triangle = (TRIANGLE*) malloc(sizeof(TRIANGLE)*numtriangles);

[where TRIANGLE is the appropriate triangle struct]

They then can be referenced by sector1.triangle[index]
Remember to: free(sector1.triangle);
malloc() simply allocates the memory. new, when called on classes, executes its constructor as well as allocating the memory for the class.
Remember not to mix calls of the memory operators.

that is, if you go
ptr = new int;

don''t go
free(ptr); // should use delete

I''ll just back TV up and say that is the correct
way to call malloc.

Chill,

-Mezz
Well after 2 days, not only have I gotten the program to function properly but I also learned alot about the differences between C and C++, though not many in some respects I still feel enlightened, heh. Not to mention Malloc and new

Thanks again guys, I dunno what I''d do without ya''s! =)



-Miraj
-Miraj

This topic is closed to new replies.

Advertisement