以下C代码的附接时运行提供了错误

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);
    }

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

有帮助吗?

解决方案

编辑:哇,我才意识到,你在你的代码有一个非常糟糕的逻辑错误。这不仅仅是漏水,你有一个缓冲区溢出呢!

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

这将分配的 18字节(输入的lengh)并治疗等int的阵列,这意味着你可以在它适合18 / sizeof(int) ints。假设通常的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个mallocs你不随地存储的结果。这使得不可能free指针那些调用返回。

其实,我不知道应该怎样aggregatedArray要真正做,此刻,它什么都不做的泄漏。

此外,已results = parseInput(str, &resultsSize);其中parseInput返回malloced指针。你应该稍后当你不再需要这个(可能是free(results);调用后右)有aggregatedArray

最后,作为一个方面说明。我猜想,aggregatedArray((int*)NULL, (int)NULL);其实应该aggregatedArray(results, resultsSize); :-P。

其他提示

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

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

但你把整数到内存区...所以它不会持续很长时间,当你将有usedup所有空间...因此多数民众赞成绝对错误的事情。

进一步不使用起来..任何free()的调用..所以这将是一个问题太..

在函数“aggregatedArray”您没有分配指针从malloc返回给一个变量,以便它们可以在稍后释放。他们失去了在空间!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top