1

If the title is not clear, I have this example:

    int *a = new int[5];
    int*b = a;
    delete[] a;
    a = NULL;

Now a is NULL but b isn't. If I access b, it will return wrong values and may crash the program.

How to prevent this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tiana987642
  • 696
  • 2
  • 10
  • 28
  • 1
    That has to be `delete [] a`. – Shafik Yaghmour Mar 25 '14 at 14:30
  • 4
    Two solutions: Don't use pointers (which is what I recommend, C++ has drastically lowered the need for pointers), or use [smart](http://en.cppreference.com/w/cpp/memory/shared_ptr) [pointers](http://en.cppreference.com/w/cpp/memory/unique_ptr). – Some programmer dude Mar 25 '14 at 14:32
  • 1
    Q: how to avoid misusing pointers? A: don't use pointers. The only alternative is to write your code correctly, but this can be a challenge. – mah Mar 25 '14 at 14:35
  • 3
    `a` isn't NULL after the delete - it still has the same value, and derefencing it will cause the same problems as for `b`. – molbdnilo Mar 25 '14 at 14:36
  • Hi; are there another ways? This is from one of my project, if I change to smart pointers this will create much work :( Maybe the next project I will implement SM from the start :) – Tiana987642 Mar 25 '14 at 14:38
  • @Tiana987642 which takes more time, writing your code correctly, or debugging the problems that poorly written code leads to? I don't ask that to be snarky, but rather to point out that you're basically saying "How can I prevent writing crappy code?". You either change your code pattern to one that prevents that aspect of crappyness (such as by using smart pointers), or you don't write crappy code in the first place. – mah Mar 25 '14 at 14:45
  • 1
    Maybe we could provide a more specific solution with more details. I have a feeling you are probably not allocating `a` and calling `delete` on it in the same block of code – DaveS Mar 25 '14 at 14:48
  • I just need a workaround :( Actually we tried not to use `delete` and use a pool to keep all the objects but that can't solve the problem :( – Tiana987642 Mar 25 '14 at 14:49

1 Answers1

4

now a is NULL

Not exactly. a points to some "invalid" (delete-d) memory. If you want it to be NULL, annul it manually.


delete a;

must be

delete[] a;

How to prevent this happen?

No way, if you really need to use (raw) pointers - just be careful with the lifetime of a. It's similar with references - you should just be careful with that, too.

To avoid similar situations, smart pointers are useful. Or just use stack variables instead (where applicable, of course).

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • Okay, can you provide a workaround? Like We tried to create a pool to hold all the objects and not use `delete` but that ain't work. Still crash the program sometime! – Tiana987642 Mar 25 '14 at 14:56
  • @Tiana987642 can't you just not leave dangling pointers? It's very specific thing. – Kiril Kirov Mar 25 '14 at 15:04
  • @Tiana987642 - the "so-called workaround" is to use reference-counting pointer, which is smart (shared) pointer. Or some other type of smart pointer, depending on the needs. – Kiril Kirov Mar 25 '14 at 15:09
  • Can you please be more specific? An example? I will change to smart pointer next time, I can't do it right now with such an old, big project :( – Tiana987642 Mar 25 '14 at 15:15
  • @Tiana987642 - example of what? "reference-counting pointer" _is_ the smart shared pointer. I don't suggest anything different. And still don't understand why you just don't avoid these situations? `delete` the memory only, when you're 100% sure, it will not be used anymore and that's all. Can't this be done? – Kiril Kirov Mar 25 '14 at 15:17
  • 1
    I already mentioned we don't use `delete` and hold all the objects in a pool. This Q-A starts go around and around. I'll be back tomorrow. Thank you for your help! – Tiana987642 Mar 25 '14 at 15:21