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. Creating a pointer and asigning a variable address to it

Creating a pointer and asigning a variable address to it

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
7 Posts 4 Posters 13 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.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved. However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?

    L V 4 Replies Last reply
    0
    • C Calin Negru

      When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved. However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If you're responsible for managing memory you've allocated, then freeing it means it's gone; regardless of which part of your app did it. i.e. you're the one "counting the references".

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      • C Calin Negru

        When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved. However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        It is "undefined behaviour": so it could result in segmentation fault or it could behave like this delete would be ignored. Or something else. See the discussion [https://stackoverflow.com/questions/441831/calling-delete-on-variable-allocated-on-the-stack\](https://stackoverflow.com/questions/441831/calling-delete-on-variable-allocated-on-the-stack)

        1 Reply Last reply
        0
        • C Calin Negru

          When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved. However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          In case 1 you are creating a new block of dynamic memory space. When you call delete on the pointer it returns the memory space to the free pool, but the pointer still exists (until it goes out of scope, or the program ends). In case 2 the pointer is referring to memory space that already exists, i.e. it was not created by new, so you cannot call delete on it.

          C 1 Reply Last reply
          0
          • C Calin Negru

            When you both create a pointer and allocate memory (with “new”) that the pointer will point to, it’s almost like creating an invisible variable. When you want to dispose if you delete the pointer the case is solved. However if you declare a variable and assign the variable address to a pointer, later if you use “delete” on the pointer will that delete the content of the variable too?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The purpose of `delete` is not to delete things. What?? But I'm serious. The purpose of `delete` is to undo `new`. As an approximation that will no doubt anger several C++ experts, you can view `new` as a combination of `malloc` and invoking the constructor, and you can view `delete` as a combination of invoking the destructor and `free`. That's not exactly literally how they work, but it's close enough that you can use that to reason about when you need them. (don't use this as an excuse to `delete` something that was `malloc`'ed) Most of the time, if you want to get rid of something, `delete` is the wrong way to do it - because most things were not created by `new`. Trying to `delete` something else results *at best* in a failing safety check but in general corrupts the state of your application. Most objects are destroyed implicitly, in various ways. Local variables by exiting a scope, values in containers by the container being destroyed, smart pointers do most of the rest. Explicitly using `delete` is occasionally necessary but it's probably better to actively avoid creating those cases. It's quite bug-prone, especially around exceptions and other non-trivial control flow.

            Calin Negru wrote:

            later if you use “delete” on the pointer will that delete the content of the variable too?

            In the sense that its destructor is invoked (so you can implement it and `delete` the members that need to be deleted), yes. If you mean that in some other sense then, probably, no (but depending on what you mean). Implementing classes with lots of raw-pointer members that you individually `delete` in the destructor is very mid-2000s. Typical advice is to delegate that to smart pointers and containers as much as possible, with the goal of following the Rule of Zero instead of one of the other rules-of-some-number, but sometimes you just have to do what you have to do.

            CPalliniC 1 Reply Last reply
            0
            • L Lost User

              In case 1 you are creating a new block of dynamic memory space. When you call delete on the pointer it returns the memory space to the free pool, but the pointer still exists (until it goes out of scope, or the program ends). In case 2 the pointer is referring to memory space that already exists, i.e. it was not created by new, so you cannot call delete on it.

              C Offline
              C Offline
              Calin Negru
              wrote on last edited by
              #6

              Thank you to everyone for helping

              1 Reply Last reply
              0
              • L Lost User

                The purpose of `delete` is not to delete things. What?? But I'm serious. The purpose of `delete` is to undo `new`. As an approximation that will no doubt anger several C++ experts, you can view `new` as a combination of `malloc` and invoking the constructor, and you can view `delete` as a combination of invoking the destructor and `free`. That's not exactly literally how they work, but it's close enough that you can use that to reason about when you need them. (don't use this as an excuse to `delete` something that was `malloc`'ed) Most of the time, if you want to get rid of something, `delete` is the wrong way to do it - because most things were not created by `new`. Trying to `delete` something else results *at best* in a failing safety check but in general corrupts the state of your application. Most objects are destroyed implicitly, in various ways. Local variables by exiting a scope, values in containers by the container being destroyed, smart pointers do most of the rest. Explicitly using `delete` is occasionally necessary but it's probably better to actively avoid creating those cases. It's quite bug-prone, especially around exceptions and other non-trivial control flow.

                Calin Negru wrote:

                later if you use “delete” on the pointer will that delete the content of the variable too?

                In the sense that its destructor is invoked (so you can implement it and `delete` the members that need to be deleted), yes. If you mean that in some other sense then, probably, no (but depending on what you mean). Implementing classes with lots of raw-pointer members that you individually `delete` in the destructor is very mid-2000s. Typical advice is to delegate that to smart pointers and containers as much as possible, with the goal of following the Rule of Zero instead of one of the other rules-of-some-number, but sometimes you just have to do what you have to do.

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

                Amen to this.

                "In testa che avete, Signor di Ceprano?" -- Rigoletto

                In testa che avete, signor di Ceprano?

                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