2
#include <stdio.h>


int *pPointer;

void SomeFunction()
{
    int nNumber;
    nNumber = 25;    

    // make pPointer point to nNumber:

    pPointer = &nNumber;
}

void main()
{
    SomeFunction(); // make pPointer point to something

    cout<< "Value of *pPointer: "<< *pPointer <<endl;
}

I have been told that using pointers like this is dangerous, could anyone please explain why it is dangerous and what would be the 'safe' way to write that piece of code? will 25 always be printed out to the screen in that way? if not then why?

yair
  • 95
  • 2
  • 9
  • 1
    Have a look at this wonderful answer from Eric Lippert on the subject http://stackoverflow.com/a/6445794/1316346 – Kevin DiTraglia Nov 11 '13 at 12:01
  • What the "safe way" is depends on what exactly you're trying to accomplish. – interjay Nov 11 '13 at 12:01
  • In simple terms, it's unsafe because after `SomeFunction()` is done executing `pPointer` points to a variable that "doesn't exist" anymore. That memory location might still contain the value it used to have but it might just as well contain anything else. – user2802841 Nov 11 '13 at 12:06
  • @user2802841 there's nothing potential about it. – WhozCraig Nov 11 '13 at 12:06
  • 1
    I Would not use word "dangerous", because it implies it may be valid if you know what you are doing. But this is just wrong. What is dangerous is the *ability* to get pointer to stack variable, and this is using this dangerous feature wrong. – hyde Nov 11 '13 at 12:07
  • @WhozCraig edited to make it clearer. – user2802841 Nov 11 '13 at 12:13

3 Answers3

6

Using a pointer to local variable outside of the scope of the variable is always dangerous. It invokes undefined behavior.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • +1 Amplifying the *using*. Once that function exits that pointer is indeterminate, even using it by eval is UB, much less by de-ref. – WhozCraig Nov 11 '13 at 12:04
3
  • Unsafe, because its value may be overwritten

  • Safe way: just

    int SomeFunction()
    {
        int nNumber;
        nNumber = 25;    
        return nNumber;
    }
    

    Will do fine. If your return value is large, return value optimization will save your life anyway.

  • 25 printed? Implementation specified. Most likely not because you are in a new stack frame when the function returned.

user877329
  • 6,717
  • 8
  • 46
  • 88
  • 1
    Most likely 25 *will* be printed, because there is nothing in the code which would clobber the bytes in stack. Sometimes an interrupt will happen and overwrite the value between value going out of scope and getting the value via the pointer for printing, so sometimes, rarely, something else will be printed. Not sure if aggressive optimization settings might change this, probably. – hyde Nov 11 '13 at 12:30
  • 1
    @hyde true. But if main called another function later, then it would refer to another value. – user877329 Nov 11 '13 at 12:40
0

You are talkin about wild pointers which termed to be dangerous to use because a pointer, is a memory address, and in modern computers memory is protected (only allowed to be accessed by the program that owns it) an attempt to read or write to memorry address 0 or NULL will cause your program to crash with a memory violation error.

he best way to protect againsed this to initalise a pointer as soon as it is created.

anouther way, is to test the pointer before using it.

if (pointer == 0) { 
   cout << "WARNING... cant use this pointer"; 
} 
else { 
   // it is okay to use this pointer 
} 
anbu selvan
  • 725
  • 3
  • 13
  • 41