我有这样的代码里面一个malloc时出了问题,ç的malloc断言
/*function starts*/
if(NULL==(partial_results=(bignum_t**)malloc(sizeof(bignum_t*)*long_op2))){
return NULL;
}
/*to initialize all the members of the array*/
for(i=0;i<long_op2;i++){
(*(partial_results+i))=NULL;
}
for(i=long_op2-1;i>=0;i--){
digit2=op2->digits[i]-48;
count++;
carry=0;
if(count==1){
count2=0;
}else{
count2=count-1;
}
/*the next malloc is the one that fails*/
if(NULL==(*(partial_results+(count-1))=(bignum_t*)malloc(sizeof(bignum_t)))){
return NULL;
}
/*after this the codes continues, but everything from here is ok an it isn't causing any problem*/
这里的事情,我试图创建long_op2元素的数组(9),所以在第一个malloc我创建了一个9 bignum_t指针数组。然后,在for内部,我尝试为数组的每个成员创建一个bignum_t结构。当long_op2小于或等于6时,我没有问题,但是当它是7或更多时,第一个malloc(假设创建指针的那个malloc)不起作用。我发现了错误,
tp3: malloc.c:2372: sysmalloc: Assertion
的事情是,我不是要写比创建数组的数量比较多,因为数量到达8最大,如果long_op2是9,所以这是好。 另一件奇怪的事情是,当我使用Valgrind运行程序时,它确实有效!
PD:这是我的第一个问题,所以如果我犯了什么错误,我很抱歉。
PD2:这是程序的工作方式。
980581618*215129902
long_op1 & long_op2 9 9
for with: i, count-1 8 0
doing malloc malloc done
for with: i, count-1 7 1
doing malloc malloc done
for with: i, count-1 6 2
doing malloc malloc done
for with: i, count-1 5 3
doing malloc malloc done
for with: i, count-1 4 4
doing malloc malloc done
for with: i, count-1 3 5
doing malloc malloc done
for with: i, count-1 2 6
doing malloc malloc done
for with: i, count-1 1 7
doing malloc malloc done
for with: i, count-1 0 8
doing malloc
tp3: malloc.c:2372: sysmalloc: Assertion ...
不要投malloc的 - 见http://stackoverflow.com/questions/605845/do-i-cast-the-结果是malloc –
这行代码如果(NULL ==(*(partial_results +(count-1))=(bignum_t *)malloc(sizeof(bignum_t)))){'试图做太多走。 ' –
'if(count == 1){ count2 = 0; } else { count2 = count-1; “我喜欢它! – wildplasser