🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Malloc

Started by
4 comments, last by Miraj 24 years, 3 months 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
Advertisement
*politely bumps the post a notch and scurries off*



-Miraj
-Miraj
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