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. C++ pointer question

C++ pointer question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
15 Posts 8 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.
  • P patnsnaudy

    If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.

    void
    foo::openWindow()
    {
       static foo* ptr = NULL;
       if (ptr)  
          ptr->closeWindow();
       ptr = this;
    
       // open this window 
       ...
    }
    

    So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a ptr = dynamic_cast<foo*>(ptr);, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advance

    T Offline
    T Offline
    TFrancis
    wrote on last edited by
    #3

    If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim

    P D 2 Replies Last reply
    0
    • T TFrancis

      If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim

      P Offline
      P Offline
      patnsnaudy
      wrote on last edited by
      #4

      I know I can do this, but I did not want to add a member for this. Thanks though! Jerry

      1 Reply Last reply
      0
      • J Joe Woodbury

        I completely fail to see the purpose of your code, but that aside, you can simply check whether the 'this' pointer is NULL. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        P Offline
        P Offline
        patnsnaudy
        wrote on last edited by
        #5

        Yeah but if you create

        foo *a = new foo();
        foo *b = new foo();
        
        a.openWindow();
        b.openWindow(); // This should close a.
        delete b;       // ptr in openWindow() still points 
                        //  to the space b used to occupy.
        a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
        

        I know I could create a member and set it to this in openWindow and NULL in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it.

        C R 2 Replies Last reply
        0
        • P patnsnaudy

          Yeah but if you create

          foo *a = new foo();
          foo *b = new foo();
          
          a.openWindow();
          b.openWindow(); // This should close a.
          delete b;       // ptr in openWindow() still points 
                          //  to the space b used to occupy.
          a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
          

          I know I could create a member and set it to this in openWindow and NULL in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          patnsnaudy wrote: but the powers that be don't like try-catch, so I'm not allowed to use it. Not being allowed to use try..catch is wrong, but so is a design that throws exceptions based on bad coding, so on aggregate, I'm with the 'powers that be'. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          1 Reply Last reply
          0
          • P patnsnaudy

            If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.

            void
            foo::openWindow()
            {
               static foo* ptr = NULL;
               if (ptr)  
                  ptr->closeWindow();
               ptr = this;
            
               // open this window 
               ...
            }
            

            So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a ptr = dynamic_cast<foo*>(ptr);, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advance

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #7

            Add an operator HWND to the class that returns the window handle and call ::IsWindow. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

            P 1 Reply Last reply
            0
            • C Christian Graus

              Add an operator HWND to the class that returns the window handle and call ::IsWindow. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

              P Offline
              P Offline
              patnsnaudy
              wrote on last edited by
              #8

              There is already a function to return HWND I think, if this is derived from the window class (I'll have to check tomorrow at work). ::IsWindow is a great suggestion! I was looking for a magical function that would look at a piece of memory and tell me if it had been deleted, but IsWindow should work. Thanks

              P 1 Reply Last reply
              0
              • P patnsnaudy

                There is already a function to return HWND I think, if this is derived from the window class (I'll have to check tomorrow at work). ::IsWindow is a great suggestion! I was looking for a magical function that would look at a piece of memory and tell me if it had been deleted, but IsWindow should work. Thanks

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #9

                it will not work if the pointer to the dialog is deleteed.


                MSN Messenger. prakashnadar@msn.com

                P 1 Reply Last reply
                0
                • P patnsnaudy

                  If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.

                  void
                  foo::openWindow()
                  {
                     static foo* ptr = NULL;
                     if (ptr)  
                        ptr->closeWindow();
                     ptr = this;
                  
                     // open this window 
                     ...
                  }
                  

                  So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a ptr = dynamic_cast<foo*>(ptr);, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advance

                  R Offline
                  R Offline
                  Ryan Binns
                  wrote on last edited by
                  #10

                  To determine if a pointer points to readable memory, use the IsBadReadPtr() function. If it returns FALSE, then the pointer cannot be read. Hope this helps,

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  1 Reply Last reply
                  0
                  • P patnsnaudy

                    Yeah but if you create

                    foo *a = new foo();
                    foo *b = new foo();
                    
                    a.openWindow();
                    b.openWindow(); // This should close a.
                    delete b;       // ptr in openWindow() still points 
                                    //  to the space b used to occupy.
                    a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
                    

                    I know I could create a member and set it to this in openWindow and NULL in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it.

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #11

                    It's good coding hygiene to set a deleted pointer to NULL. This will help prevent run-time errors and could make your code simpler. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

                    1 Reply Last reply
                    0
                    • T TFrancis

                      If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      TFrancis wrote: If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. How about:

                      IsBadReadPtr()
                      IsBadCodePtr()
                      IsBadStringPtr()
                      IsBadWritePtr()


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      1 Reply Last reply
                      0
                      • P Prakash Nadar

                        it will not work if the pointer to the dialog is deleteed.


                        MSN Messenger. prakashnadar@msn.com

                        P Offline
                        P Offline
                        patnsnaudy
                        wrote on last edited by
                        #13

                        By it will not work does that mean that it will crash if the pointer has been deleted?

                        P 1 Reply Last reply
                        0
                        • P patnsnaudy

                          By it will not work does that mean that it will crash if the pointer has been deleted?

                          P Offline
                          P Offline
                          Prakash Nadar
                          wrote on last edited by
                          #14

                          yes,it will very unpredictable, it may crash in some cases and it will work in some cases. but then working with deleted pointer is very bad idea.


                          MSN Messenger. prakashnadar@msn.com

                          1 Reply Last reply
                          0
                          • P patnsnaudy

                            If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.

                            void
                            foo::openWindow()
                            {
                               static foo* ptr = NULL;
                               if (ptr)  
                                  ptr->closeWindow();
                               ptr = this;
                            
                               // open this window 
                               ...
                            }
                            

                            So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a ptr = dynamic_cast<foo*>(ptr);, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advance

                            P Offline
                            P Offline
                            patnsnaudy
                            wrote on last edited by
                            #15

                            I just created a static pointer to the window and set it to this in the openWindow function and NULL in the dtor. Thanks

                            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