-1

when we declare a pointer it points to some random location or address in memory unless we explicitly assign a particular value(address of any variable) to it.

Here is code:

int *p;

printf("int is %p\n",p);

float *j;

printf("float is %p\n",j);

double *dp;

printf("double is %p\n",dp);

char   *ch ;

printf("char is %p\n",ch);

j=(float *)p;

printf("cast int to float %p\n",j);

output:

int is (nil)

float is 0x400460

double is 0x7fff9f0f1a20

char is (nil)

cast int to float (nil)

Rather than printing the random location it prints (nil)

what is (nil) here ?? I don't understand the behaviour of pointers here??

4 Answers 4

2

Uninitialized pointer variables don't point to random address. Where they point is undefined.

In practice, they have the value what's left in the stack, which you can't know for sure. In your example, those several pointers happens to have 0 values, so they happen to be null pointer, and they print as nil.

Don't rely on such behavior, ever.

Sign up to request clarification or add additional context in comments.

2 Comments

so it means the value stored in uninitialised pointer could be 0 or some random value left in stack
@user4255847 They have undefined values, in particular, they are not random, let me say that again. Randomness have certain requirements.
1

If by the "gnu" tag you mean that you're using glibc, then the reason is that the printf implementation in glibc will print "(nil)" when encountering a NULL pointer.

In other words, several of your pointers happen to have the value NULL (0), because that's what happened to be on the stack at that particular location.

2 Comments

I wouldn't call it "protection". Printing a pointer value with %p doesn't dereference the pointer; the output is implementation-defined, but it's perfectly safe.
@KeithThompson: Fair enough. FWIW, IIRC glibc does have null ptr protection for cases where it has to dereference the pointer.
0

Elaborating on what @yu hao has said:

Whenever a subroutine is invoked, a stack frame is allocated to the subroutine. This frame exists until return statement is encountered.

A subroutine frequently needs memory space for storing the values of local variables, the variables that are known only within the active subroutine and do not retain values after it returns. For doing so , the compiler allocate space for this use by simply moving the top of the stack by enough to provide the space. This is very fast when compared to dynamic memory allocation, which uses the heap space. Note that each separate activation of a subroutine gets its own separate space in the stack for locals known as Stack Frames.

The main reason for doing this is to keep track of the point to which each active subroutine should return control when it finishes executing. An active subroutine is one that has been called but is yet to complete execution after which control should be handed back to the point of call. Such activations of subroutines may be nested to any level (recursive as a special case), hence the stack structure. If, for example, a subroutine DrawSquare calls a subroutine DrawLine from four different places, DrawLine must know where to return when its execution completes. To accomplish this, the address following the call instruction, the return address, is also pushed onto the call stack with each call.

Call Stack call stack 2

Coming back to your question, during its execution , the function can make changes to its stack frame and When a function 'returns', its frame is 'popped' from stack.
But the contents of stack remains unchanged in this process. Only the stack pointer gets modified to point to previous frame. So when a new subroutine gets called, new frame is allocated on top of previous one,and if the subroutine has uninitialized variables, they will print value that is stored in the memory allocated to them . Which will depend on the state of stack at that point of time.

Comments

-1

All of your pointers are nil/ undefiend or the are just random values from the stack!

See: http://ideone.com/wwiy8F

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.