2

in the following code I found that same pointer instruction crash the application in a situation while not in other situation.

#include <iostream>
using namespace std;

int main()
{
    int *p;
    *p = 50;                    //this instruction causes the crash
    int* q = new int;
    *q = 50;                    //this instruction executes ok
    cout << "p:" << p << endl;
    cout << "q:" << q << endl;
    return 0;
}

I want to know why this is the case?

  • 5
    `p` is not initialized. Who knows where it points? It is undefined behavior. – Aluan Haddad Nov 02 '17 at 07:47
  • 2
    Possible duplicate of [Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?](https://stackoverflow.com/questions/4285895/where-exactly-does-c-standard-say-dereferencing-an-uninitialized-pointer-is-un) –  Nov 02 '17 at 07:51
  • 2
    Not really related but `int* q = new int;` dynamically allocates memory, so the fact it initializes memory is not the only reason one should have before deciding to use `new` :-) – George Nov 02 '17 at 07:51
  • Thanks. How can I initialize the p so that the *p = 50; executes correctly ? – Muhammad Nada Nov 02 '17 at 07:52
  • 3
    By giving it an initalized int to point to `int a = 0; int* p = &a; *p = 50;` – George Nov 02 '17 at 07:53
  • or by doing what you are already doing, `= new int` (just beware: any `new` needs an eventual `delete`) – kmdreko Nov 02 '17 at 07:53

3 Answers3

4

The first pointer is uninitialized. It doesn't point to a memory location that has an int value. So when you deref it on the next line, you get a crash.

The second pointer is initialized to an int that has an actual space in memory. So when you deref it, it finds the value held in that space.

Carlos
  • 5,991
  • 6
  • 43
  • 82
2
int *p;

This pointer points to nowhere i.e not at any valid address of the process. That's why it crashes

int* q = new int;

Points to a valid address returned by new int, hence worked

DeepakKg
  • 349
  • 2
  • 4
1

I see you need some links to documentation:

To wrap it up:

  • You can use the indirection operator (*) to return the object the pointer points to (dereference the pointer).
  • You can only access (read or modify) an object via a pointer, when the pointer actually points to an object (which by default they don't).
  • You can assign the address of an object to the pointer to let the pointer point at it.
  • You can use the address-of operator (&) to acquire the address of an object for assigning it to a pointer.
  • You can use the new operator to create a new object and return the address to it for assigning it to a pointer.
  • You must use delete to eventually destroy objects created using new.
  • When you use pointers, you alone are responsible for the validity of the objects your pointers point to. Don't expect the compiler to warn you when objects are leaked or accessed beyond the end of their lifetime. If you do it wrong, you might observe undefined behavior.
  • Smart pointers can help to keep track of object ownership and take care of proper destruction.

For further reading:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187