Dinamic阵列分配问题 [英] Dinamic Array Allocation problems

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

问题描述

我正在编写一个程序,它应该可以处理非常大的数字(数千个数字)。因此,数字是由包含指向整数的指针的结构所代表的,e 2为数组的维数e的数字。

指针用于数组的malloc ...

我的2个问题是:


1)有一段时间,当我使用我的exp函数时,我收到错误:


*** glibc检测到*** ./a.out:free():无效大小:0x0975a328 ***

======= Backtrace:===== ====

/lib/tls/i686/cmov/libc.so.6[0xb7e8b604]

/lib/tls/i686/cmov/libc.so。 6 [0xb7e8eabc]

/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x95)[0xb7e8f9c5]

./a.out[0x804a8ab]

./a.out[0x804bb52]

./a.out[0x804ba3d]

./a.out[0x804ba3d]

./a.out[0x804ba3d]

./a.out[0x804ba3d]

./a.out[0x804ba3d]

./a.out[0x804ba3d]

./a.out[0x8049e32]

./a.out[0x8048892]

/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e32775]

./a。 out [0x8048401]


和其他东西......可以因为它是一个递归函数吗?


第二个也是最大的问题是当数字变得太大(类似于1000位数字)时,我会遇到分段错误...这个维度的数组有奇怪的问题吗?


代码非常大但您可以在以下位置找到它:
http://www.speedyshare.com/403525688.html


ps:我删除了free()函数。 ..它造成了太多错误...


感谢您的帮助,对不起我的工作......

解决方案

< blockquote>您是否使用malloc(或calloc)动态分配内存;并且可以自由释放动态分配的内存?


如果是这样,当malloc / calloc返回错误因为没有可用的内存要分配时,你是否捕获了异常情况?如果没有,那么你需要这样做。


免费():无效大小:0x0975a328消息听起来就像你试图释放一块没有动态分配的内存。如果你两次释放同一个块,可能会发生这种情况;或者如果指针参数在被malloc返回并被传递回free之间的某处被破坏。


动态内存分配与递归函数一起工作正常...前提是你不要内存耗尽。你需要在不需要的时候小心释放记忆。


一般问题是内存碎片 - 当免费模式没有时匹配malloc'的模式,所以小的释放块不是连续的,最终导致大块的malloc失败的情况(尽管有足够的内存),因为没有任何部分的空闲堆空间足够大以满足请求。


感谢您的回复,我不会免费使用,我不会陷入这种情况......怎么办?类似于:

a = malloc(d * sizeof(int));

if(a == NULL){printf(" error");退出(1);}


有效吗?我将在今天下午尝试...


它不起作用......以及分段故障问题?


hi, I''m writing a program a kind of calculator that should work with very big numbers (thousand of digits). So, numbers are rappresented by a struct that contains a pointer to integer, e 2 int for dimension of the array e number of digits.
the pointer is than used for the malloc of the array...
my 2 problems are:

1)some time, when I use my exp function I get the error:

*** glibc detected *** ./a.out: free(): invalid size: 0x0975a328 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e8b604]
/lib/tls/i686/cmov/libc.so.6[0xb7e8eabc]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x95)[0xb7e8f9c5]
./a.out[0x804a8ab]
./a.out[0x804bb52]
./a.out[0x804ba3d]
./a.out[0x804ba3d]
./a.out[0x804ba3d]
./a.out[0x804ba3d]
./a.out[0x804ba3d]
./a.out[0x804ba3d]
./a.out[0x8049e32]
./a.out[0x8048892]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e32775]
./a.out[0x8048401]

and other stuff... can It be becouse it is a recursive function?

the second and biggest problem is that when the number becomes too large (something like 1000 digits) I get a segmentation fault... there are strange problems with arrays of this dimension?

the code is very big but you can find it at:
http://www.speedyshare.com/403525688.html

ps: I''ve deleted the free() functions... It caused too many errors...

thanks for you help, and sorry for my eng...

解决方案

Are you using malloc (or calloc) to dynamically allocate memory; and free to release dynamically allocated memory?

If so, are you trapping the exceptional case when malloc/calloc return an error because there is no available memory to allocate? If not, then you need to do so.

The "free(): invalid size: 0x0975a328" message sounds like you are trying to free a block of memory that wasn''t dynamically allocated. This could happen if you free the same block twice; or if the pointer argument gets corrupted somewhere between being returned by malloc and being passed back to free.

Dynamic memory allocation works fine with recursive functions ... provided that you don''t run out of memory. You want to be careful to free memory as soon as it isn''t needed.

A general problem is memory fragmentation -- when the pattern of free''s doesn''t match the pattern of malloc''s, so small freed blocks are not contiguous, leading eventually to the circumstance where malloc for a large block fails (despite there being more than enough memory) because no portion of free heap space is large enough to satisfy the request.


thanks for the reply, I don''t use free and I''m not trapping that case... how to do it? something like:
a=malloc(d*sizeof(int));
if(a==NULL) {printf("error"); exit(1);}

does it works? I will try this afternoon...


it does''t work.... and for the segmentation fault problem?


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

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