简体   繁体   English

为什么指针不在堆栈上?

[英]Why are pointers not located on a stack?

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, the address of j is 4 units below k. 当我尝试上面的代码时,j的地址比k低4个单位。 But the address of p is no where near. 但是p的地址不在附近。 Since a pointer is an integer variable that could store 4 bytes of data, why isn't it allocated on the stack like the other three variables? 由于指针是一个可以存储4个字节数据的整数变量,为什么不像其他三个变量一样在堆栈上分配?

Use %p to print all the addresses. 使用%p打印所有地址。

Spoiler: It is on the stack. 扰流板:它在栈上。

You should post the output you're getting. 你应该发布你得到的输出。 I suspect that you're getting a bit confused because most of the addresses you're printing are being displayed in decimal (using %d) while p is being displayed in hex (using %p). 我怀疑你有点困惑,因为你正在打印的大多数地址都是以十进制显示(使用%d)而p是以十六进制显示(使用%p)。

I just tried your code on my computer (running Ubuntu 9.04) and got the following: 我刚刚在我的计算机上运行了你的代码(运行Ubuntu 9.04)并获得了以下内容:

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

after changing the code somewhat: 稍微更改代码后:

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);

}

Since all you're printf() are addresses you should use %p instead of %d. 由于你所有的printf()都是地址,你应该使用%p而不是%d。 Maybe you misinterpreted your results? 也许你误解了你的结果?

I can't help but tell you that your program is one of those that "should be indented six feet and covered with dirt." 我不能不告诉你,你的程序是“应该缩进六英尺并且被泥土覆盖”的程序之一。 (google for who said it when and why:-) It is a prime example of sloppyness for the following reasons, most of which are causing undefined behavior: (google为谁说的时间和原因:-)由于以下原因,这是草率的一个主要例子,其中大多数都导致了未定义的行为:

  • Uses printf without including stdio.h 使用printf而不包括stdio.h
  • Uses void main 使用void main
  • Uses %d for addresses 使用%d表示地址
  • Uses %p with something other than a ptr-to-void 使用%p而不是ptr-to-void

Properly written it looks like this: 正确编写它看起来像这样:

#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;
}

When you use %d printf() specifier you get an address printed as decimal number and when you use %p specifier you get it printed as hexadecimal number. 当您使用%d printf()说明符时,您将获得一个打印为十进制数的地址,当您使用%p说明符时,您将其打印为十六进制数。 It just happened that the hexadecimal number contains no letters and you misinterpret it. 恰好十六进制数字不包含任何字母而你误解了它。

In the end a pointer is a variable.As far as the linux architecture is concerned,it is very clear that 最后一个指针是一个变量。就linux架构而言,很明显

All those variables whose memory is localized to a function will remain on the stack.< 所有那些内存本地化为函数的变量都将保留在堆栈中

There is no reason why for the above case the pointers should not be on the stack.Its a case of misinterpreting the ouput and ignoring the warning from gcc.Its a case with beginners that they ignore gcc warnings as harmless.There are cases when they are equivalent to a compiler error 没有理由为什么对于上面的情况,指针不应该在堆栈上。这是一个错误解释输出并忽略来自gcc的警告的情况。这是一个初学者的情况,他们忽略gcc警告是无害的。有些情况下他们等同于编译器错误

Pointers may or may not be located on the stack as they are also some sort of variables. 指针可能位于堆栈上,也可能不位于堆栈中,因为它们也是某种变量。 Note that they are some not very big variables. 请注意,它们是一些不是很大的变量。 If the CPU/MCU has lots of registers and the compiler optimizes well, you may not see a pointer on the stack, it may very well spend its whole lifetime in registers. 如果CPU / MCU有很多寄存器并且编译器优化得很好,你可能看不到堆栈上的指针,它可能会在寄存器中花费整个生命周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM