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. Very simple question :o)

Very simple question :o)

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • M Offline
    M Offline
    MemLeak
    wrote on last edited by
    #1

    Hi, I have something like: Object* MyClass::MyFunction(int value) { Object* pObj = new Object(value); if(!pObj) return NULL; else return pObj; } do I have to delete pObj? and if I have to delete it, how can I do it? Do I need to call a delete everytime everywhere? Thanks Everything's beautiful if you look at it long enough...

    R J 2 Replies Last reply
    0
    • M MemLeak

      Hi, I have something like: Object* MyClass::MyFunction(int value) { Object* pObj = new Object(value); if(!pObj) return NULL; else return pObj; } do I have to delete pObj? and if I have to delete it, how can I do it? Do I need to call a delete everytime everywhere? Thanks Everything's beautiful if you look at it long enough...

      R Offline
      R Offline
      Roger Allen
      wrote on last edited by
      #2

      You could have written:

      CObject* MyClass::MyFunction(int value)
      {
      return new Object(value);
      }

      As new returns NULL if the allocation fails. That said, what you are doing is bad coding practice as whoever calls the function needs to remember to delete the returned pointer, and its possible to use a function in such a way that you don't get a copy of the returned pointer, as it would get assigned to a temporary. So its very bad practice. See if you can do it in some other way. Roger Allen Sonork 100.10016 Were you different as a kid? Did you ever say "Ooohhh, shiny red" even once? - Paul Watson 11-February-2003

      1 Reply Last reply
      0
      • M MemLeak

        Hi, I have something like: Object* MyClass::MyFunction(int value) { Object* pObj = new Object(value); if(!pObj) return NULL; else return pObj; } do I have to delete pObj? and if I have to delete it, how can I do it? Do I need to call a delete everytime everywhere? Thanks Everything's beautiful if you look at it long enough...

        J Offline
        J Offline
        Joan M
        wrote on last edited by
        #3

        The gold rule: if you use new you must delete, if you use malloc you must free... but the other person who has answered is right, it is a bad coding style, it can lead to lots of errors if the coder is not aware of that... Hope this helps.

        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