申报和malloc的区别 [英] Difference between declaration and malloc

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

问题描述

首先,我很抱歉,但我不知道这是一个适当的标题,我不知道该怎么称呼某人时allocs记忆像

First of all, I'm sorry but I'm not sure this is an appropriate title, I don't know how to call when someones "allocs" memory like

int list[n]

所以下跌随意更改标题。

so fell free to change the title.

今天我帮助与一些C code我的一个朋友,我发现一些奇怪的行为,我无法解释他为什么发生。我们有TSV文件,整数列表,用int每一行。第一行是行列表具有的数目。

Today I was helping a friend of mine with some C code, and I've found some strange behavior that I couldn't explain him why it was happening. We had TSV file with a list of integers, with an int each line. The first line was the number of lines the list had.

我们也有一个非常简单的ReadFile的C文件。第一行读取到n,行数,则出现了一个初始化

We also had a c file with a very simple "readfile". The first line was read to n, the number of lines, then there was an initialization of:

int list[n]

和最后一个用于与的fscanf n个循环

and finally a for loop of n with a fscanf.

有关小样本的(直到〜100.000),一切都很好。然而,我们发现,当n为大(10 ^ 6),将发生段错误。

For small n's (till ~100.000), everything was fine. However, we've found that when n was big (10^6), a segfault would occur.

最后,我们改变了列表初始化

Finally, we changed the list initialization to

int *list = malloc(n*sizeof(int))

和一切,当好,甚至具有非常大的n。

and everything when well, even with very large n.

有人可以解释为什么发生这种情况?是什么导致与诠释列表[N]的段错误,当我们开始使用名单,已停止=的malloc(N * sizeof的(INT))?

Can someone explain why this occurred? what was causing the segfault with int list[n], that was stopped when we start using list = malloc(n*sizeof(int))?

推荐答案

有几种不同的作品在这里打球。

There are several different pieces at play here.

首先是声明数组为

int array[n];

int* array = malloc(n * sizeof(int));

在第一个版本,您声明与自动存储持续时间的对象。这意味着,在阵列只生活只要调用它的存在的功能。在第二个版本,你正在使用动态存储的持续时间,这意味着它会一直存在,直到它被明确地免费释放的内存。

In the first version, you are declaring an object with automatic storage duration. This means that the array lives only as long as the function that calls it exists. In the second version, you are getting memory with dynamic storage duration, which means that it will exist until it is explicitly deallocated with free.

这是第二个版本在这里工作的原因是C是如何通常编译的实现细节。通常情况下,C内存被分为几个区域,包括堆栈(函数调用和局部变量)和堆(为的malloc 编辑对象)。栈典型地具有比堆的尺寸小得多;通常它像8MB。因此,如果你尝试分配一个巨大的数组以

The reason that the second version works here is an implementation detail of how C is usually compiled. Typically, C memory is split into several regions, including the stack (for function calls and local variables) and the heap (for malloced objects). The stack typically has a much smaller size than the heap; usually it's something like 8MB. As a result, if you try to allocate a huge array with

int array[n];

然后,你可能会超过栈的存储空间,导致段错误。另一方面,堆通常具有巨大尺寸(例如,尽可能多的空间是自由的系统上),等等的malloc ING一个大对象不会导致内存外的一个错误。

Then you might exceed the stack's storage space, causing the segfault. On the other hand, the heap usually has a huge size (say, as much space as is free on the system), and so mallocing a large object won't cause an out-of-memory error.

在一般情况下,要小心变长数组在C他们可以很容易地超过堆栈大小。 preFER 的malloc ,除非你知道的尺寸小,或者你真的只想要做的时间短时期的数组。

In general, be careful with variable-length arrays in C. They can easily exceed stack size. Prefer malloc unless you know the size is small or that you really only do want the array for a short period of time.

希望这有助于!

这篇关于申报和malloc的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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