2

What is wrong in that: I just wanted to pointers to int and give that ints value of 0.

    int* p;int* q;

*p = 0; *q = 0;
cout<<"p = "<<*p<<" q = "<<*q<<endl;

This is annoying

WORKS:

int* p;
   *p = 0;

   cout<<*p<<endl;

CRASHES:

     int* p;
   int* q;
   *p = 0;
   *q = 0;

   cout<<*p<<endl;
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • See: http://en.wikipedia.org/wiki/Dangling_pointer#Cause_of_wild_pointers – Paul R Aug 30 '12 at 12:50
  • Duplicate (same user): [How to get size of a dynamic allocated array of ints in C++](http://stackoverflow.com/questions/12196712/how-to-get-size-of-a-dynamic-allocated-array-of-ints-in-c) – Paul R Aug 30 '12 at 12:50
  • 3
    @PaulR That is **not** the same question!!! – Luchian Grigore Aug 30 '12 at 12:52
  • @Luchian: it has the exact same code with wild pointers in it and someone has already explained the problem in a comment. OP has ignored the comments and asked (part of) the same question again, hence it's a duplicate. – Paul R Aug 30 '12 at 12:53
  • 1
    @PaulR, if the user uses the same code to ask _different things_, I believe it is by no means a duplicate _question_ – SingerOfTheFall Aug 30 '12 at 12:54
  • @Singer: even if they have already had their problem diagnosed and have chosen to ignore it ? – Paul R Aug 30 '12 at 12:55
  • @PaulR that question was wrongfuly edited (I reverted it back). Sorry, I didn't see that edit, but originally it did ask a completely different thing. – Luchian Grigore Aug 30 '12 at 12:55
  • @Luchian: OK - I guess this is no longer a duplicate then. – Paul R Aug 30 '12 at 12:56

3 Answers3

14
WORKS:

int* p;
*p = 0;

Nope! It appears to work, but is in fact undefined behavior.

Declaring int* whatever; leaves you with an uninitialized pointer. You can't dereference it.

To initialize a pointer & set the value it points to to 0 (in your case):

int* p = new int(0);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

To use a pointer, that pointer has to point to something. So there are two steps: create the pointer, and create the thing it points to.

int *p, *q;    // create two pointers
int a;         // create something to point to
p = &a;        // make p point to a
*p = 0;        // set a to 0
q = new int;   // make q point to allocated memory
*q = 0;        // set allocated memory to 0
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • And you can also combine the two steps on one line as Luchian Grigore shows. – Derek Aug 30 '12 at 13:10
  • @Derek - yes, of course. I was illustrating the two steps, and combining them would muddle the lesson. – Pete Becker Aug 30 '12 at 13:12
  • 1
    +1 for showing you can create a pointer to something on the stack (a), not just the heap (q). – Nathan Aug 30 '12 at 13:19
  • @PeteBecker - I agree. I wasn't trying to criticize your answer. Just pointing it out. – Derek Aug 30 '12 at 13:19
  • 1
    I feel obligated to point out that any use of `new` should have a corresponding `delete` (or some way to release the memory). – taz Aug 30 '12 at 13:26
  • @taz - Yes, certainly. This is a code snippet, intentionally incomplete. – Pete Becker Aug 30 '12 at 13:29
  • @PeteBecker I know; not trying to be overly pedantic. I do things like that so in the future if someone is just learning C++ all the necessary information is together and they won't start writing code with memory leaks. – taz Aug 30 '12 at 14:11
2

You didn't allocate any memory for your pointers, so you're getting Undefined behavior. Basically that means that anything could happen (including the possibility that it will work, too).

Use int something = new int(<initialization_value>); to initialize the pointer.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105