2009-12-04 102 views
0

所附以下C代码在运行时提供错误总结:malloc.c:3074 - 为什么这个代码将导致错误

summary: malloc.c:3074: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. 

在每个任意每个呼叫对的malloc(21); (见下文)。有人可以解释为什么?我已经尝试过所有我能想到的事情,但仍然失败。

文件:summary.c

/* 
* File: summary.c 
* Author: Maxim Veksler 
* 
* Created on December 4, 2009, 3:09 AM 
*/ 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "manipulation.h" 

/* 
* Main for Maman 12, task 1 : Array summary utility 
*/ 
int main(int argc, char** argv) { 
    /*STUB*/ 
    char str[100]; 
    strcpy(str, "3 5 234 11 77 44 5"); 
    /*STUB*/ 

    int resultsSize; 
    int* results; 
    int* aggregated; 

    results = parseInput(str, &resultsSize); 
    aggregatedArray((int*)NULL, (int)NULL); 


    return 0; 
} 

文件manipulation.c

/* 
    * File: manipulation.c 
    * Author: Maxim Veksler 
    * 
    * Created on December 4, 2009, 3:09 AM 
    */ 

    #include <stdlib.h> 
    #include <string.h> 
    #include <stdio.h> 

    /* 
    * Parse the input from user, dynamically allocate memory to the maximum 
    * possible requirment. Then convert the array of string tokens into an 
    * simple array of integers. 
    */ 
    int* parseInput(char* input, int* nOfResults) { 
     /* Allocate memory by the maximum possibly required size for int... */ 
     int *results = (int*) malloc(strlen(input)); 

     int* insertIterator = results; 
     char* pch; 


     /* We trash the user input, but it's cool - Worthless as usual. */ 
     pch = strtok(input,"\t ,.-"); 
     *nOfResults = 0; 

     while(pch != NULL) { 
     (*nOfResults)++; 

     *insertIterator = atoi(pch); 
     insertIterator++; 
     pch = strtok(NULL, "\t ,.-"); 
     } 

     return results; 
    } 


    /* 
    * Summary the values given in the int array and return adress to new array 
    * containing an increasin sum of the values. 
    */ 
    int* aggregatedArray(int* inputArray, int size) { 
     int* results; 
     malloc(20); 
     malloc(21); 
    } 

编辑请考虑此代码是带到这里显示的问题一个精简版。我删除了所有不相关的部分。

回答

5

编辑: woah,我刚刚意识到你的代码中有一个很糟糕的逻辑错误。这不仅仅是泄漏,你也有缓冲区溢出!

int *results = (int*) malloc(strlen(input)); 

这将分配18字节(输入的lengh)并治疗等的int的阵列,这意味着你可以在它适合18/sizeof(int)int秒。假设通常的x86大小,这意味着你只能适应(18/4)== 4.5整数!稍后,您的代码将写入比数组更多的代码。大错误。

要解决此问题,您应该使用realloc。事情是这样的:

int *results = malloc(sizeof(int)); 
int result_size = 1; 
int result_count = 0; 

while(/*whatever*/) { 
    /* ok, i want to add an element to results */ 
    if(result_count == result_size - 1) { 
     int *p = realloc(results, (result_size + 1) * sizeof(int)); 
     if(!p) { 
      free(results); 
      return NULL; /* no more memory! */ 
     } 
     results = p; 
     result_size++; 
    } 
    results[result_count++] = /*value*/ 
} 
return results; 

它泄漏,因为你有2个malloc S的你不存储任何地方的结果。这使得这些调用返回的指针不可能为free

事实上,我不确定aggregatedArray应该是在做什么,此刻,它什么也没做,但泄漏。

此外,您有results = parseInput(str, &resultsSize);其中parseInput返回一个malloc ED指针。如果您不再需要此功能(可能恰好在拨打aggregatedArray之后),稍后应该有一个free(results);

最后,作为一个附注。我猜想aggregatedArray((int*)NULL, (int)NULL);实际上应该是aggregatedArray(results, resultsSize); :-P。

+0

等等家伙,这是一个简化版本的代码。 我已经缩小到最小重现错误。 我知道这是目前有用的...期望重现问题 – 2009-12-04 16:36:37

+3

你问**为什么**代码泄漏,你有3'malloc'调用和零'免费'调用。这就是为什么...... – 2009-12-04 16:38:13

+0

确定再次:此代码仅用于显示如何再现问题。它正在发展中,并已被剥离到显示问题所需的最低限度。 请把所有其他的笔记(这是非常正确,但不相关)放在一边 – 2009-12-04 16:41:10

0

在函数“aggregatedArray”中,您没有将从malloc返回的指针分配给变量,以便稍后释放它们。他们在太空中迷失了!

1

下面的语句分配的18个字节(“3 5 234 11 77 44 5”)

int *results = (int*) malloc(strlen(input)); 

,但你把整数到内存区域内存...所以它不会持续很长时间,当你会有usedup所有空间...所以这绝对是错误的做。

更多..你没有使用任何免费()来电..所以这也是一个问题太..

+0

oops ... Evan Teran已经回复了类似但更好的.. – ashishsony 2009-12-04 16:51:16

相关问题