Public Group

# Amnesia = Forget How To Allocate Memory

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

## 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..."

guess:
int *myVar[4];

##### Share on other sites
Figured it out. I shouldn't have included the parens around "void*"

void ** temp = new void*[4];

##### Share on other sites
void *malloc(size_t size);

##### Share on other sites
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 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 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 on other sites
Memory mem = new Memory(size);

mem.use();

delete mem;

8-P

##### Share on other sites
Quote:
 Original post by OxydI 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 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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 37
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999952
×