Reputation: 1114
I'm testing this piece of code, and the result is that the program crashes when reaches the << op
in the first cout
(because it prints out op =
and then stops).
char *lo = 0, *op = 0, *ro = 0;
cout << "op = " << op << endl;
cout << "*op = " << *op << endl;
Now my question is: why this happens?
I know, pointer causes me a lot of problems..
Upvotes: 0
Views: 223
Reputation: 5239
Going by your title "Pointer generate a crash also if it is initialized".
The pointer you created is not initialised to any memory location. When you initialise a pointer to 0
it means you are initialising it to NULL.
A pointer is supposed to hold a memory address (before you plan to use *
on it) or NULL(to keep it from causing problems by modifying random memory location until you assign some memory location to it, before dereferencing. It also indicates that the pointer is currently inactive).
char *lo = 0, *op = 0, *ro = 0;
your pointers are currently NULL, not pointing to any memory location.
char a = 'A';
char * lo = &a; / char* lo = new char[10]; etc
Dereferencing a uninitialised or NULL pointer is undefined behaviour.
Upvotes: 1
Reputation: 183888
Your programme already crashes on the
cout << "op = " << op << endl;
line because there is an overload of the <<
operator for char*
that interprets them as a pointer to the initial element of a 0-terminated character array and prints the characters before the next 0-byte out, thus needing to dereference the pointer. But you initialised op
to be a null pointer, hence dereferencing it invokes undefined behaviour, often resulting in a crash (segmentation fault).
Casting op
to a different pointer type, e.g. a void*
cout << "op = " << static_cast<void*>(op) << endl;
would make that line work and print an implementation-defined representation of a null pointer.
You would then still have undefined behaviour (and most likely a crash) on the next line, where you explicitly dereference op
.
Upvotes: 1
Reputation: 126442
Because here:
cout << "*op = " << *op << endl;
// ^^^
// op is a NULL pointer!
you are dereferencing a pointer that does not point to any object, and that is undefined behavior.
So although the pointer is initialized, it is not initialized with the address of an existing object, and therefore you cannot dereference it meaningfully. Paragraph 5.3.1/1 of the C++11 Standard specifies:
The unary
*
operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points.
The Standard does not specify what the result should be when the expression is not pointing to any object: hence, it is undefined behavior.
Upvotes: 6
Reputation: 385174
Your question may be summarised thus:
I was taught that dereferencing uninitialized pointers is bad, and have come to the conclusion that this means all initialized pointers must be good. Am I right?
The answer is a resounding no.
Uninitialized pointers aren't the problem.
The problem is with pointers that don't point to some decent storage in memory. This is usually the case for uninitialized pointers, but it's also the case when you deliberately assigned the pointer some useless value, like here.
For the purpose of pointing to a string, 0
is a useless value, because it indicates "this pointer does not point anywhere". Then attempting to dereference it is not okay.
It's not entirely clear what different behaviour you expected from this code.
Upvotes: 4
Reputation: 409176
It's because you initialize the pointers to point to address zero, which is the same a NULL
.
Upvotes: 0