|
-
July 21st, 2008, 02:39 PM
#9
Re: Dynamic Memory Allocation Which is best?
 Originally Posted by Dave1024
Reason behind the search for various methods.
char *kk;
kk = new char[1000];
//Array Use (Copy a .txt file content to a .doc file)
delete []kk;
kk = NULL;
When i use this program runs success.Output is OK But compile in VS2005 with warning level 4 I got following message
Leaking memory 'kk' due to an exception. Consider using a local catch block to clean up memory:
Which method will avoid this problem.....
Code:
try
{
char* kk = 0;
kk = new char[1000];
// do stuff with your buffer
delete []kk;
kk = NULL;
}
catch()
{
// prevents program from crashing if bad_alloc is thrown.
}
Yes, the catch is empty but it prevents the program from crashing due to unhandled bad_alloc exception. That compiler warning doesn't stop your program from executing the sunny day scenario. It's just telling you that if an exception were to occur, there is no way to catch it.
Worry about writing a program correctly first. Worry about whether it is fast enough later. In this case, I really don't know why we are talking about speed. This is not a complex problem where speed should be an issue. I tend to prefer vectors over arrays because my code looks nicer and the vector provides a convenient object oriented interface which improves the overall quality of the software. I have never had a problem with a vector causing my software to be so slow that it wouldn't work. I'm not saying that it couldn't happen. If it did, you could analyze the program and refactor it. Worrying about which will be faster, before you even write the program and profile it is a complete waste of time in this case.
Last edited by kempofighter; July 30th, 2008 at 05:09 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|