简体   繁体   中英

why wild pointer holds zero address rather than garabge address?

I have been trying to find the size of an particular datatype like "int" without using sizeof() and found this:

#include<stdio.h>

int main() {

    int *ptr;   /*Declare a pointer*/
    printf("Size of ptr = %d\n",ptr);
    ptr++;

    printf("Size of ptr = %d\n",ptr);
    return 0;
}

This returns correct size for int. How? Isn't wild pointer suppose to contain garbage address rather than zero. And if it contains zero how is it different than NULL pointer as NULL is (void*)0?

Since ptr is uninitialised, its value is indeterminate and accessing its value gives undefined behaviour. The meaning of "undefined", somewhat ironically, is defined by C and C++ standards to mean something like "this standard doesn't constrain what happens".

Beginners often incorrectly assume this means it must contain a "garbage value" or be a "wild pointer" or "add some colourful description here" but that is simply not the case.

The meaning of "value is indeterminate" or "the behaviour on accessing the value is undefined" is that any behaviour is permitted from code that accesses the value.

Accessing the value is necessary to print it, increment it, or (in case of a pointer) dereference it (access contents of the address identified by the pointer's value).

The behaviour of code that accesses the value is undefined. Giving a printed value of zero, 42 , or a "garbage value" are all correct outcomes. Equally, however, the result could mean no output, or undesirable actions, such as reformatting a hard drive. The behaviour may even change over time if the code is executed repeatedly. Or it may be 100% repeatable (for a specific compiler, specific operating system, specific hardware, etc).

Practically, it is quite common for code with undefined behaviour to give no sign of malfunction during program testing, but to later cause some nasty and visible but unintended effect when the program is installed and executed on a customer's computer. That tends to result in grumpy customers, bug reports that the developers may be unable to replicate, and stress for developers in trying to fix the flaw.

Trying to explain why undefined behaviour results in some particular outcome (such as printing a value of zero) is therefore pointless.

the first print will have garbage or zero, depends on your compiler and previous value that was in the memory location. If it was zero, then the second print will have the size of int, because incrementing a pointer increments with the size of the pointee.

for instance:

char *x = 0;
x++; //x=1

int *y = 0;
y++; //y=4

In your case, if you got a 0 on the first print, it was the same as if you initialized it to NULL, but you can't count it to always be zero .

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