malloc的限制??? [英] restrictions with malloc???

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

问题描述




我有一个(更大的)程序,里面有大约15-30个malloc'(太大了,不能在这里发布
) )... ...我今天尝试的最后一件事是添加另一个

malloc ** two_dimensional_data。但是我发现malloc总是

此时返回null并且程序退出(即使我是
malloc''只有20个字节或者其他东西)......然后我用Google搜索了这个问题

并发现了一些关于内存池的问题???是那个标准的C?


我不明白它,但它说了一些关于它的可能性

来选择内存池的大小要像4 kb(或其他)...?

我复制/粘贴代码,但它没有工作......


然后我开始想起这个堆 - 我从来没有真正理解过的东西。这个堆是否有任何大小限制?在我的程序中,我可以没有任何麻烦地获得更多

而不是20-30 malloc吗?


我的解决方案是静态分配这个two_dimensional_data-array

(声明为double two_dimensional_data [15] [15]),但如果可能,我更喜欢

动态方法......


假设在某个时刻我需要分配200 MB RAM。 AFAIR这个

是不可能的,即使系统有免费的内存量?

在这种情况下可以做什么?

因此问题:使用malloc是否有任何限制

与知道相关?

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

--------------------- -------------------------------------------------- ----

Martin J?rgensen的家 - http:// www .martinjoergensen.dk

推荐答案

Martin J?rgensen< un ********* @ spam.jay。净>写道:
Martin J?rgensen <un*********@spam.jay.net> writes:
我有一个(更大的)程序,里面有大约15-30个malloc'(太大了,不能在这里发布)...我今天尝试的最后一件事是添加另一个
malloc ** two_dimensional_data。但我发现malloc总是
此时返回null并且程序退出(即使我只使用了malloc''只有20个字节或者其他东西)...然后我用Google搜索了
问题,发现了一些关于内存池的事情???这是标准的C吗?

我不明白它,但它说了一些关于它可以选择内存池的大小就像4 kb(或<无论如何......?我复制/粘贴代码,但它没有工作......

然后我开始想起这个堆 - 我从未真正理解过的东西。这个堆是否有任何大小限制?在我的程序中,我可以毫不费力地使用超过20-30个malloc吗?
I have a (bigger) program with about 15-30 malloc''s in it (too big to
post it here)... The last thing I tried today was to add yet another
malloc **two_dimensional_data. But I found out that malloc always
returned null at this moment and the program exited (even though if I
malloc''ed only 20 bytes or something)... Then I googled for this
problem and found something about a memory pool??? Is that standard C?

I didn''t understand it, but it said something about that it was
possible to choose the size of the memory pool to be like 4 kb (or
whatever)...? I copy/pasted the code, but it didn''t work...

Then I came to think of this "heap"-thing which I never really
understood. Is there any size limit on this "heap"? And can I have
more than 20-30 malloc''s in my program without any trouble?




当你说20-30 malloc时's",你的意思是你的程序在
中调用20-30个malloc(),或者你的意思是malloc()被称为20-30次

你的程序正在执行?


无论哪种方式,malloc调用的次数都不太可能相关。


调用malloc()尝试为你的程序分配内存以使用
。它执行此操作的方式是特定于实现的,但

通常有一大块(实际或虚拟)内存

某处,可能会根据需要扩展,malloc()给你

该块的子块。这个大块通常被称为堆,但是知道它的名字并不是真的对你有多好。

好​​。关键是它是有限的,你最终可以耗尽它b $ b。实际大小不是由语言定义的;可能没有

甚至可以找到答案。


与您分配的内存总量相关的是什么。 />

您的系统可能提供一些控制内存限制的方法(如果你在类Unix系统上,请参阅限制

或ulimit )。


如果你添加到每个malloc()调用一个printf语句来记录你分配的内存多少
,它可能会有所帮助追查

问题 - 假设你实际上已经没有内存了。或者

像交互式调试器这样的东西可能会提供一些跟踪这些信息的工具。或者你也许可以使用一种名为内存分析器的东西。

。 (这些工具都没有用

语言指定,所以这里的细节是偏离主题的。)


它也有可能是malloc( )是失败的,因为你已经完成了一些调用未定义行为的东西。例如,如果你在一块已分配内存的范围之外写入
,你可能会破坏malloc()用来跟踪 />
分配了什么。这种问题可能非常难以追踪。


comp.lang.c FAQ,< http://www.c- faq.com/> ;.有一个关于内存的部分

分配你可能会觉得有用。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

San迭戈超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



When you say "20-30 malloc''s", do you mean 20-30 malloc() calls in
your program, or do you mean that malloc() is called 20-30 times as
your program is executing?

Either way, the number of malloc calls is unlikely to be relevant.

A call to malloc() attempts to allocate memory for your program to
use. The manner in which it does this is implementation-specific, but
typically there''s some big chunk of (real or virtual) memory
somewhere, possibly expandable as necessary, and malloc() gives you
sub-chunks of that chunk. This big chunk is commonly referred to as
the "heap", but knowing the name for it doesn''t really do you much
good. The point is that it''s finite, and you can eventually exhaust
it. The actual size is not defined by the language; there may not
even be a way to find out.

What''s relevant is the total amount of memory you''ve allocated.

Your system may provide some way to control memory limits (see "limit"
or "ulimit" if you''re on a Unix-like system).

If you add to each malloc() call a printf statement that logs how much
memory you''re allocating, it may be helpful in tracking down the
problem -- assuming you''re actually running out of memory. Or
something like an interactive debugger might provide some facility for
tracking this information. Or you might be able to use something
called a "memory profiler". (None of these tools are specified by the
language, so the details are off-topic here.)

It''s also possible that malloc() is failing because you''ve done
something that invokes undefined behavior. For example, if you write
outside the bounds of a chunk of allocated memory, you might corrupt
the internal data structures that malloc() uses to keep track of
what''s been allocated. This kind of problem can be very difficult to
track down.

The comp.lang.c FAQ, <http://www.c-faq.com/>. has a section on memory
allocation that you might find useful.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson写道:
Keith Thompson wrote:
Martin J?rgensen< un ********* @ spam.jay.net>写道:
Martin J?rgensen <un*********@spam.jay.net> writes:
我有一个(更大的)程序,里面有大约15-30个malloc'(太大了,不能在这里发布)......最后一件事我今天试过要添加另一个
malloc ** two_dimensional_data。但我发现malloc总是
此时返回null并且程序退出(即使我只使用了malloc''只有20个字节或者其他东西)...然后我用Google搜索了
问题,发现了一些关于内存池的事情???这是标准的C吗?

我不明白它,但它说了一些关于它可以选择内存池的大小就像4 kb(或<无论如何......?我复制/粘贴代码,但它没有工作......

然后我开始想起这个堆 - 我从未真正理解过的东西。这个堆是否有任何大小限制?我可以毫不费力地在我的程序中使用超过20-30个malloc吗?

当你说20-30 malloc时,你的意思是20 -30 malloc()调用你的程序,或者你的意思是当你的程序执行时,malloc()被调用了20-30次?
I have a (bigger) program with about 15-30 malloc''s in it (too big to
post it here)... The last thing I tried today was to add yet another
malloc **two_dimensional_data. But I found out that malloc always
returned null at this moment and the program exited (even though if I
malloc''ed only 20 bytes or something)... Then I googled for this
problem and found something about a memory pool??? Is that standard C?

I didn''t understand it, but it said something about that it was
possible to choose the size of the memory pool to be like 4 kb (or
whatever)...? I copy/pasted the code, but it didn''t work...

Then I came to think of this "heap"-thing which I never really
understood. Is there any size limit on this "heap"? And can I have
more than 20-30 malloc''s in my program without any trouble?

When you say "20-30 malloc''s", do you mean 20-30 malloc() calls in
your program, or do you mean that malloc() is called 20-30 times as
your program is executing?




这不是一回事吗?


我的意思是......我有很多变量被定义为** two_D_arrays:


main()

{

two_D_array1 = malloc(字节数);

printf(" bla.bla") ;

//从文件加载

two_D_array2 = malloc(字节数);

two_D_array3 = malloc(字节数);


two_D_array1 [0] = malloc((字节数)*(字节数));

two_D_array1 [1] = malloc((字节数) *(字节数));

等...


for(i = 1; i< number; i ++)

{

two_D_array1 [i] = two_D_array1 [i-1] + number_of_cols;

two_D_array2 [i] = two_D_array2 [i-1] + number_of_cols;

//还有更多......

}


// bla.bla。从文件/输出到屏幕的输入

//这里有更多的mallocs ...

}


Malloc可能被称为20左右-30次,所以大约有20-30

行,其中包含malloc()...

无论哪种方式,malloc调用的次数都不太可能相关。


我这么认为....

调用malloc()会尝试为你的程序分配内存以供使用。它执行此操作的方式是特定于实现的,但通常在某处可能会有一大块(实际或虚拟)内存,可能会根据需要进行扩展,而malloc()会为您提供<这个块的子块。这个大块通常被称为堆,但知道它的名称并不是真的对你有好处。关键是它是有限的,你最终可能会耗尽它。实际大小不是由语言定义的;有可能甚至没有办法找出答案。


但是我只分配了大约7-8千字节....我必须做某事

错误的地方....也许那里''' s某个索引错了我可能

已经为一个变量分配了太少的内存来覆盖

的东西,你不觉得吗?

AFAIR我还调试了一个for循环,如:


for(i = 0; i< number; i ++)//突然i从2更改为6 ...

与您分配的内存总量相关的是什么。


大约8千字节是没问题的,是吗?

您的系统可能提供一些控制内存限制的方法(参见限制
或ulimit如果你在类Unix系统上)。


嗯...我正在使用visual studio 2005在windows xp上。我如何看待

是否有限制?

如果你添加到每个malloc()调用一个printf语句来记录你分配的内存量,它可能有助于追踪
问题 - 假设你是实际上内存不足。或者像交互式调试器这样的东西可能会为跟踪这些信息提供一些便利。或者您可以使用称为内存分析器的内容。 (这些工具都不是由
语言指定的,所以详细信息在这里是偏离主题的。)


什么是交互式调试器?我有一个带有

visual studio 2005的交互式调试器吗?既然我可以制作断点并进入

计划,你的意思是交互式吗?


我也读了一下关于那些探查器工具(valgrant等),但从来没有

尝试过它们......我想我将从printf(...)开始 - 再次使用行和我

希望'足够......

因为你已经完成了调用未定义行为的东西,所以malloc()也可能失败了。例如,如果你在一块已分配的内存的边界之外编写
,你可能会破坏malloc()用来跟踪分配的内部数据结构
。这种问题很难追查。


我认为这是真正的问题...

comp.lang.c FAQ,< http://www.c- faq.com/> ;.有一个关于内存分配的部分你可能会觉得很有用。



Isn''t that the same thing?

I mean... I have a lot of variables defined as **two_D_arrays:

main()
{
two_D_array1 = malloc( number of bytes);
printf("bla.bla");
// load from files
two_D_array2 = malloc( number of bytes);
two_D_array3 = malloc( number of bytes);

two_D_array1[0] = malloc( (number of bytes)*(number of bytes) );
two_D_array1[1] = malloc( (number of bytes)*(number of bytes) );
etc...

for(i=1; i<number; i++)
{
two_D_array1[i] = two_D_array1[i-1] + number_of_cols;
two_D_array2[i] = two_D_array2[i-1] + number_of_cols;
// and a lot more...
}

// bla.bla. input from file/output to screen
// more mallocs here...
}

Malloc is probably called about 20-30 times, so there are about 20-30
lines with the word malloc() in it...
Either way, the number of malloc calls is unlikely to be relevant.
I thought so....
A call to malloc() attempts to allocate memory for your program to
use. The manner in which it does this is implementation-specific, but
typically there''s some big chunk of (real or virtual) memory
somewhere, possibly expandable as necessary, and malloc() gives you
sub-chunks of that chunk. This big chunk is commonly referred to as
the "heap", but knowing the name for it doesn''t really do you much
good. The point is that it''s finite, and you can eventually exhaust
it. The actual size is not defined by the language; there may not
even be a way to find out.
But I only allocated about 7-8 kilo-bytes.... I must have done something
wrong somewhere.... Perhaps there''s an index wrong somewhere and I might
have allocated too little memory for a variable that overwrites
something, don''t you think?

AFAIR I also debugged a for-loop like:

for(i=0; i<number; i++) // and suddenly "i" changed from 2 to 6...
What''s relevant is the total amount of memory you''ve allocated.
About 8 kilo-bytes is no problem, is it?
Your system may provide some way to control memory limits (see "limit"
or "ulimit" if you''re on a Unix-like system).
Hmmm.. I''m on windows xp using visual studio 2005. How do I see if there
is any limit?
If you add to each malloc() call a printf statement that logs how much
memory you''re allocating, it may be helpful in tracking down the
problem -- assuming you''re actually running out of memory. Or
something like an interactive debugger might provide some facility for
tracking this information. Or you might be able to use something
called a "memory profiler". (None of these tools are specified by the
language, so the details are off-topic here.)
What an interactive debugger? Do I have an interactive debugger with
visual studio 2005? Since I can make breakpoints and step into the
program, is that what you mean by "interactive"?

I''ve also read a bit about those profiler tools (valgrant etc) but never
tried them... I guess I''ll start with printf("...")-lines again and I
hope that''s sufficient...
It''s also possible that malloc() is failing because you''ve done
something that invokes undefined behavior. For example, if you write
outside the bounds of a chunk of allocated memory, you might corrupt
the internal data structures that malloc() uses to keep track of
what''s been allocated. This kind of problem can be very difficult to
track down.
I think that''s the real problem...
The comp.lang.c FAQ, <http://www.c-faq.com/>. has a section on memory
allocation that you might find useful.




所以,做数百个malloc()调用没问题 - 调用如果

他们每次只请求50个字节的内存,有吗?


我也从未想过这个内存池 - 事情" ...有没有人

对此有所了解:

http://www.die.net/doc/linux/man/man3/mpool.3.html


那不是标准C是它还是???在我需要知道的

windows-machines上是否有这样的东西?


它说:函数mpool_open初始化一个内存池。 ; ...

致敬/ Med venlig hilsen

Martin J?rgensen


-

------------------------------------------------ ---------------------------

Martin J?rgensen的故乡 - http://www.martinjoergensen.dk




Martin J?rgensen写道:

Martin J?rgensen wrote:
我的意思是......我有很多变量被定义为** two_D_arrays:

main()
{
two_D_array1 = malloc(字节数);
printf(" bla.bla");
//从文件加载
two_D_array2 = malloc(字节数);
two_D_array3 = malloc(字节数);

two_D_array1 [0] = malloc((字节数)*(字节数));
two_D_array1 [1] = malloc((字节数)*(字节数));
等...
I mean... I have a lot of variables defined as **two_D_arrays:

main()
{
two_D_array1 = malloc( number of bytes);
printf("bla.bla");
// load from files
two_D_array2 = malloc( number of bytes);
two_D_array3 = malloc( number of bytes);

two_D_array1[0] = malloc( (number of bytes)*(number of bytes) );
two_D_array1[1] = malloc( (number of bytes)*(number of bytes) );
etc...




这几乎肯定不是什么你要。如果你想要一个rowXcols

数组,你可以这样做:


int ** two_D_array;

two_D_array = malloc(rows * sizeof(int *));

for(int i = 0; i< 15; i ++)

two_D_array [i] = malloc(cols * sizeof(int) );

并且能够将值引用为two_D_array [x] [y],


或者你可以做

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

并且无法使用array [x] [y]表示法轻松引用。

(你需要做数组[rows * i + j]



This is almost certainly not what you want. If you want a rowsXcols
array of ints you could do:

int **two_D_array;
two_D_array = malloc(rows * sizeof(int *));
for (int i=0; i< 15; i++)
two_D_array[i] = malloc(cols * sizeof(int));
And be able to reference values as two_D_array[x][y],

Or you can do
int *array = malloc (rowsXcols * sizeof(int));
and lose the ability to reference easily with array[x][y] notation.
(you need to do array[rows*i + j]


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

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