Jump to content
  • Advertisement
Sign in to follow this  
sfx81

malloc to new

This topic is 3559 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

Hi all. I am trying to use replace malloc and free with new and delete in this function. //this is taken from microsoft site //http://msdn.microsoft.com/en-us/library/ms533843(VS.85).aspx int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = 0; // number of image encoders UINT size = 0; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size == 0) return -1; // Failure pImageCodecInfo = (ImageCodecInfo*)(malloc(size));//<<<<<This is I have to replace if(pImageCodecInfo == NULL) return -1; // Failure GetImageEncoders(num, size, pImageCodecInfo); for(UINT j = 0; j < num; ++j) { if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) { *pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo);//<<<<<<<<requires replacing return j; // Success } } free(pImageCodecInfo);//<<<<<<< requires replacing return -1; // Failure } my revision is this. It works fine, but to avoid any hidden bugg can any one please point out if I am doing any thing wrong in here. any help will be appreciated. Regards Kazz ------------------------------------- int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num = 0; // number of image encoders UINT size = 0; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size == 0) return -1; // Failure char* mem = new char[size];//<<<<my replacement for malloc pImageCodecInfo = (ImageCodecInfo*)(mem); if(pImageCodecInfo == NULL) return -1; // Failure GetImageEncoders(num, size, pImageCodecInfo); for(UINT j = 0; j < num; ++j) { if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) { *pClsid = pImageCodecInfo[j].Clsid; delete[] mem;//<<<<<deleting the allocated memory return j; // Success } } delete[] mem;//<<<<<deleting the allocated memory return -1; // Failure }

Share this post


Link to post
Share on other sites
Advertisement
I haven't looked at the code because you simply haven't asked anything here.

What are you trying to do? Are you worried about the correctness of your code? Will it not compile? These are all questions you need to address when posting, otherwise no one can help you.

You probably want to start with what you're trying to accomplish and an overview of the problem. You may find that your approach may not be the best, and people here can give you better alternatives if possible.

Good luck.

Share this post


Link to post
Share on other sites
I probably would have kept using malloc in that particular situation. You aren't correcting a bug you have found and the replacement code isn't really any cleaner that the original.

Note that new will never return 0 or NULL, so you can omit that check. If new cannot give you memory, it will throw an exception of type std::bad_alloc. This means that your revised function now has two ways of communicating an error, its return value and an exception.

Share this post


Link to post
Share on other sites
Quote:
Original post by GenuineXP
What are you trying to do? Are you worried about the correctness of your code? Will it not compile? These are all questions you need to address when posting, otherwise no one can help you.

You probably want to start with what you're trying to accomplish and an overview of the problem. You may find that your approach may not be the best, and people here can give you better alternatives if possible.


Quote:
Original post by sfx81
I am trying to use replace malloc and free with new and delete in this function.
(...)
my revision is this. It works fine, but to avoid any hidden bugg
can any one please point out if I am doing any thing wrong in here.


I would, however, question the point of replacing malloc/free with new/delete, suggest the use of [source][/source] tags, and worry about why there's an "array count" and "array size" pair of parameters on that darned function!

And I don't see any obvious flaws.

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!