-
May 8th, 2012, 11:32 AM
#1
Questions about New and Delete
Hi
im afraid to create leaks while using heap, I have few questions,
how does delete [] knows how many memory was allocated in th 1st place.
Is it stored somewhere in the memory layout?
my confusion is about char* arrays since if i try to write with memcpy to &BmpFormatBuff instead of &BmpFormatBuff[0] it would write some binary data plus the string which lead me to think it stores the lenght somehwere in the memory layout.
what is going on there is a doub that cranks me up since some time ago.
thx in advance for your time.
-
May 8th, 2012, 11:53 AM
#2
Re: Questions about New and Delete
Originally Posted by Alphadan
im afraid to create leaks while using heap,
Then get rid of using new/delete! Use container classes instead: there are MFC container classes and STL ones. Just choose what you like better!
Originally Posted by Alphadan
how does delete [] knows how many memory was allocated in th 1st place.
Is it stored somewhere in the memory layout?
I guess, yes, it is stored somewhere. But why care? Just use the container classes!
Victor Nijegorodov
-
May 8th, 2012, 11:55 AM
#3
Re: Questions about New and Delete
The visual studio CRT has functionality that helps you both to detect and to track down the reason for a memory leak. See for instance this thread http://forums.codeguru.com/showthread.php?t=507470
I don't really understand what you're saying but yes, when you allocate memory the memory manager somehow makes a note of how many bytes the block you get contains. How it does that we don't know but that doesn't matter since it's nothing that we should tamper with. All we should do is to make sure we only write to addresses that belong to the allocated block and to release it when we're done with it.
An even better way is to avoid allocating raw memory in the first place. Use instead STL containers and strings whenever possible, see http://cplusplus.com/reference/stl/
-
May 8th, 2012, 12:14 PM
#4
Re: Questions about New and Delete
hmmm interesting ill consider using STL vectors, do they add much overhead? since im trying to work with time critical functions.
Thx for ur time once again.
-
May 8th, 2012, 12:24 PM
#5
Re: Questions about New and Delete
Originally Posted by Alphadan
hmmm interesting ill consider using STL vectors, do they add much overhead?
No, they don't.
Victor Nijegorodov
-
May 8th, 2012, 12:26 PM
#6
Re: Questions about New and Delete
Try using a vector, measure the performance and compare with a plain array. If you're not doing something very exotic I doubt that you will see any difference in performance.
Also read this http://msdn.microsoft.com/en-us/library/aa985965.aspx and this http://msdn.microsoft.com/en-us/library/aa985982.aspx
-
May 8th, 2012, 12:27 PM
#7
Re: Questions about New and Delete
do they add much overhead? since im trying to work with time critical functions.
Time critical and memory usage are 2 totally different things.
-
May 8th, 2012, 01:01 PM
#8
Re: Questions about New and Delete
its official im using vectors, thx everyone!!
-
May 8th, 2012, 04:04 PM
#9
Re: Questions about New and Delete
Your welcome! When you get fully in love with STL you might also want to look into the boost library as well www.boost.org (most are just headers)
Since your current concern was mostly memory leaks the Smart Ptr library is probably the most interesting at the moment but quickly browse thru all libraries, there is a lot of handy stuff there!
-
May 8th, 2012, 08:42 PM
#10
Re: Questions about New and Delete
I agree with S_M_A, however for shared_ptr, you can also use std::shared_ptr.
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
|