Why are pointers not stacked?

void main(){
    int i,k;
    char* p;
    int j;
    printf("address of i is %d \naddress of k is %d \naddress of p is %p\naddress of j is %d", &i,&k,&p,&j);

}

when i tried the above code, address j is 4 units below k. But the address p is not nearby. Since the pointer is an integer variable that can hold 4 bytes of data, why is it not allocated on the stack like the other three variables?

+3
source share
7 answers

You must post the output you get. I suspect you are a bit confused because most of the addresses you print are displayed in decimal form (using% d) and pdisplayed in hexadecimal format (using% p).

+11
source

%p .

: .

+13

( Ubuntu 9.04) :

address of i is 0xbf96fe30
address of k is 0xbf96fe2c
address of p is 0xbf96fe28
address of j is 0xbf96fe24

:

void main(){
    int i,k;
    char* p;
    int j;
    printf("address of i is %p \naddress of k is %p \naddress of p is %p\naddress of j is %p\n", &i,&k,&p,&j);

}

printf() - , % p % d. , ?

+6

%d printf(), , , %p, . , , .

0

. linux, ,

, , . <

, . gcc. gcc . ,

0

, - , " ". (google, :-) , undefined:

  • printf stdio.h
  • void main
  • % d
  • % p - ptr-to-void

Correctly written is as follows:

#include <stdio.h>

int main (void)
{
    int i, k;
    char *p;
    int j;

    printf ("address of i is %p\n", (void *)&i);
    printf ("address of k is %p\n", (void *)&k);
    printf ("address of p is %p\n", (void *)&p);
    printf ("address of j is %p\n", (void *)&j);
    return 0;
}
0
source

Pointers may or may not be located on the stack, as they are also some variables. Please note that these are some variables that are not very large . If the CPU / MCU has many registers, and the compiler is well optimized, you may not see a pointer to the stack, it may well spend its whole life on registration.

-1
source

Source: https://habr.com/ru/post/1721158/


All Articles