Jump to content
  • Advertisement
Sign in to follow this  
blaze02

Amnesia = Forget How To Allocate Memory

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hopefully I will find the answer before someone replies, but I seem to have forgot how to use the new operator :( How would I go about dynamically allocating 4 void ptrs? Naive guess: void * temp = new (void*)[4]; And my naive guess does not compile. Also, you don't get to cheat and use predefined types (typedef, structs, etc). Feel free to flame cuz I should know this :( Thanks for any help. Edit: Naive guess should be "void ** temp..."

Share this post


Link to post
Share on other sites
Advertisement
No, DaAnde, in C++ we use 'new'.

Although - in C++ using 'void*' is generally a pretty bad idea. What are you *really* trying to do, blaze02?

Share this post


Link to post
Share on other sites
ha ha ha you could also do something like this too (though for your case it's overkill):
void** data = (void**)::operator new(4*sizeof(void*));

Share this post


Link to post
Share on other sites
I really can’t see the advantage of using new instead of std::malloc as long as we’re talking about void*’s. Using std::malloc would seem easier and more straightforward in this case. Of course, I’d suggest against using void* and rather use a normal dynamically-allocated array of non-void* types – preferably std::vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyd
I really can’t see the advantage of using new instead of std::malloc as long as we’re talking about void*’s. Using std::malloc would seem easier and more straightforward in this case.


But then you (and anyone using your code) has to know and remember that you've called std::malloc and not new in order to release it with std::free instead of delete.

Share this post


Link to post
Share on other sites
typedef's are handy for simplifying complex type expressions. e.g. (to borrow Windows' typedef):

PVOID * temp = new PVOID[4];

The of course there's always the obligatory appeal to use STL instead of manual memory management:

vector<void *> temp(4);

Which is even simpler.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!