c - 可变大小的对象可能未初始化
问题描述
我得到了错误variable sized object may not be initialized
,我不明白为什么。
有人可以告诉我如何修复这条线吗?
int arr[size] = (int *)(augs->one);
这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
int count = 0;
int cmpfunc(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
struct structure {
int two;
int *one;
};
void *sort(void *augments) {
struct structure *augs = (struct structure*)augments;
int i = 0;
int size = 1;
size = augs->two;
int arr[size] = (int *)(augs->one);
//int *arr = (int *)data;
//printf("sizeof:%d\n", sizeof(arr));
qsort(arr, size, sizeof(int), cmpfunc);
printf("finaloutput:\n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return NULL;
}
int main(int argc, char *argv[]) {
FILE *myFile;
myFile = fopen("data.txt", "r");
// number of lines in file
char charicter;
for (charicter = getc(myFile); charicter != EOF; charicter = getc(myFile)) {
if (charicter == '\n') {
count++;
}
}
printf("count is %d\n", count);
int numberArray[count];
int i = 0;
if ((myFile = fopen("data.txt", "r"))) {
while ((fscanf(myFile, "%d", &numberArray[i]) != EOF)) {
++i;
}
fclose(myFile);
}
assert(argv[1] != NULL);
int num = atoi(argv[1]); //num equals number input
int arrayarray[num - 1][(count / num)];
int idx;
for (i = 0; i < (count); i++) {
printf("numberarray[%d]= %d\n", i, numberArray[i] /*[0],numberArray[i][1]*/);
}
for (i = 1; i < num + 1; i++) {
for (idx = 0; idx < (count / num); idx++) {
arrayarray[i - 1][idx] = numberArray[i * idx];
}
}
///*
for (i = 0; i < ((count / num)); i++) {
printf("arrayarray[0]=%d\n", arrayarray[0][i]);
}
//*/
int lastarray[((count / num) + (count % num))];
for (idx = 0; idx < ((count / num) + (count % num)); idx++) {
lastarray[idx] = numberArray[idx + ((count / num) * (num - 1))];
}
for (i = 0; i < ((((count / num) + (count % num)))); i++) {
printf("lastaray[%d]=%d\n", i, lastarray[i]);
}
//*******************
pthread_t thread_id_arr[num];
for (i = 0; i < num; i++) {
pthread_t tid;
struct structure *augs;
if (i != (num - 1)) {
augs = malloc(sizeof(struct structure) + sizeof(int) + sizeof(int) * num);
(*augs).one = arrayarray[i];
(*augs).two = (count / num);
pthread_create(&tid, NULL, sort, augs);
} else {
(*augs).one = lastarray;
(*augs).two = (count / num) + (count % num);
pthread_create(&tid, NULL, sort, augs);
//pthread_create(&tid, NULL, sort, (void*)lastarray);
}
thread_id_arr[i] = tid;
}
for (i = 0; i < num; i++) {
pthread_join(thread_id_arr[i], NULL);
}
return 0;
}
解决方案
正如其他人指出的那样,您不能像您正在做的那样使用指针初始化可变长度数组。但是,您实际上根本不需要 VLA。改用这个:
int *arr = augs -> one;
您想直接作用于传递到线程中的数组,而不是复制它。
话虽如此,我看到了另一个问题。在产生排序线程的循环中,您不会args
在最后一次循环迭代中分配新的,它会重用args
从前一次迭代中分配的,这可能会导致倒数第二个线程发生灾难。您需要将malloc()
呼叫移至if
.
此外,malloc()
分配的内存比您的线程实际使用的更多。您只需要为结构本身分配足够的内存,而不是为结构后面的任何整数分配足够的内存。
此外,当每个线程使用分配的分配完成时,args
它需要避免内存泄漏。free()
args
推荐阅读
- javascript - 拆分字符串后的数组值“未定义”?
- javascript - 列返回为 0, 0, 0
- python - 如何使用 scipy griddata 插入 32x32 图像的缺失像素值?
- python - Convert list encasing inside data frame row values
- javascript - IE 是否支持“复制”事件
- java - 获取源?如何将按钮的位置保存在数组中?(网格布局)
- javascript - 使用reduce计算数组中每个元素的出现次数
- typescript - 为什么在用作函数参数之前需要将子类型分配给变量?
- php - Laravel:设置数据类型时使用逻辑运算符
- java - 如何在 spring 控制器中处理 db 特定异常