【问题标题】:Learning C trying to figure out this malloc stuff via functions学习 C 试图通过函数找出这个 malloc 的东西
【发布时间】:2018-01-11 15:11:43
【问题描述】:

嘿,我想弄清楚为什么下面的代码会从 Valgrind 那里得到无效写入大小错误:array[i-1] = I;

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

还有几个错误,但我只是想先检查一下为什么这条线是错误的,或者为什么我的数组没有分配。

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

#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);
}

【问题讨论】:

  • i 等于0 时考虑array[i-1]
  • 然后看看free_memory,它return开头。我们如何联系free-calls?
  • 请注意,while (fgetc(stdin) != '\n'); 是文件末尾的无限循环。
  • 请注意,i &lt;= size 总是为有经验的 C 编码器敲响警钟。 99% 的时间你的意思是i &lt; size。提醒:包含 3 个条目的数组的索引为 0,1,2
  • 顺便说一句 - +1 使用 valgrind

标签: c arrays memory valgrind


【解决方案1】:

正如我所见,对于 initialize_array() 函数,对于 for 循环,第一次迭代,i0,而你正在执行

   array[i-1] = i;

翻译成

   array [-1] = ....

这是非法的。

您可以使用基于 0 的索引方案的默认 C 数组属性来解决此问题。类似的东西

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

【讨论】:

  • 还要注意从 for (int i = 0; i &lt;= size; ++i)for (int i = 0; i &lt; size; i++) 的更改,以避免写入超出数组末尾。
  • 如你所见,我改变了一切。我仍然收到以下 Valgrind 错误:泄漏摘要:==3995== 肯定丢失:0 个块中的 0 个字节 ==3995== 间接丢失:0 个块中的 0 个字节 ==3995== 可能丢失:3 个块中的 72 个字节= =3995== 仍然可访问:6 个块中的 200 个字节 ==3995== 抑制:154 个块中的 21,894 个字节 ==3995== 可访问块(找到指针的块)未显示。
猜你喜欢
  • 2015-05-08
  • 1970-01-01
  • 2021-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
相关资源
最近更新 更多