堆与堆栈分配 [英] Heap vs Stack allocations

查看:71
本文介绍了堆与堆栈分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


void f1(int n){vector< int> X(N); / * C ++ * /}

void f2(int n){int x [n]; / *只有C99 * /}

void f3(int n){int * x = new int [n]; / * C ++ * / delete [] x; } $ />

void f4(int n){int * x =(int *)malloc(n * sizeof(int));

/ * .. 。* / free(x);在所有这些情况下,编译器在堆栈而不是堆上分配x /
是有意义的。但是,AFAIK,只有在f2的情况下,

编译器才需要这样做。没有学习汇编,是否有任何

方式来查看特定编译器如何处理这些情况?


祝福,

MSG

Hello

void f1(int n) { vector<int> x(n); /* C++ */ }

void f2(int n) { int x[n]; /* C99 only */ }

void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }

void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
/*...*/ free(x); }

In all of these cases it makes sense for the compiler to allocate x on
the stack instead of the heap. However, AFAIK, only in case of f2 the
compiler is required to do so. Without learning assembly, is there any
way to see how a particular compiler handles each of these cases?

Best wishes,
MSG

推荐答案

ms ***** @ yahoo .com (MSG)写道:
ms*****@yahoo.com (MSG) writes:
void f1(int n){vector< int> X(N); / * C ++ * /}

void f2(int n){int x [n]; / * C99 only * /}

void f3(int n){int * x = new int [n]; / * C ++ * / delete [] x; } void void f4(int n){int * x =(int *)malloc(n * sizeof(int));
/*...*/ free(x);在所有这些情况下,编译器在堆栈而不是堆上分配x是有意义的。但是,AFAIK,只有在f2的情况下,
编译器才需要这样做。


不,在C和C ++中都不需要堆和/或堆栈来存在
。实现分配内存的方式/位置是未指定的。

如果没有学习汇编,有没有办法看看特定的
编译器如何处理这些情况?
void f1(int n) { vector<int> x(n); /* C++ */ }

void f2(int n) { int x[n]; /* C99 only */ }

void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }

void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
/*...*/ free(x); }
In all of these cases it makes sense for the compiler to allocate x on
the stack instead of the heap. However, AFAIK, only in case of f2 the
compiler is required to do so.
No, neither in C nor in C++ a heap and/or stack is even required to
exist. How/where the implementation allocates memory is unspecified.
Without learning assembly, is there any way to see how a particular
compiler handles each of these cases?




- 请参阅编译器文档。

- 询问编译器供应商。

- 对于编译器和平台的每个组合,请记住,请教在专门用于该编译器/平台的
新闻组中。


Martin



- Consult the compiler documentation.
- Ask the compiler vendor.
- For each combination of compiler and platform you have in mind, ask in a
newsgroup dedicated to that compiler/platform.

Martin


MSG写道:< br>
MSG wrote:
void f1(int n){vector< int> X(N); / * C ++ * /}

void f2(int n){int x [n]; / * C99 only * /}

void f3(int n){int * x = new int [n]; / * C ++ * / delete [] x; } void void f4(int n){int * x =(int *)malloc(n * sizeof(int));
/*...*/ free(x);在所有这些情况下,编译器在堆栈而不是堆上分配x
是有意义的。


为什么?

然而,AFAIK,只有在f2的情况下,编译器才需要这样做。
没有学习汇编,是否有任何看到
特定的编译器如何处理这些情况?
void f1(int n) { vector<int> x(n); /* C++ */ }

void f2(int n) { int x[n]; /* C99 only */ }

void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }

void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
/*...*/ free(x); }

In all of these cases, it makes sense for the compiler to allocate x
on the stack instead of the heap.
Why?
However, AFAIK, only in case of f2, the compiler is required to do so.
Without learning assembly, is there any see
how a particular compiler handles each of these cases?




是的。


数组x的存储在除f2之外的所有情况下从免费商店(堆)分配



自动存储在f2中为数组x分配(来自堆栈)如果您使用符合C99的C编译器或C或C ++编译器,那么
将支持可变大小数组的
作为C89或C ++的扩展。


已经起草了一个新的C ++标准

但是我不认为它指定了对可变大小数组的支持......

呢。



Yes.

Storage for array x is allocated from the free store (the "heap")
in all cases except f2.
Automatic storage is allocated (from the stack) in f2 for array x
if you use a C99 compliant C compiler or a C or C++ compiler
that supports variable size arrays as an extension to C89 or C++.

A new C++ standard has been drafted
but I don''t think that it specifies support for variable size arrays ...
yet.


ms*****@yahoo.com ( MSG)在消息新闻中写道:< 54 ************************** @ posting.google。 com> ...
ms*****@yahoo.com (MSG) wrote in message news:<54**************************@posting.google. com>...

void f1(int n){vector< int> X(N); / * C ++ * /}

void f2(int n){int x [n]; / * C99 only * /}

void f3(int n){int * x = new int [n]; / * C ++ * / delete [] x; } void void f4(int n){int * x =(int *)malloc(n * sizeof(int));
/*...*/ free(x);在所有这些情况下,编译器在堆栈而不是堆上分配x是有意义的。但是,AFAIK,只有在f2的情况下,
编译器才需要这样做。


你是说C99引入了一个要求实现

有堆栈吗?我很惊讶。

如果没有学习汇编,有什么方法可以看看特定编译器如何处理这些情况?

void f1(int n) { vector<int> x(n); /* C++ */ }

void f2(int n) { int x[n]; /* C99 only */ }

void f3(int n) { int* x = new int[n]; /* C++ */ delete [] x; }

void f4(int n) { int* x = (int*)malloc(n*sizeof(int));
/*...*/ free(x); }

In all of these cases it makes sense for the compiler to allocate x on
the stack instead of the heap. However, AFAIK, only in case of f2 the
compiler is required to do so.
Are you saying that C99 introduced a requirement that implementations
have a stack? I''m surprised.
Without learning assembly, is there any
way to see how a particular compiler handles each of these cases?




不在标准C中。特定的编译器可能有办法做到这一点,

但你需要在新闻组中询问讨论该编译器。

我不知道C ++。



Not in Standard C. A particular compiler might have a way to do it,
but you''d need to ask in a newsgroup that discussed that compiler.
I''ve no idea about C++.


这篇关于堆与堆栈分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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