c - 动态数组初始化无效导致读写错误
问题描述
我正在尝试 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);
}
解决方案
一些分配大小计算不正确: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);
}
}
推荐阅读
- angular - 当我们在订阅函数中设置变量时更新Angular6中的视图
- ios - 如何在滚动视图中添加所有(标签、按钮、子视图)?
- javascript - 使用 cors-anywhere.herokuapp 下载图片
- java - 不兼容的类型:类
不能转换为类 - >
- javascript - 空文本框的Javascript ScrollToView不起作用-Chrome
- solr - Solr-Lucene 支持同一字段中的多种语言标记
- python - OpenCV中的渐变方向
- javascript - 有没有办法为浏览器指定最小和最大缩放级别?
- java - 在 Java 中创建新的自定义对象与将多个值设置为 Null 的同一对象
- java - 我在 leetcode 上让数组索引越界