0

I have one question on static variable. Suppose i have a static pointer in class and declare 2 object of that class.By using first object, i deleted that static pointer.That pointer now become dangling pointer and suppose second object referring to that static pointer also.
Now what will happen and how to avoid such condition??

RAW
  • 27
  • 5
  • 2
    program will crash... and you avoid it by not doing it :D (or setting it to 0 after deleting it so that the other guy can check if it is still there). – jsantander May 24 '14 at 09:23
  • Couldn't you count how many times was the pointer referenced? The destructor will substract 1 from the reference counts of the pointer, when the reference count gets to 0, you delete the pointer. – prkist May 24 '14 at 09:24
  • By "second object referring to that static pointer", do you mean that some of your code dereferences that pointer? That would have undefined behaviour. The number of objects has no effect on static members. – eerorika May 24 '14 at 09:25
  • 2
    It's not entirely clear what underlying problem you're trying to solve, but it sounds like you should read [What is a smart pointer and when should I use one?](http://stackoverflow.com/q/106508/78845). – Johnsyweb May 24 '14 at 09:29
  • 1
    That's why you should use a `std::shared_ptr` – vsoftco May 24 '14 at 09:33
  • I would follow @vsoftco's advice! – πάντα ῥεῖ May 24 '14 at 09:44
  • 1
    In addition to using a `shared_ptr`, I would make the pointer a non-static member. Reference counting only works when `shared_ptr` is copied but there is only one copy of a static member between the instances. – eerorika May 24 '14 at 09:48

2 Answers2

0

Unless you have a really good reason don't make it static. If you want to make something static then try avoid using a pointer.

* I did say place it on the stack to simplify the discussion but this was wrong for static data *

Of course you can always set a pointer to NULL or nullptr after deletion and check if the pointer is valid before using it.

What you have described is a common problem that occurs in software when pointers are not managed properly.

EDIT:

in response to @πάντα ῥεῖ comment, let me give example:

// not on the stack, read link in comments for more detail.
static MyClass my_class;

which is simpler to manage than:

static MyClass* m_class;

The semantics of what @πάντα ῥεῖ says is correct, in that in reality its not on the stack, although it will behave in a similar way to being on the stack. static data has a longer life time than data on the stack.

ANOTHER EDIT:

I gave a simplified answer to deal purely with avoiding the need to manage a static pointer by not using a pointer in the first place.

I'm more than happy to let @πάντα ῥεῖ go into the low level details of what actually happens if the original person asking the question things this adds any value.

  • _'If you want to make something static then place it on the stack'_ This is blatantly wrong! Stack allocated objects aren't static in any form. – πάντα ῥεῖ May 24 '14 at 09:43
  • static int im_on_the_stack; –  May 24 '14 at 09:46
  • `static int im_on_the_stack;` No, you're wrong. Saying it doesn't improve it. It's not placed on the stack, no matter where this line appears in the code. – πάντα ῥεῖ May 24 '14 at 09:48
  • Please clarify what you think the problem seems to be as I don't know what you consider to be a problem, ideally without shouting :) –  May 24 '14 at 09:50
  • Stack allocated objects are living between function calls and in their functions scope. Static instances are different, and the memory is allocated in the programs data section. I did't shout, but used proper code formatting in comments BTW. – πάντα ῥεῖ May 24 '14 at 09:52
  • Ah yes I see your point, and yes I did keep the description simplified, but you are right I'm technically misleading. But the behavior will be much like a stack based variable. –  May 24 '14 at 09:56
  • _'But the behavior will be much like a stack based variable.'_ Again wrong, the behavior is _very different_ in various aspects. – πάντα ῥεῖ May 24 '14 at 09:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54336/discussion-between-curg-and--). –  May 24 '14 at 10:00
  • This link already exists on stackoverflow to provide the discussion on static data storage. http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c –  May 24 '14 at 10:12
  • Yes BSS section is the other option. But certainly not on the stack. – πάντα ῥεῖ May 24 '14 at 10:14
  • Indeed, but I didn't think it appropriate for this discussion to go into the low level detail... –  May 24 '14 at 10:19
  • It's not about _'low level detail'_, but using the correct terminology and to avoid producing any further misconceptions, especially when you're going to answer a beginner. – πάντα ῥεῖ May 24 '14 at 10:21
0

if you are using c++11 , use std::unique_ptr.
if not , use boost

alirakiyan
  • 396
  • 3
  • 15