Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
学习C试图通过函数来理解malloc_C_Arrays_Memory_Valgrind - Fatal编程技术网

学习C试图通过函数来理解malloc

学习C试图通过函数来理解malloc,c,arrays,memory,valgrind,C,Arrays,Memory,Valgrind,嘿,我正试图弄明白为什么下面的代码从Valgrind的行中得到无效的大小写错误:array[I-1]=I 我现在真的不明白为什么我的allocate_数组函数不起作用。我尝试了很多东西 还有一些错误,但我只是想先检查一下为什么这一行为false,或者为什么我的数组没有分配 希望你能帮我找出我的错误 #include<stdio.h> #include<stdlib.h> //Programm to check Gaussian function int read_nu

嘿,我正试图弄明白为什么下面的代码从Valgrind的行中得到无效的大小写错误:
array[I-1]=I

我现在真的不明白为什么我的allocate_数组函数不起作用。我尝试了很多东西

还有一些错误,但我只是想先检查一下为什么这一行为false,或者为什么我的数组没有分配

希望你能帮我找出我的错误

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

//Programm to check Gaussian function

int read_number_from_stdin(int* value) {
  printf("Number for the Gaussian Function: ");
  int return_value = scanf("%d", value);
  if (return_value == 0) {
    while (fgetc(stdin) != '\n')
      ;
  }
  if (return_value == EOF) {
    return_value = 0;
  }
  return return_value;
}

int read_number_from_string(char* string, int* value) {
  printf("Reading input...\n");
  int return_value = sscanf(string, "%d", value);
  if (return_value == 0 || return_value == EOF) {
    printf("\t... Error your input is not a Number!\n");
    return_value = 0;
  } else {
    printf("\t... Number %d read and saved.\n", *value);
  }
  return return_value;
}

int* allocate_array(int* size) //allocating memory for the array
{
  int* result = (int*) malloc(sizeof(int) * (*size));
  return result;
}

void initialize_array(int array[], int size) {
  for (int i = 0; i < size; i++) {
    array[i] = i+1;
  }
}

int compute_sum_and_place_in_first_elem(int array[], int* size) {

  int sum_array = 0;
  for (int i = 0; i < *size; i++) {
    sum_array += array[i];
  }

return sum_array;

}

void free_memory(int array[], int* N) {
  free(array);
  free(N);
}

int main(int argc, char* argv[]) {
  int* N = malloc(sizeof(int));
  if (argc == 1) {
    while (read_number_from_stdin(N) != 1)
      ;
  } else if (argc == 2) {
    if (read_number_from_string(argv[1], N) == 0) {
      printf("Error: No valid number!\n", argv[1]);
      return -1;
    }
  } else {
    printf("No valid number!\n");
    return -1;
  }

  int* array = allocate_array(N); //allocate via function

  initialize_array(array, *N); //initialize the array up to n



  int result = compute_sum_and_place_in_first_elem(array, N); 

  int result_gauss = ((*N + 1) * (*N) / 2);
  if (result == result_gauss) {
    printf("Gauss was right your calculations match with his function");
  } else {
    printf(
        "\nGauss was not right!\n" 
        "The summ of %d is %d and therefore not equal to(%d+1)*%d/2\n\n",
        *N, result, *N, *N);
  }

  //free memory
  free_memory(array, N);
}
#包括
#包括
//高斯函数检验程序
从标准输入中读取整数(整数*值){
printf(“高斯函数的数值:”);
int return_value=scanf(“%d”,value);
if(返回值=0){
while(fgetc(stdin)!='\n')
;
}
if(返回值=EOF){
返回_值=0;
}
返回_值;
}
从字符串读取整数(字符*字符串,整数*值){
printf(“读取输入…\n”);
int return_value=sscanf(字符串,“%d”,值);
如果(返回值==0 | |返回值==EOF){
printf(“\t…错误您的输入不是数字!\n”);
返回_值=0;
}否则{
printf(“\t…读取并保存了编号%d。\n”,*值);
}
返回_值;
}
int*allocate\u数组(int*size)//为数组分配内存
{
int*result=(int*)malloc(sizeof(int)*(*size));
返回结果;
}
无效初始化_数组(int数组[],int大小){
对于(int i=0;i
如我所见,对于
初始化数组()
函数,对于
for
循环,第一次迭代,
I
0
,您正在执行

   array[i-1] = i;
也就是说

   array [-1] = ....
这是违法的

可以使用基于0的索引方案的默认C-array属性修复此问题。差不多

    for(int i = 0; i < size; ++i)
    {
        array[i] = i;
    }
for(int i=0;i
i
等于0时,考虑
数组[i-1]
。然后看看以
返回开始的
空闲内存
。我们将如何到达
免费
-呼叫?请注意
while(fgetc(stdin)!='\n')
是文件末尾的无限循环。请注意,
i BTW-+1用于使用valgrindNote,也可以将
更改为(inti=0;正如您所看到的,我改变了一切。我仍然得到以下Valgrind错误:泄漏摘要:==3995==肯定丢失:0块中的0字节==3995==间接丢失:0块中的0字节==3995==可能丢失:3块中的72字节==3995==仍然可访问:6块中的200字节==3995==抑制:6块中的21894字节)154块==3995==可到达块(找到指针的块)未显示。