0
int main()
{
    int* nir = new int; // creating dynamic memory
    *nir = 7; // assigning value
    cout << *nir << endl;
    delete nir; // deleting 
    nir = 0; // **is this line for assigning the address nir=0? 0 is also part of memory right? Why didn't we put NULL?
    *nir = 8; // **can i do like this and change the value, so that the output can be 8 now?
    cout << *nir << endl;
    delete nir;
    nir = 0;
    return 0;
}

This is the code that I created to understand new. But, even though it was compiled fine by Code::Blocks, during the runtime, it crashes. I have two questions, which I have already mentioned in comment part.

nir = 0;

is this line for assigning the address nir = 0? 0 is also part of memory right? Why didn't we put nir = NULL?

*nir = 8;

can I do like this and change the value, so that the output can be 8 now? After all, I have already deleted the *nir value.

David G
  • 94,763
  • 41
  • 167
  • 253
Rockink
  • 180
  • 4
  • 17
  • `nir = 0` makes `nir` point to nothing.. It's equivalent to setting it to `null`. After that, you can't do `*nir = 8` because `nir` has no memory allocated to hold 8. You deleted it using `delete`. – Brandon Mar 30 '14 at 01:11
  • `nir = 0` and `nir = NULL` are absolutely the same thing. – Shoe Mar 30 '14 at 01:16
  • @Jefffrey depending on what `NULL` has been defined as. `NULL`doesn't actually exist in C++. [It's a C thing, in a C library header](http://stackoverflow.com/a/12023528/85371) – sehe Mar 30 '14 at 11:03
  • ok, apparently, parts of the contents of these headers are in fact specified in the C++ standard (TIL; haven't checked). In that case, it still remains to be seen what `NULL` means in your context :) – sehe Mar 30 '14 at 12:43

4 Answers4

0

nir=0;

This sets the pointer to NULL. 0 and NULL are the same in this context.

*nir=8

This is wrong as nir in not a valid pointer. It's no suprise that it crashes!

cout<<*nir<<endl;

This is also wrong as nir is invalid pointer. You cannot read or write.

delete nir;

This is harmless, as deleting a NULL pointer is safe (it does nothing).

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • since I have already used int* nir=new int, does that mean that to yield the output 8, I have to allocate another dynamic memory with other name? – Rockink Mar 30 '14 at 01:15
  • @Rockink I'm not sure what you mean. You have to allocate again, but you can use the same pointer. You can reuse the pointer again. – Neil Kirk Mar 30 '14 at 01:17
0

This code snippet is wrong

nir=0;     //**is this line for assigning the address nir=0? 0 is also part of memory right? Why didn't we put NULL?
*nir=8;    //**can i do like this and change the value, so that the output can be 8 now?
cout<<*nir<<endl;
delete nir;
nir=0;

You did not allocate memory and are trying to write to address 0.

*nir=8;    //**can i do like this and change the value, so that the output can be 8 now?

Usually the program will crash.

As for the line

nir = 0;

then it is equivalent to

nir = NULL;

In C++ NULL usualy defined as 0 or ( long )0 and so on.

According to the C++ Standard

1 A null pointer constant is an integer literal (2.14.2) with value zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You tagged c++ so I recommend using nullptr instead of 0/NULL

nir = nullptr;

The problem

The literal 0 (which is essentially of type int) also serves as a null pointer literal in C++. This kludge results in ambiguity and bugs.

Solution

Use the nullptr keyword instead of 0 to indicate a null pointer value

source

0

A short breakdown of errors you purposely committed:

int main()
{
    int* nir = new int; // allocating dynamic memory
    *nir = 7; // assigning value
    cout << *nir << endl;
    delete nir; // deleting 
    nir = 0; // **is this line for assigning the address nir=0?
      // 0 is also part of memory right? Why didn't we put NULL?

The previous comment is wrong. For historical reasons, assigning a literal 0 to a pointer variable means setting it to a null pointer constant. This is not guaranteed to be 0 [!!!]. NULL and nullptr_t are more modern...

    *nir = 8; // **can i do like this and change the value,
      // so that the output can be 8 now?
    cout << *nir << endl;

On some systems you can do that. But your computing platform is now irretrievably corrupted. Modern systems catch the culprit and raise General Protection Fault, which only kills your program.

    delete nir;

Because programmers are keen to avoid useless work, the above (delete NULL) is defined as a no-op

    nir = 0;
    return 0;

The previous two lines are useless, as nir is never used again and main per standard returns 0 unless it explicitly doesn't, quite in contrast to any other function.

}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118