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. pointer deletion querry

pointer deletion querry

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 5 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.
  • D Offline
    D Offline
    Deepu Antony
    wrote on last edited by
    #1

    Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

    C R D CPalliniC T 5 Replies Last reply
    0
    • D Deepu Antony

      Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Deepu Antony wrote:

      delete * p;

      It's like: delete p;

      Deepu Antony wrote:

      Whether pointer q is also created on the heap.

      Nope, heap allocation occurs only when you use any of the heap allocation functions (new, calloc, malloc, etc.,)

      Deepu Antony wrote:

      Is q = NULL is correct?

      No. q has the address of the variable a. And can be pointing to some random value if you hadn't assigned it like that.

      It is a crappy thing, but it's life -^ Carlo Pallini

      1 Reply Last reply
      0
      • D Deepu Antony

        Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Deepu Antony wrote:

        Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

        No, q is not allocated on the heap. It just contains the address of a, you didn't allocate anything for it. Keep in mind a very simple rule: for each call to new, you should have a matching delete. No more, no less. In this case, you have one single new (for the p pointer), so you have to call delete on this pointer, and that's it.

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • D Deepu Antony

          Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

          D Offline
          D Offline
          Deepu Antony
          wrote on last edited by
          #4

          Thank you all very much.

          1 Reply Last reply
          0
          • D Deepu Antony

            Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Deepu Antony wrote:

            Whether pointer q is also created on the heap. If so how to delete it?

            If the momory pointed by q (pointers are ususally created on the stack) is created on the heap then you must use delete.

            Deepu Antony wrote:

            Is q = NULL is correct?

            Again, when q points heap-allocated memory you've to delete it. Invalidating the pointer afterward is a good pactice (for instance):

            int * q;
            q = new int[10];
            //...
            //...
            if (q) delete [] q;
            q = NULL;

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • D Deepu Antony

              Hi everybody I am having a small querry int * p = new int(10); int a = 10; int * q = &a; then to delete pointer p delete * p; Whether pointer q is also created on the heap. If so how to delete it? Is q = NULL is correct?

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              If you delete memory, which you didn't allocate dynamically, you surly putting yourself in trouble. Remember simple rule, you only need to delete the memory, which is you allocate to the program! <blockquote class="FQ"><div class="FQA">Deepu Antony wrote:</div>Is q = NULL is correct?</blockquote> yes!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              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