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

Pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
8 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.
  • D Offline
    D Offline
    dehseth
    wrote on last edited by
    #1

    Hey ppl, I have two pointers and one value:

    int* p1 = new int;
    *p1 = 10;

    int *p2;
    p2 = p1;

    Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:

    delete p1;
    p1 = NULL;

    Now p1 is NULL and referenced the only integer is deleted. Now if I try

    delete p2;
    p2 = NULL;

    Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?

    P S S 3 Replies Last reply
    0
    • D dehseth

      Hey ppl, I have two pointers and one value:

      int* p1 = new int;
      *p1 = 10;

      int *p2;
      p2 = p1;

      Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:

      delete p1;
      p1 = NULL;

      Now p1 is NULL and referenced the only integer is deleted. Now if I try

      delete p2;
      p2 = NULL;

      Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      dehseth wrote:

      p2 = p1;

      But, why one would like to do that ? Heard of need for copy c'tor and assingment operators ? Or dangling pointers ?

      D 1 Reply Last reply
      0
      • D dehseth

        Hey ppl, I have two pointers and one value:

        int* p1 = new int;
        *p1 = 10;

        int *p2;
        p2 = p1;

        Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:

        delete p1;
        p1 = NULL;

        Now p1 is NULL and referenced the only integer is deleted. Now if I try

        delete p2;
        p2 = NULL;

        Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        dehseth wrote:

        I read about shared pointers with templated which are not good for me to use right now?

        In case you change your mind: shared_ptr[^] from Boost[^].

        Steve

        D 1 Reply Last reply
        0
        • D dehseth

          Hey ppl, I have two pointers and one value:

          int* p1 = new int;
          *p1 = 10;

          int *p2;
          p2 = p1;

          Now p1 and p2 has different addresses (of course). They both points the same unnamed integer value. Lets delete p1:

          delete p1;
          p1 = NULL;

          Now p1 is NULL and referenced the only integer is deleted. Now if I try

          delete p2;
          p2 = NULL;

          Just crashes cause int which is referenced by p2 is not valid anymore. So how can I check if int is still valid OR maybe make p2 = NULL when object is deleted.. I read about shared pointers with templated which are not good for me to use right now? Any workarounds?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          You could always try designing your software so you've got a clear, single owner of resources such as allocated heap memory. Alternatively,

          • Use .NET - garbage collection means you don't need to worry about object ownership quite as much
          • Use Objective C 2.0 - it allows garbage collection, and garbage collection means you don't need to worry about object ownership quite as much
          • Use the Boehm allocator[^] - it's garbage collected, and garbage collection means you don't need to worry about object ownership quite as much
          • As other posters have suggested, use Boost's shared_ptr class.

          If you want to stick with Windows and C++, I'd go for designing your software to manage resources or Boost shared_ptrs.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          D 1 Reply Last reply
          0
          • P prasad_som

            dehseth wrote:

            p2 = p1;

            But, why one would like to do that ? Heard of need for copy c'tor and assingment operators ? Or dangling pointers ?

            D Offline
            D Offline
            dehseth
            wrote on last edited by
            #5

            Just need it... :)

            1 Reply Last reply
            0
            • S Stephen Hewitt

              dehseth wrote:

              I read about shared pointers with templated which are not good for me to use right now?

              In case you change your mind: shared_ptr[^] from Boost[^].

              Steve

              D Offline
              D Offline
              dehseth
              wrote on last edited by
              #6

              Nope, but thanx... :)

              1 Reply Last reply
              0
              • S Stuart Dootson

                You could always try designing your software so you've got a clear, single owner of resources such as allocated heap memory. Alternatively,

                • Use .NET - garbage collection means you don't need to worry about object ownership quite as much
                • Use Objective C 2.0 - it allows garbage collection, and garbage collection means you don't need to worry about object ownership quite as much
                • Use the Boehm allocator[^] - it's garbage collected, and garbage collection means you don't need to worry about object ownership quite as much
                • As other posters have suggested, use Boost's shared_ptr class.

                If you want to stick with Windows and C++, I'd go for designing your software to manage resources or Boost shared_ptrs.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                D Offline
                D Offline
                dehseth
                wrote on last edited by
                #7

                .NET won't satisfy my needs. Garbage collector slows me down. :)

                S 1 Reply Last reply
                0
                • D dehseth

                  .NET won't satisfy my needs. Garbage collector slows me down. :)

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  Not as much as pointer errors will :-)

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  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