大型数组:malloc或'只是声明'? [英] Large arrays: malloc or 'just declare'?

查看:63
本文介绍了大型数组:malloc或'只是声明'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有人可以提供帮助。


我需要使用大型阵列而且我不确定何时使用应该

使用(A)以及何时应该使用(B): -


#define MAX 10000000


(A)int x [MAX];


(B)int * x =(int *)malloc(MAX * sizeof(int));


这不是编译器特定的问题,但是对于某些背景,我使用(A)较低的MAX值,但当我开始增加它时,我点击

borland编译器的一些编译器/链接器问题。


然后我注意到可执行文件的大小是相似的,无论

是否

我使用(A)或(B)可能是(B)的温和优势。


除编译器/链接器问题外,假设MAX是固定的(因为它!)

为什么我会使用(B)而不是(A)?


我认为答案与堆栈对比堆(但我很感兴趣在任何

其他答案中出现)。任何人都可以指点我在网上参考

堆栈与堆,我的谷歌搜索没有激发我的灵感。


提前谢谢

Hal。

Hello,

Can someone please help.

I need to use a large array and I''m not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );

This isn''t a compiler specific problem, but just for some background, I was
using (A) for lower values of MAX but when I started increasing it, I hit
some compiler/linker problems with the borland compiler.

I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).

Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?

I think the answer relates to stack versus heap (but I''m interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven''t inspired me.

Thanks in advance
Hal.

推荐答案

i)首先,如果你想要使用非常大的数组,(因为你有

正确地把它放到b4),去堆积并避免占用堆栈空间

吧。


ii)其次,生活变量的时间。大多数情况下,你会想要移动非常大的数组所指向的数据并在它们上运行

。在这种情况下,将它放在堆上并且

传递给它的地址也是有意义的。

是的。你甚至可以在堆栈上定义一个局部变量并在地址周围移动

,但不用说,一旦函数

返回就会丢失,而如果它从堆中取出,它仍然存在(但是然后

通过在差异函数中分离

malloc / free,总会有潜在的内存泄漏危险。)

我的投票是为了堆!!


Hal Styli写道:
i) First of all, if you want to go for very large arrays,( as you had
rightly put it b4 ) , go for the heap and avoid taking up stack space
for it.

ii) Second, the life time of the variable. Most of the time, you would
want to move around the data pointed by the very large array and operate
on them. In that case, again it makes sense to have it on the heap and
pass around the address of the same.
Yeah . you can even define a local variable on the stack and move
around the address, but needless to say that is lost once the function
returns, whereas if it taken from heap, it is still there ( But then
there is always a lurking danger of a memory leak by separating the
malloc / free in diff. functions . )
My vote goes for heap !!

Hal Styli wrote:
你好,

可以有人请帮忙。

我需要使用大型阵列,我不确定何时应该使用(A)以及何时应该使用(B): -

#define MAX 10000000
(A)int x [MAX];

(B)int * x =(int *)malloc(MAX * sizeof(int));

提前致谢
Hal。
Hello,

Can someone please help.

I need to use a large array and I''m not sure when one should
use (A) and when one should use (B) below:-

#define MAX 10000000

(A) int x[MAX];

(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );

Thanks in advance
Hal.




-


-

Rakesh Kumar

**从我的电子邮件地址中删除nospamplz以获取我的真实电子邮件**



--

--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **


Hal Sty li< no_spam @ all>这样说:
Hal Styli <no_spam@all> spoke thus:
#define MAX 10000000
(A)int x [MAX];
(B)int * x =(int *)malloc(MAX * sizeof(int));
^^^^^^^^^

不要再投出malloc的返回值。如果您忘记了包含< stdlib.h>的
,如果您投出malloc'的返回

值,您就不会知道。关于这个主题的高度详细的讨论可以在这个

组的档案中找到。

这不是编译器特定的问题,但仅仅是为了某些背景,我是
使用(A)较低的MAX值但是当我开始增加它时,我用borland编译器打了一些编译器/链接器问题。


如果它不是编译器特定的,为什么要提到Borland的编译器?您可以分配的
自动存储空间量是您的实现以及如何使用它的函数。

然后我注意到它的大小无论是否使用(A)或(B)可能是(B)的温和优势,可执行文件都是类似的。


大多数情况下都是无关紧要的,除非你有一个(错误的)发展在

高度空间敏感的平台上,比如嵌入式设备。

除编译器/链接器问题外,假设MAX是固定的(因为它!)
为什么我会使用(B)而不是(A)?


在以下时间使用A:


1)你知道在编译时你想要多少空间,

2你将来不会想要更多的空间,并且

3)你不会遇到诸如你所拥有的问题。

否则,使用B.(对,clc?)请记住,你必须释放()内存

你用malloc()分配。

我认为答案涉及堆栈与堆(但我对任何其他答案感兴趣)。任何人都可以指向我在堆栈与堆栈的在线参考,我的谷歌搜索没有启发我。
#define MAX 10000000 (A) int x[MAX]; (B) int *x = ( int * ) malloc( MAX*sizeof( int ) ); ^^^^^^^^^
Don''t cast the return value of malloc, ever. If you''ve forgotten to
include <stdlib.h>, you won''t know it if you cast malloc''s return
value. Highly detailed discussions on this topic can be found in this
group''s archives.
This isn''t a compiler specific problem, but just for some background, I was
using (A) for lower values of MAX but when I started increasing it, I hit
some compiler/linker problems with the borland compiler.
If it isn''t compiler specific, why mention Borland''s compiler? The
amount of automatic storage you can allocate is a function of your
implementation and how you use it.
I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).
Mostly irrelevent, unless you have the (mis)fortune of developing on a
highly space-sensitive platform, such as an embedded device.
Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?
Use A when:

1) You know how much space you want at compile time,
2) You won''t want more space at any future time, and
3) You don''t run into problems such as the one you''re having.

Otherwise, use B. (Right, clc?) Remember that you must free() memory
you allocate with malloc().
I think the answer relates to stack versus heap (but I''m interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven''t inspired me.




在这个新闻组,堆栈 ;和堆是危险的术语,因为
实现不需要使用这样的结构来管理它们分配的内存。至于参考文献,以下内容包含丰富的有价值的C信息:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/ off-topic.html

-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



On this newsgroup, "stack" and "heap" are dangerous terms, as
implementations need not use such structures to manage the memory they
allocate. As for references, the following contain a wealth of
valuable C information:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


2004年4月20日星期二01:45:53 +0000,Christopher Benson-Manica写道:
On Tue, 20 Apr 2004 01:45:53 +0000, Christopher Benson-Manica wrote:
Hal Styli< no_spam @ all>这样说:
Hal Styli <no_spam@all> spoke thus:
#define MAX 10000000
#define MAX 10000000
(A)int x [MAX];
(A) int x[MAX];


(B)int * x =(int *)malloc(MAX * sizeof(int));
(B) int *x = ( int * ) malloc( MAX*sizeof( int ) );


^^^^^^^^^
不要投永远都是malloc的返回值。如果您忘记包含< stdlib.h>,如果您投出malloc'的返回值,您就不会知道它。关于这个主题的高度详细的讨论可以在这个
组的档案中找到。


^^^^^^^^^
Don''t cast the return value of malloc, ever. If you''ve forgotten to
include <stdlib.h>, you won''t know it if you cast malloc''s return
value. Highly detailed discussions on this topic can be found in this
group''s archives.

然后我注意到可执行文件的大小是相似的,无论
是否
我使用(A)或(B)这可能是(B)的一个温和优势。
I then noticed that the size of the executable was similar regardless of
whether
I used (A) or (B) which might have been a mild advantage of (B).



大多数情况下是无关紧要的,除非你有一个(错误的)发展在<高度对空间敏感的平台,例如嵌入式设备。



Mostly irrelevent, unless you have the (mis)fortune of developing on a
highly space-sensitive platform, such as an embedded device.




在这种情况下你应该知道你的程序可以使用多少内存来

最近的字节或字。



In which case you should know how much memory your program can use to the
nearest byte or word.

除了编译器/链接器问题,假设MAX是固定的(因为它!)
为什么我会使用(B)而不是比(A)?
使用A时:

1)你知道在编译时你想要多少空间,
2)你不想要任何空间未来的时间,以及
3)你不会遇到诸如你所拥有的问题。

否则,使用B.(对, clc?)请记住你必须释放()内存你用malloc()分配。
Other than compiler/linker problems, assuming MAX is fixed (as it is!)
why would I use (B) rather than (A)?
Use A when:

1) You know how much space you want at compile time,
2) You won''t want more space at any future time, and
3) You don''t run into problems such as the one you''re having.

Otherwise, use B. (Right, clc?) Remember that you must free() memory
you allocate with malloc().




并且总是检查你是否调用malloc()成功;也就是说,

总是测试你反对NULL的指针。


我喜欢使用如下结构:


#include< stdlib.h>


/ * ... * /


int * ptr;


if((ptr = malloc(sizeof(int)* LEN))== NULL){

/ *处理无法分配RAM * /

}



And always check to see if your call to malloc() succeeded; that is,
always test the pointer you got back against NULL.

I like to use constructions like the below:

#include <stdlib.h>

/* ... */

int *ptr;

if ((ptr = malloc(sizeof(int) * LEN)) == NULL) {
/* Handle failure to allocate RAM */
}

我认为答案与堆栈与堆有关(但我对任何其他答案感兴趣)。任何人都可以指向我在堆栈与堆栈的在线参考,我的谷歌搜索没有启发我。
I think the answer relates to stack versus heap (but I''m interested in any
other answers that come up). Can anyone point me to an online reference on
stack vs heap, my google searches haven''t inspired me.



在这个新闻组,堆栈,和堆是危险的术语,因为
实现不需要使用这样的结构来管理它们分配的内存。至于参考文献,以下内容包含丰富的有价值的C信息:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic。 html




好​​指针。 ;)


-

yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz

给我发电子邮件,rot13和将拼写的数字转换为数字形式。

使黑客微笑让黑客微笑。



Good pointers. ;)

--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.


这篇关于大型数组:malloc或'只是声明'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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