CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2009
    Posts
    252

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Questions about New and Delete

    Quote Originally Posted by Alphadan View Post
    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!

    Quote Originally Posted by Alphadan View Post
    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

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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/
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Feb 2009
    Posts
    252

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Questions about New and Delete

    Quote Originally Posted by Alphadan View Post
    hmmm interesting ill consider using STL vectors, do they add much overhead?
    No, they don't.
    Victor Nijegorodov

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  8. #8
    Join Date
    Feb 2009
    Posts
    252

    Re: Questions about New and Delete

    its official im using vectors, thx everyone!!

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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!
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #10
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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
  •  





Click Here to Expand Forum to Full Width

Featured