动态数组的无效初始化会导致读写错误

时间:2020-03-25 06:21:13

标签: c memory malloc

我正在尝试分配结构数组。

typedef struct {
    long int val;
    long int time;
    long int last_used;
} pair;

所以我主要是

pair **fifoVM = (pair **) malloc(sizeof(pair *) * framecount);
pair **fifop1VM = (pair **) malloc(sizeof(pair *) * framecount + 1);
pair **fifop2VM = (pair **) malloc(sizeof(pair *) * framecount + 2);
pair **LRUVM = (pair **) malloc(sizeof(pair *) * framecount);

然后我用

初始化所有对
void init(pair **frames, int size) {
    for (int i = 0; i < size; i++) {
        frames[i] = (pair *) malloc(sizeof(pair));
        frames[i]->val = -1;
        frames[i]->last_used = TIME_VAL;
        frames[i]->time = TIME_VAL++;
    }
}

但是当我尝试重新分配它时,我从Valgrind收到了一个错误错误。

我最初以为问题出在数组中使用pair*,但仅pair仍然无法解决。我还认为可能是pair返回时init()超出范围,但这也说明是正确的,因为它只会释放包含指针的变量。

由于某些奇怪的原因,LRUVM是唯一崩溃的数组,即使它是最后一个。

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

//since time.h only has millisecond resolution,
//I need to simulate time
int TIME_VAL = 0;

typedef struct {
    long int val;
    long int time;
    long int last_used;
} pair;

//Allocate the pairs for a given array
void init(pair **frames, int size) {
    //iterate through array
    for (int i = 0; i < size; i++) {
        //allocate memory and assign
        frames[i] = (pair *) malloc(sizeof(pair));
        frames[i]->val = -1;
        frames[i]->last_used = TIME_VAL;
        frames[i]->time = TIME_VAL++;
    }
}

int main(int argc, char **argv) {
    //Command line arguments
    int framecount = atoi(argv[1]);
    int x = atoi(argv[2]);
    int NUM_ACCESSES = atoi(argv[3]);
    int NUM_ITERATIONS = atoi(argv[4]);

    for (int i = 0; i < NUM_ITERATIONS; i++) {

        //Allocate Arrays
        pair **fifoVM = (pair **) malloc(sizeof(pair *) * framecount);
        pair **fifop1VM = (pair **) malloc(sizeof(pair *) * framecount + 1);
        pair **fifop2VM = (pair **) malloc(sizeof(pair *) * framecount + 2);
        pair **LRUVM = (pair **) malloc(sizeof(pair *) * framecount);

        //initialize all of the pairs in the arrays
        init(fifoVM, framecount);
        init(fifop1VM, framecount + 1);
        init(fifop2VM, framecount + 2);
        init(LRUVM, framecount);

        //deallocate arrays
        freeList(fifoVM, framecount);
        freeList(fifop1VM, framecount + 1);
        freeList(fifop2VM, framecount + 2);
        freeList(LRUVM, framecount);
    }
}

void freeList(pair **vm, int framecount) {
    for (int i = 0; i < framecount; i++) {
        free(vm[i]);
    }
    free(vm);
}

1 个答案:

答案 0 :(得分:1)

某些分配大小的计算不正确:malloc(sizeof(pair *) * framecount + 1)应该是:

malloc(sizeof(pair *) * (framecount + 1))

请注意,您的数据结构似乎没有充分的理由是间接的。为什么不分配结构数组而不是指向分别分配的结构的指针数组?

这是一个简化的版本:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>

//since time.h only has millisecond resolution,
//I need to simulate time
int TIME_VAL = 0;

typedef struct {
    long int val;
    long int time;
    long int last_used;
} pair;

//Allocate the pairs for a given array
void init(pair *frames, int size) {
    for (int i = 0; i < size; i++) {
        frames[i].val = -1;
        frames[i].last_used = TIME_VAL;
        frames[i].time = TIME_VAL++;
    }
}

int main(int argc, char **argv) {
    //Command line arguments
    if (argc < 5) return 1;

    int framecount = atoi(argv[1]);
    int x = atoi(argv[2]);
    int num_accesses = atoi(argv[3]);
    int num_iterations = atoi(argv[4]);

    for (int i = 0; i < num_iterations; i++) {
        //Allocate Arrays
        pair *fifoVM = calloc(sizeof(pair), framecount);
        pair *fifop1VM = calloc(sizeof(pair), framecount + 1);
        pair *fifop2VM = calloc(sizeof(pair), framecount + 2);
        pair *LRUVM = calloc(sizeof(pair), framecount);

        if (fifoVM && fifop1VM && fifop2VM && LRUVM) {
            //initialize all of the pairs in the arrays
            init(fifoVM, framecount);
            init(fifop1VM, framecount + 1);
            init(fifop2VM, framecount + 2);
            init(LRUVM, framecount);

            //...
        }
        //deallocate arrays
        free(fifoVM);
        free(fifop1VM);
        free(fifop2VM);
        free(LRUVM);
    }
}