简体   繁体   中英

(nil) value returned by int pointer in c

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??

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.

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.

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.

调用堆栈调用堆栈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.

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

See: http://ideone.com/wwiy8F

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