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. a doubt ?

a doubt ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    someobject *var=new someobject[100]; delete var; ---------------- someobject *var=new someobject[100]; var=null; --------------- what is the difference betn these two statements?

    X 1 Reply Last reply
    0
    • A Anonymous

      someobject *var=new someobject[100]; delete var; ---------------- someobject *var=new someobject[100]; var=null; --------------- what is the difference betn these two statements?

      X Offline
      X Offline
      Xander80
      wrote on last edited by
      #2

      I'm not sure, but I think that someobject *var=new someobject[100]; delete var; frees alocated memory and someobject *var=new someobject[100]; var=null; changes the pointer to null

      A 1 Reply Last reply
      0
      • X Xander80

        I'm not sure, but I think that someobject *var=new someobject[100]; delete var; frees alocated memory and someobject *var=new someobject[100]; var=null; changes the pointer to null

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Does that mean, when the pointer is made null, the memory it pointed to also gets freed. can "var=null" be used inplace of "delete var;" ?!!

        J I 2 Replies Last reply
        0
        • A Anonymous

          Does that mean, when the pointer is made null, the memory it pointed to also gets freed. can "var=null" be used inplace of "delete var;" ?!!

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          No, as C++ is not garbage collected var=NULL is probably an error and generates a leak in your application. Nobody will take care of the memory you leave behind undeleted. This doesn't necessarily mean a memory leak if var points to some memory block that is also pointed to by some other pointer, and you properly delete that. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          • A Anonymous

            Does that mean, when the pointer is made null, the memory it pointed to also gets freed. can "var=null" be used inplace of "delete var;" ?!!

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            "var=NULL" is NOT the same as "delete var". If you imagine that

            BYTE *var= new BYTE [100]

            allocates a lump of memory for your use (it may be longer than 100 bytes long) and sets
            var to point to it.

            var=NULL;

            simply makes your variable point "nowhere". The memory is still allocated.

            delete [] var;

            frees the memory, but your variable is still pointing to the now freed patch or memory.
            You could still use it, but not safely. Note the [] in the delete command as you
            are freeing an array.

            You should be doing something like:

            BYTE *var = new BYTE [100]; // Allocate some short term memory

            ... // Use it for some purpose

            delete [] var; // "give it back"
            var = NULL; // Forget about it so we don't use it my mistake.

            Pointers are one of those things you struggle with for a while then wake up one morning
            going "Ahah!"

            Iain.

            A 1 Reply Last reply
            0
            • I Iain Clarke Warrior Programmer

              "var=NULL" is NOT the same as "delete var". If you imagine that

              BYTE *var= new BYTE [100]

              allocates a lump of memory for your use (it may be longer than 100 bytes long) and sets
              var to point to it.

              var=NULL;

              simply makes your variable point "nowhere". The memory is still allocated.

              delete [] var;

              frees the memory, but your variable is still pointing to the now freed patch or memory.
              You could still use it, but not safely. Note the [] in the delete command as you
              are freeing an array.

              You should be doing something like:

              BYTE *var = new BYTE [100]; // Allocate some short term memory

              ... // Use it for some purpose

              delete [] var; // "give it back"
              var = NULL; // Forget about it so we don't use it my mistake.

              Pointers are one of those things you struggle with for a while then wake up one morning
              going "Ahah!"

              Iain.

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thanks guys!! That was tremenodus response in no time at all. You're all great. Life is so much easier with you. God Bless All.

              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