堆栈或堆 [英] Stack or Heap

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

问题描述

使用下面的代码,当我将参数设置为大于20,832,000bytes的任何值时,我能够让我的编译器出现故障。

me。在

结构的情况下,其结构为868个实例。

编译器显然允许VLA,但是在上面的

字节数之后它就会崩溃。

我被告知我试图将每个字符串放入堆栈而不是

堆。所以我想知道是否有人可以清除它,这是真的吗?

我需要在需要时使用malloc 868个结构实例

避免seg故障?堆栈和堆到底是什么,我从来没有真正得到过这么多吗?


代码:

/ *程序说明了devc ++创建VLA的能力。* /


#include< stdio.h>

#include< string.h>

#define SIZE 200

#define MAX_CLMS 12

typedef struct {

char info [SIZE] ;

} CLMS

typedef struct {

CLMS clm [MAX_CLMS];

} PERSON;

int main(){

printf(多少个数组?);

int num; int n = 0;


scanf("%d",& num);

PERSON数组[num];


int x [ num];


for(n = 0; n< n; n ++)

x [n] = n;


for(n = 0; n< n ++)

printf("%d \ nn",x [n]);

main( ); / *用于测试目的* /


getchar(); getchar();


返回0;

}

using the following code, i was able to have my compiler seg fault on
me when i gave the argument as anythng greater than 20,832,000bytes. In
the case of the struct its 868 instances of said structure. The
compiler obviously allows VLA however it craps out after the above
amount of bytes.
I was told i was attempting to put everythng on the stack and not the
heap. So i was wondering if anyone can maybe clear it up, is that true?
would i have to then malloc 868 instances of that struct when needed to
avoid a seg fault? what exactly is the stack and the heap, i''ve never
really gotten around to that yet?

code:
/*program illustrates devc++ ability to create a VLA.*/

#include<stdio.h>
#include<string.h>
#define SIZE 200
#define MAX_CLMS 12

typedef struct{
char info[SIZE];
}CLMS
typedef struct{
CLMS clm[MAX_CLMS];
}PERSON;
int main(){
printf("how many arrays?");
int num;int n=0;

scanf("%d", &num);
PERSON array[num];

int x[num];

for(n=0;n<num;n++)
x[n]=n;

for(n=0;n<num;n++)
printf("%d\n",x[n]);
main(); /*for testing purposes*/

getchar();getchar();

return 0;
}

推荐答案




周一,2005年11月14日07:01: 32 -0800,bitshadow写道:
Hi,

On Mon, 14 Nov 2005 07:01:32 -0800, bitshadow wrote:
我被告知我试图将每个东西放在堆栈而不是堆上。所以我想知道是否有人可以清除它,这是真的吗?


'所有局部变量通常都是这种情况。

我需要在需要时使用malloc 868个实例来构造
避免seg故障?


为了避免出现seg故障,你需要回到malloc(我们是如何在C99的VLA之前做到这一点的b $ b)并且不要忘记正确的错误处理,并在你不再需要它时释放

内存。它并不是真的更复杂,

看起来并不好看。

堆栈和堆是什么,我从来没有真正得到过
到那个?
I was told i was attempting to put everythng on the stack and not the
heap. So i was wondering if anyone can maybe clear it up, is that true?
that''s generally the case with all local variables.
would i have to then malloc 868 instances of that struct when needed to
avoid a seg fault?
To avoid a seg fault, you need to fall back to malloc (how we did it
before C99''s VLAs) and don''t forget proper error handling and freeing the
memory when you don''t need it anymore. It''s not really more complicated,
just doesn''t look as nice.
what exactly is the stack and the heap, i''ve never really gotten around
to that yet?




基本上,它们只是记忆中的两个不同区域。全局可用

内存从堆中分配,堆栈用于本地

可用内存,返回地址,参数传递等。内存从堆中出来的
被分配在或多或少任意大小的
块中,而堆栈包含一个大块和指向它的指针

在分配或释放内存时移动。通常,从函数返回

会将堆栈指针返回到调用

函数之前的位置,自动释放分配在
$ b $上的所有内存从那以后b堆栈。在无法增加堆栈的平台上,堆栈空间因此而受限制。


情况并非总是这样。一些操作系统(比如某些版本的Linux的b
)从可寻址内存的底部增加堆栈区域,从顶部开始堆积区域,或者使用任意一个数字类似计划。

Daniel



Basically, they''re just two different areas in memory. Globally available
memory is allocated from the heap and the stack is used for locally
available memory, return addresses, parameter passing and the like. Memory
coming out of the heap is allocated in more or less arbitrarily sized
blocks, while the stack consists of one large block and a pointer into it
that is moved whenever memory is allocated or freed. Typically, returning
from a function will return the stack pointer to the position before the
function was invoked, automagically freeing all memory allocated on the
stack since then. On platforms that can''t grow the stack, stack space is
consequently limited.

This is not always the case. Some operating systems (like some versions
of Linux) grow the stack area from the bottom of addressable memory and
the heap area from the top, or employ any of a number of similar schemes.
Daniel


大家好。我知道这对你们大多数人来说都是微不足道的。

我为此道歉,但我没有想法。

下面的代码编译得很好但在执行时给出了分段错误。

看起来getline很麻烦。我使用debian linux,gcc。


#include< stdlib.h>

int main(){

char line [256];

int n;

printf("输入一个整数:\ n");

getline(第256行) );

n = atoi(线);

}


谢谢。 Cristian Barbarosie
http://cmaf.fc.ul.pt/~ barbaros

Hi everybody. I know this will look trivial for most of you.
I apologize for that, but I am out of ideas.
The code below compiles well but gives segmentation fault at execution.
Looks like getline is giving trouble. I use debian linux, gcc.

#include <stdlib.h>
int main () {
char line[256];
int n;
printf("Type an integer:\n");
getline(line, 256);
n = atoi(line);
}

Thank you. Cristian Barbarosie
http://cmaf.fc.ul.pt/~barbaros


2005年11月14日星期一08:09:22 -0800,barbaros写道:
On Mon, 14 Nov 2005 08:09:22 -0800, barbaros wrote:
下面的代码编译得很好,但在执行时会出现分段错误。
看起来getline会给你带来麻烦。我使用debian linux,gcc。
The code below compiles well but gives segmentation fault at execution.
Looks like getline is giving trouble. I use debian linux, gcc.




你的问题是1. getline()不是标准C和2.它不是
t工作就像你认为的那样。阅读''男人获取'' - 它有一个例子。

Daniel



Your problems are 1. that getline() is not standard C and 2. that it doesn
t work like you thought it did. Read ''man getline'' - it has an example.
Daniel


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

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