Is the memory address positive or negative in c?

in c, I tried to print the address of the variable and the address of some function. I got one negative value, the other a positive value. My question is: why does C not represent all negative or all positive values?

Here is my code:

int foo() { return 0; } int main() { int a; printf("%d\n",&a); printf("%d\n",foo); return 0; } 

Here is the result:

 -1075908992 134513684 
+7
c memory-address
source share
10 answers

Memory addresses should not be interpreted as signed integers in this way. The sign of the number depends on the oldest bit set (provided that two additional representations are used, which is used by the vast majority of systems currently in use), so the memory address above 0x80000000 in a 32-bit system will be negative, and the memory address below 0x80000000 will be positive. There is no real meaning to this.

You must print the memory addresses using the %p modifier; Alternatively, some people use %08x to print memory addresses (or %016llx on 64-bit systems). It will always be output as an unsigned integer in hexadecimal, which is much more useful than a signed decimal integer.

 int a; printf("%p\n", &a); 
+29
source share

To be the most standard, perhaps pass "% p" in the format string to printf() and pass the argument void* .

 printf("%p\n", (void*)&a); printf("%p\n", (void*)foo); 

Quote from the draft standard (n1401.pdf)

     7.20.6.1 The fprintf function

 8 The conversion specifiers and their meanings are:

 p The argument shall be a pointer to void.  The value of the pointer
         is converted to a sequence of printing characters, in an
         implementation-defined manner.
+7
source share

You must pass a pointer formatter:

 printf("%p\n",foo); 
+6
source share

You print in decimal form. You probably want to print the memory in hexadecimal format (using "% p") (most memory addresses are displayed in hexadecimal by convention).

+4
source share

In addition, the pointer is unsigned, but when converted to a signed integer, it can be positive or negative.

By the way, passing a pointer to printf with a format variable designed to print the whole causes undefined behavior. You should always explicitly specify the correct type in order to get undefined or implementation-specific behavior (depending on the cast).

+3
source share

Pointers are not integers, which is more like unsigned integers. However, you print them as if they were whole integer characters. On a 32-bit system, there are several things that can be 32 bits long: int , unsigned int , float and pointers. You cannot say at all that it is just by looking at them, but they all mean different things. (As an experiment, you can pass various integers and print them as a float and vice versa: this is bad practice in general, but it can show you that different data types mean different things.)

Functions that take a variable number of arguments ("variational" functions) make this difficult. In particular, they do not check argument types. You can pass any arguments you like printf() and similar functions, but getting them to agree with the format specifier is your problem. This means that the function has no way to get the correct type (as if you passed the float a function like int foo(int) ).

Passing the wrong argument types leads to undefined behavior and is more likely to cause problems if you pass arguments of the wrong size. On most 64-bit systems, pointers have 64 bits and int have 32 bits.

So you need to use the right thing in the format string. printf("%p", &a); will print address a as the pointer you want. The standard requires something like printf("%p", (void *)&a); , but as a practical question, unnecessary on any computer that you ever come across.

+3
source share

You can also use printf("%p", &a) . %p is a pointer format specifier to display the pointer value in a specific way. In practice, it is almost the same as% x, but will handle the case where the pointer is larger than int.

+1
source share

Use the following code:

 printf("%u",&a); 
0
source share

printf is not a magical interpretation tool. Everything that you do, if you give it, "% d" shows you what the bit pattern in this variable looks like as an integer.

As to why some pointers are negative and some positive, all of this means that some of them have a high order bit and some do not. What addresses are provided to objects is really the business of your operating system (usually with some hardware support). The programming language does not say this.

If I were you, I would not print the addresses in this way. If you really feel the need to look at them, use "% x" instead of "% d". This will print them in hexadecimal, which will greatly simplify the determination of which bits are on and which are not. The general information that you will take care of addresses is if they are 0 or not, and if they correspond to a different address value.

0
source share

Besides using %p , you can also use for uintptr_t to get a single integer.

Use the PRIuPTR (or PRIXPTR for hex) form <inttypes.h> to get the correct format specifier:

 printf("%" PRIuPTR "\n", (uintptr_t)(void *)&foo); 
0
source share

All Articles