如何知道堆栈正在向上/向下增长? [英] How to know that stack is growing upward/downward?

查看:104
本文介绍了如何知道堆栈正在向上/向下增长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道C程序中的堆栈是否在向上增长



向下。所以我写了一些代码来看看。请指导我这个代码是否正确告诉我这个代码是否正确?


#include< stdio .h>

int main(无效)

{

int * buf1 = malloc(10 * sizeof(int));

int * buf2 = malloc(10 * sizeof(int));


printf(buf1?buf2?" Downward":" Backward");


返回0;

}


谢谢......

Hi all,

I wanted to know whether the stack in a C program is growing upwards
or
downwards.So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ?

#include <stdio.h>
int main(void)
{
int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeof(int));

printf( buf1 ? buf2 ? "Downward" : "Backward");

return 0;
}

Thanks...

推荐答案

在文章< 17 ************************** @ posting.google.com>,

Nitin Bhardwaj写道:
In article <17**************************@posting.google.com >,
Nitin Bhardwaj wrote:
我想知道C程序中的堆栈是向上增长
还是
向下。所以我写了一些代码看到那个。请指导我这个代码是否正确告诉我这个?

#inclu de< stdio.h>
int main(void)
{* / int * buf1 = malloc(10 * sizeof(int));
int * buf2 = malloc(10 * sizeof(int));

printf(buf1? buf2? "向下" :向后);

返回0;
}
I wanted to know whether the stack in a C program is growing upwards
or
downwards.So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ?

#include <stdio.h>
int main(void)
{
int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeof(int));

printf( buf1 ? buf2 ? "Downward" : "Backward");

return 0;
}




malloc等都不会在堆栈上分配。

堆栈的方向在C中是未定义的。请参阅操作系统或硬件

特定文档。


Ariane

-

我们还记得那首歌,

但不是写它的手。

Nightwish - Dead Boys诗歌



malloc et all don''t allocate on the stack.
The direction of the stack is undefined in C. Refer to OS or hardware
specific documentation.

Ariane
--
We remember the song,
But not the hand that wrote it.
Nightwish -- Dead Boys Poem


文章< 17 ************************* *@posting.google.com>,

Nitin Bhardwaj< ni ************* @ hotmail.com>写道:
In article <17**************************@posting.google.com >,
Nitin Bhardwaj <ni*************@hotmail.com> wrote:
我想知道C程序中的堆栈是向上还是向下。所以我写了一些代码来看看。请指导我这个代码是否正确告诉我这个?
int * buf1 = malloc(10 * sizeof(int));
int * buf2 = malloc(10 * sizeof(int));

printf(buf1?buf2? 向下:向后);
I wanted to know whether the stack in a C program is growing upwards
or downwards. So I wrote a little code to see that.Please guide me as to
whether this code is correct in telling this ? int * buf1 = malloc(10 * sizeof(int));
int *buf2 = malloc(10*sizeof(int));

printf( buf1 ? buf2 ? "Downward" : "Backward");




这将显示堆上分配的两个对象的顺序(

可能不一致)。你需要比较当地

变量的地址,例如


char * stack_dir(无效)

{

int a;

返回stack_dir_helper(& a);

}


char * stack_dir_helper(int * a)

{

int * b;

返回b<一个 ? "向下" :up;

}


这当然在理论上不是便携式的,但很可能适用于所有

答案很有用的机器。


我记得唯一需要了解堆栈的程序

方向是Doug Gwyn''的便携式alloca"。


- Richard



This shows you the order of two objects allocated on the heap (which is
likely not to be consistent). You need to compare the addresses of local
variables, e.g.

char *stack_dir(void)
{
int a;
return stack_dir_helper(&a);
}

char *stack_dir_helper(int *a)
{
int *b;
return b < a ? "down" : "up";
}

This is of course not theoretically portable, but is likely to work on all
machines where the answer would be useful.

The only program I recall seeing that needed to know the stack
direction was Doug Gwyn''s "portable alloca".

-- Richard


Nitin Bhardwaj在02/08/04写道:
Nitin Bhardwaj wrote on 02/08/04 :
我想知道C程序中的堆栈是否向上增长

向下。
I wanted to know whether the stack in a C program is growing upwards
or
downwards.




没有标准方法可以确定。答案在你的

编译器的文档中。


-

Emmanuel

C-FAQ: http://www.eskimo。 com /~scs / C-faq / faq.html


C是一个尖锐的工具



There is no standard way to determine that. The answer is on your
compiler''s documentation.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"


这篇关于如何知道堆栈正在向上/向下增长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆