# replacing malloc with new

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

## Recommended Posts

hello, i was just going over some code for loading a raw image from one of the nehe tutorials and find that is done in c, so i converted to more c++ type by chaning the typedef in structs, and using streams, and then i needed to change how the memory is allocated ... however i ran into a little problem, with new i must specify a type, but with malloc it seems you get to specify a size ... for example to allocate memory for a raw image is simple ... malloc (width * height * format) ... howver how can i do this with using new? also if i keep the malloc will using delete clear this memory properly or must i use free to do this? thanks for any help. EDIT: just a thought ... would new sizeof (w * h * f) ... work?

##### Share on other sites
well, when using new u say for example:
char * alfa = new char[45] and u got a 45 array o f char.
so u just have to use some 1 byte type and put [width*height*...]. pretty simple

##### Share on other sites
Quote:
 Original post by ekraxhow can i do this with using new?

typedef char byte;typedef byte* byte_ptr;byte_ptr p = new byte[width * height * format];

Quote:
 Original post by ekraxalso if i keep the malloc will using delete clear this memory properly or must i use free to do this?

your better off not mixing C memory management facilities with C++'s memory management facilities.

Just need to know that C's memory functions deal with unintialized memory so when you "malloc" no constructor is invoked and when "free" no destructor is invoked but this is okay for just raw bytes of memory like pixels but it doesn't gel well with c++ user-defined types.

EDIT: i may aswell point out if you use malloc with a c++ user-defined type you can invoke the construtor afterwords to initialize using placement new operator, when your done you invoke the destructor expliclity before you call free on the memory, so in the end it isn't really worth it.

##### Share on other sites
If you know how many bytes you need to allocate, you can easily allocate a char array to do this for you.
char* pRawImageData = new char[Width * Height * BytesPerPixel]

Don't forget to delete useing "delete [] pRawImageData;, though. Those brackets are required by the standard when deleting an array, and depending on the compiler, they might also be required by the compiler. (On some compilers you can get away without them, but this isn't recommended practice.)

And no, it's not wise to use delete on pointers that were malloced. If you use new, use delete. If you use new [], use delete []. And if you use malloc, use free.

Dude, was I ever slow, yet again. This has been happening a lot today.[/edit]

##### Share on other sites
wow, i sure should have remembered that a char was a byte. thanks for everyones help.

• 10
• 48
• 12
• 10
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999681
×