在存储器块求和变量

问题描述 投票:0回答:1

/ I试图动态地创建一个存储器中,然后总结值作为被输入我将在阵列做,但我不理解逻辑error./的原因

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

int main() {

    int numOfElements,*intPtr,sum = 0;
    scanf("%d\n",&numOfElements);
    intPtr = (int *) malloc(numOfElements * sizeof(int));
    int *endOfInt = intPtr +numOfElements-1;
    for(;intPtr<=endOfInt;intPtr++){
        scanf("%d", intPtr);
        sum += *intPtr;
    }
    printf("\n%d\n",sum);
    free(intPtr);


    return 0;
}
pointers
1个回答
0
投票

在此行for(;intPtr<=endOfInt;intPtr++){你增加你的指针variabl IntPtr的。在此行中free(intPtr);你尝试释放以上分配的内存。 ,然而IntPtr并没有说在这一点上返回给你的malloc相同的值。

一般来说,地址传递给free()必须是完全相同的这是通过malloc()返回。

© www.soinside.com 2019 - 2024. All rights reserved.