C zsh:所有程序运行后是否会出现中止消息?

C zsh:所有程序运行后是否会出现中止消息?,c,memory-management,C,Memory Management,所以我运行这个代码 #include <stdio.h> #define ARRAYSIZE 17 #define NUMTHREADS 4 struct ThreadData { int start, stop; int *array; }; void squarer(struct ThreadData *data) { int start = data->start; int stop = data->stop; int *array = da

所以我运行这个代码

#include <stdio.h>
#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
  int start, stop;
  int *array;
};

void squarer(struct ThreadData *data) {
  int start = data->start;
  int stop = data->stop;
  int *array = data->array;
  int i;
  for (i=start; i<stop;i++) {
    array[i] = i*i;
  }
}

int main(void) {
  int array[ARRAYSIZE];
  int i, start, stop;
  struct ThreadData *data;
  data->array = array;
  int tasksPerThread = (ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;


  for (i=0; i < NUMTHREADS; i++) {
    start = i * tasksPerThread;
    stop = (i+1) * tasksPerThread;
    data->start = start;
    data->stop = stop;

    if (stop > ARRAYSIZE) {
      stop = ARRAYSIZE;
    }
    squarer(data);
  }
  for (i=0; i < ARRAYSIZE; i++) {
    printf("%d\n", i);
  }
  return 0;
}
#包括
#定义数组化17
#定义NUMTHREADS 4
结构线程数据{
int启动、停止;
int*数组;
};
void squarer(结构线程数据*数据){
int start=数据->开始;
int stop=数据->停止;
int*array=data->array;
int i;
对于(i=开始;iarray=数组;
int tasksPerThread=(ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;
对于(i=0;i开始=开始;
数据->停止=停止;
如果(停止>排列){
停止=排列;
}
平方器(数据);
}
对于(i=0;i

出于某种原因,如果在我运行它时给我错误消息
zsh:abort./thread
。有趣的是,它直到数组完全打印后才会中止?我不明白为什么会这样,它在执行完所有操作后肯定无法访问越界内存?请注意,最初printf循环打印的是out数组项,但我将其更改为bug check,它仍然会给我相同的运行时错误。

您的第3行不正常,即未定义:

struct ThreadData *data;
data->array = array;
因为数据没有在第二行初始化,对吗?
数据
指向任何地方


可能会有帮助。

在函数
main
中,变量
数据
未初始化。无论何时使用该变量,都很可能执行内存访问冲突,因此发生的一切几乎都是“运气问题”

您必须首先修复它

struct ThreadData* data = (struct ThreadData*)malloc(sizeof(struct ThreadData));
...
free(data); // at the end of the program
另一种选择(在本例中更好)是使用静态分配的实例:

struct ThreadData data;
...
squarer(&data);
现在,数组的问题在于索引。

更改:

array[i] = i*i
致:


顺便说一句,我强烈建议您在
ThreadData
结构中声明
int-array[ARRAYSIZE]
,或者在使用此结构的每个函数中动态分配它(而不是将
array
设置为指向函数
main
中的本地数组)。当您开始在多个地方使用此结构时,将成为一个问题。

因此我的问题基本上是停止变量的边缘情况条件定义如下,其中边缘情况被传递到结构中,因此停止值可能比数组大

#include <stdio.h>
#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
  int start, stop;
  int *array;
};

void squarer(struct ThreadData *data) {
  int start = data->start;
  int stop = data->stop;
  int *array = data->array;
  int i;
  for (i=start; i<stop;i++) {
    printf("%d %d\n",start,stop);
    array[i] = i*i;
  }
}

int main(void) {
  int array[ARRAYSIZE];
  int i, start, stop;
  struct ThreadData data;
  data.array = array;
  int tasksPerThread = (ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;


  for (i=0; i < NUMTHREADS; i++) {
    start = i * tasksPerThread;
    stop = (i+1) * tasksPerThread;
    data.start = start;
    if (stop >= ARRAYSIZE) {
      break;
    }
    data.stop = stop;

    squarer(&data);
  }
  for (i=0; i < ARRAYSIZE; i++) {
    printf("%d\n", i);
  }
 return 0;
}
#包括
#定义数组化17
#定义NUMTHREADS 4
结构线程数据{
int启动、停止;
int*数组;
};
void squarer(结构线程数据*数据){
int start=数据->开始;
int stop=数据->停止;
int*array=data->array;
int i;
for(i=start;i=ARRAYSIZE){
打破
}
data.stop=停止;
平方器(和数据);
}
对于(i=0;i

我从其他一些答案中借用了代码

我尝试了你的两次编辑,但仍然得到相同的错误。是否还有其他原因导致我出现此错误?你的解决方案不正确,但确实帮助我找到了正确的解决方案,因此,谢谢!立即发布问题的解决方案。不确定你的解决方案是什么,但你仍然必须重新发布e
array[i]
array[i-start]
,正如我在回答中提到的。您的解决方案可以防止内存错误,但最终没有得到正确的解决方案。正如我在问题中提到的,数组项是它应该计算的数据,而不是I值。因此,您的解决方案打印的结果相同,但数组中的值不正确。
array[i-start] = i*i
#include <stdio.h>
#define ARRAYSIZE 17
#define NUMTHREADS 4

struct ThreadData {
  int start, stop;
  int *array;
};

void squarer(struct ThreadData *data) {
  int start = data->start;
  int stop = data->stop;
  int *array = data->array;
  int i;
  for (i=start; i<stop;i++) {
    printf("%d %d\n",start,stop);
    array[i] = i*i;
  }
}

int main(void) {
  int array[ARRAYSIZE];
  int i, start, stop;
  struct ThreadData data;
  data.array = array;
  int tasksPerThread = (ARRAYSIZE+NUMTHREADS-1)/NUMTHREADS;


  for (i=0; i < NUMTHREADS; i++) {
    start = i * tasksPerThread;
    stop = (i+1) * tasksPerThread;
    data.start = start;
    if (stop >= ARRAYSIZE) {
      break;
    }
    data.stop = stop;

    squarer(&data);
  }
  for (i=0; i < ARRAYSIZE; i++) {
    printf("%d\n", i);
  }
 return 0;
}