Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Allocation: new vs. malloc vs. ?

Memory Allocation: new vs. malloc vs. ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiodata-structuresdebuggingperformance
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nadiric
    wrote on last edited by
    #1

    Thanks to all that helped me get over my debug issues... It turns out (as many suggested) that the code I was porting from 'c' and wrapping in an MFC gui had to lose all the old malloc/reallocs it had to work correctly without causing debug to puke. This manifested itself in strings for the most part. From what several said, malloc uses a different stack than new, and was causing one to stomp on the other. I must ask at this point...why? Is there a "safe" wrapper out there for malloc (to possibly save re-writing old code)? why did only debug have a problem with sharing methods? Based on call stacks examined while getting to the bottom of this, I saw CString using malloc and free deep down inside... why don't these cause issues? Thanks again

    C J 2 Replies Last reply
    0
    • N nadiric

      Thanks to all that helped me get over my debug issues... It turns out (as many suggested) that the code I was porting from 'c' and wrapping in an MFC gui had to lose all the old malloc/reallocs it had to work correctly without causing debug to puke. This manifested itself in strings for the most part. From what several said, malloc uses a different stack than new, and was causing one to stomp on the other. I must ask at this point...why? Is there a "safe" wrapper out there for malloc (to possibly save re-writing old code)? why did only debug have a problem with sharing methods? Based on call stacks examined while getting to the bottom of this, I saw CString using malloc and free deep down inside... why don't these cause issues? Thanks again

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      new and delete call malloc/free internally, I believe, but if you're writing C++, the rule is to use new/delete. Apart from anything else, it gives you constructor/destructor behaviour.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      1 Reply Last reply
      0
      • N nadiric

        Thanks to all that helped me get over my debug issues... It turns out (as many suggested) that the code I was porting from 'c' and wrapping in an MFC gui had to lose all the old malloc/reallocs it had to work correctly without causing debug to puke. This manifested itself in strings for the most part. From what several said, malloc uses a different stack than new, and was causing one to stomp on the other. I must ask at this point...why? Is there a "safe" wrapper out there for malloc (to possibly save re-writing old code)? why did only debug have a problem with sharing methods? Based on call stacks examined while getting to the bottom of this, I saw CString using malloc and free deep down inside... why don't these cause issues? Thanks again

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        The key is if you use malloc/realloc to allocate memory you use free to deallocate the memory. If you use new to allocate memory you use delete to deallocate it. When I port code from C, I don't bother changing the malloc's. There's no point and it often causes more bugs than just leaving the mallocs/frees alone.

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups