简体   繁体   English

Malloc上的段故障

[英]Seg fault on malloc

I am reading integers from a file, and when I try to grow my array, I am getting a segmentation fault on the second call to growMyArray(struct myArray) , specifically at int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int)); 我正在从文件中读取整数,并且当我尝试扩展数组时,在第二次调用growMyArray(struct myArray)时遇到分段错误,特别是在int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int)); :

struct myArray growMyArray(struct myArray arrayToGrow) {

    arrayToGrow.maxCount *= 2;

    int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int));

    int i;
    for (i = 0; i < arrayToGrow.count; i++)
        grownArray[i] = arrayToGrow.numbers[i];

    free(arrayToGrow.numbers);

    arrayToGrow.numbers = grownArray;

    return arrayToGrow;
}

My structure: 我的结构:

typedef struct myArray {
    int count;
    int maxCount;
    int *numbers;
} myArray;

Reading from input redirection: 从输入重定向读取:

struct myArray getRandomNumbers() {

    struct myArray randomNumbers;
    randomNumbers.count = 0;
    randomNumbers.maxCount = DEFAULT_SIZE;
    randomNumbers.numbers = malloc(randomNumbers.maxCount * sizeof(int));

    while (scanf("%d", &randomNumbers.numbers[randomNumbers.count]) == 1) {

        randomNumbers.count++;

        if (randomNumbers.count > randomNumbers.maxCount)
            randomNumbers = growMyArray(randomNumbers);
    }

    return randomNumbers;
}

I find this particularly odd because growing the array always works the first time but never works the second time. 我发现这特别奇怪,因为增大数组总是在第一次使用时有效,而在第二次时则无效。 I have used multiple values for DEFAULT_SIZE , ranging from 2 to 20000 on a set of test data of size 200000. 我对一组大小为200000的测试数据使用了多个DEFAULT_SIZE值,范围从2到20000。

Is there an apparent reason why I am getting a segmentation fault on the second call to growMyArray , specifically at int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int)); 是否有明显的原因导致我在第二次调用growMyArray时遇到分段错误,特别是在int *grownArray = malloc(arrayToGrow.maxCount * sizeof(int)); ?

You wrote past the end of the array. 您写完了数组的末尾。

while (scanf("%d", &randomNumbers.numbers[randomNumbers.count]) == 1) {

    randomNumbers.count++;

    if (randomNumbers.count > randomNumbers.maxCount)
        randomNumbers = growMyArray(randomNumbers);
}

Because you use > in the test, the if only fires once randomNumbers.count = randomNumbers.maxCount + 1 , ie the scanf writes to randomNumbers.numbers[randomNumbers.maxCount] which is past the end of the array. 因为您在测试中使用> ,所以if仅触发一次randomNumbers.count = randomNumbers.maxCount + 1 ,即scanf写入了randomNumbers.numbers[randomNumbers.maxCount]数组末尾的randomNumbers.numbers[randomNumbers.maxCount]

Therefore, change > to >= in the if statement there. 因此,改变>>=if语句出现。

take care of youre data type 照顾你的数据类型

typedef struct myArray {
    int count;
    int maxCount;
    int *numbers;
} myArray;

this means that count and maxcount are signed integers and they can reach negative values which is not correct for count and can lead also to some memory corruption. 这意味着count和maxcount是带符号的整数,它们可以达到负数,这对于count是不正确的,并且还可能导致某些内存损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM