简体   繁体   中英

What happen with the pointer members of a class if I declare a pointer?

I have written the following code:

#include <iostream>

using namespace std;

class Node {
public:
    int id;
    Node* n;
};

int main()
{
    Node* node;
    cout << node->n;
    return 0;
}

In this case I declared a pointer (it's uninitializated) but in the output I obtain an address given by "n". if I don't initializate the pointer, why does the code show me a reference?.

It is a wild pointer. VC compilers assign to those pointers the value 0xcdcdcdcd which may point to the last memory in the RAM ( not sure of that) . You should not dereference this kind of pointers because the compiler will throw a violation memory access.If you doesn't want to assign a object of type Node to the pointer it is strongly recommended that you assign the pointer with NULL value.In this case you will always be able to check in the code if the pointer points to a real object or is not assigned by checking his address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM