首页 > 解决方案 > 动态数组初始化无效导致读写错误

问题描述

我正在尝试 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*在数组中使用 a ,但它仍然不适用于 just 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);
}

标签: cmemorymalloc

解决方案


一些分配大小计算不正确: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);
    }
}

推荐阅读