A question about pointers

Hello everyone, I'm sorry if this question was already answered before, I just couldn't find anything on google...
So, I am gradually learning pointers and a few days ago I started getting confused about this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main () {
    int *a = new int;
    int *b = a;
    std::cout << *b << ", " << b << std::endl; //"0, 0x2324020"
    *a = 100;
    std::cout << *b << ", " << b << std::endl; //"100, 0x2324020"
    delete a;
    a = NULL;   //a is now pointing to NULL, b tough is still pointing to the old address, 0x2324020
    std::cout << *b << ", " << b << std::endl; //"0, 0x2324020" <- (?)
    std::cout << a; //"0", which is NULL
}


Of course, I know that is something that should never be done in the first place, but I don't really understand why that happens, shouldn't that give me a segmentation fault?
A segmentation fault will happen when you try to read or write a memory address that the OS thinks your process shouldn't.

The OS assigned the memory location 0x2324020 for use by your process. When you say "delete", it means "execute this object's destructor, and I don't care what happens to the memory after that".

The OS is free to leave that memory assigned to your process, and is free to leave whatever value happens to be in that memory there. Which in this case, it does (and why wouldn't it?). So when you read from it again, you're reading from memory that is assigned to your process, and thus the OS doesn't stop you with a segFault.
Oh, thanks for the clarification, I tought delete actually cleaned the memory on that address.
That actually makes sense now, thanks again :)
Topic archived. No new replies allowed.