Calloc在不同的计算机上的行为不同

Calloc在不同的计算机上的行为不同,c,calloc,C,Calloc,我用C编写了以下代码: #包括 #包括 int func(int n,int*数据){ 对于(inti=1;i这个程序的行为是未定义的。如果它在任何一台计算机上打印出你期望的内容,那纯粹是巧合 int * array = calloc(1, sizeof(int)); func(numb, array); 您在此处通过值传递array,这意味着func对其所做的任何操作都对外部作用域不可见。请注意realloc不保证返回相同的指针,因此您需要预计array将在调用过程中更改。请尝试传递

我用C编写了以下代码:

#包括
#包括
int func(int n,int*数据){

对于(inti=1;i这个程序的行为是未定义的。如果它在任何一台计算机上打印出你期望的内容,那纯粹是巧合

  int * array = calloc(1, sizeof(int));
  func(numb, array);
您在此处通过值传递
array
,这意味着
func
对其所做的任何操作都对外部作用域不可见。请注意
realloc
不保证返回相同的指针,因此您需要预计
array
将在调用过程中更改。请尝试传递指向
array
inst的指针ead

int func(int n, int ** data){
  for(int i = 1; i <= n; i++){
    *data = realloc(*data, i * sizeof(int));
    (*data)[i - 1] = i;
  }
  return 0;
}

...

    func(numb, &array);
int func(int n,int**data){

对于(inti=1;i这个程序的行为是未定义的。如果它在任何一台计算机上打印出你期望的内容,那纯粹是巧合

  int * array = calloc(1, sizeof(int));
  func(numb, array);
您在此处通过值传递
array
,这意味着
func
对其所做的任何操作都对外部作用域不可见。请注意
realloc
不保证返回相同的指针,因此您需要预计
array
将在调用过程中更改。请尝试传递指向
array
inst的指针ead

int func(int n, int ** data){
  for(int i = 1; i <= n; i++){
    *data = realloc(*data, i * sizeof(int));
    (*data)[i - 1] = i;
  }
  return 0;
}

...

    func(numb, &array);
int func(int n,int**data){

对于(int i=1;i下面的Ian Abbot注释,您可以做的最小更正是让
func()
返回新分配的块:

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

int * func(int n, int * data)  // <-- Notice the * in the return value
{
  for(int i = 1; i <= n; i++){
    data = realloc(data, i * sizeof(int));
    data[i - 1] = i;
  }
  return data; // <-- We return a pointer to the newly allocated memory block
}

int main(void)
{
  int numb = 10;
  int * array = calloc(1, sizeof(int));
  array = func(numb, array); // <-- reassign to array (same pattern as realloc)

  for(int j = 0; j < numb; j++){
    printf("%d \t", array[j]);
  }

  free(array);
  return 0;
}
#包括
#包括

int*func(int n,int*data)//在Ian Abbot注释之后,您可以做的最小更正是让
func()
返回新分配的块:

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

int * func(int n, int * data)  // <-- Notice the * in the return value
{
  for(int i = 1; i <= n; i++){
    data = realloc(data, i * sizeof(int));
    data[i - 1] = i;
  }
  return data; // <-- We return a pointer to the newly allocated memory block
}

int main(void)
{
  int numb = 10;
  int * array = calloc(1, sizeof(int));
  array = func(numb, array); // <-- reassign to array (same pattern as realloc)

  for(int j = 0; j < numb; j++){
    printf("%d \t", array[j]);
  }

  free(array);
  return 0;
}
#包括
#包括

int*func(int n,int*data)/您的程序具有未定义的行为。因此,即使在同一台计算机中,它也可能具有不同的行为……让我解释一下:

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

int func(int n, int * data){
  for(int i = 1; i <= n; i++){
    // HERE, YOU CHANGE THE VALUE OF data FOR THE NEW
    // VALUE RETURNED BY REALLOC.  As the value ov array
    // was copied on entry, it is not known outside of
    // func()
    data = realloc(data, i * sizeof(int));
    data[i - 1] = i;
  }
  return 0;
  // AFTER THIS POINT THE ORIGINAL (the passed in value
  // to func() is not valid anymore
}

int main(void){

  int numb = 10;
  int * array = calloc(1, sizeof(int));
  func(numb, array);
  // at this point, the value of array is not valid,
  // in case the value realloc() has returned inside
  // func() has changed.

  for(int j = 0; j < numb; j++){
    printf("%d \t", array[j]);
  }

  // THIS IS ALSO INCORRECT.
  free(array);
  return 0;
}

您的程序有未定义的行为。因此,即使在同一台计算机上,它也可能有不同的行为……让我解释一下:

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

int func(int n, int * data){
  for(int i = 1; i <= n; i++){
    // HERE, YOU CHANGE THE VALUE OF data FOR THE NEW
    // VALUE RETURNED BY REALLOC.  As the value ov array
    // was copied on entry, it is not known outside of
    // func()
    data = realloc(data, i * sizeof(int));
    data[i - 1] = i;
  }
  return 0;
  // AFTER THIS POINT THE ORIGINAL (the passed in value
  // to func() is not valid anymore
}

int main(void){

  int numb = 10;
  int * array = calloc(1, sizeof(int));
  func(numb, array);
  // at this point, the value of array is not valid,
  // in case the value realloc() has returned inside
  // func() has changed.

  for(int j = 0; j < numb; j++){
    printf("%d \t", array[j]);
  }

  // THIS IS ALSO INCORRECT.
  free(array);
  return 0;
}

我猜在其中一台计算机上,
realloc
将内存块留在了原来的位置,而在另一台计算机上,它被移动了。您对函数内部的
data
所做的更改不会影响函数外部的
array
,您需要制作一个
data
指针,指向int(
int**data
)。看起来没问题。什么是“不同的计算机”?@IanAbbott答对了!我猜在其中一台计算机上,
realloc
将内存块留在了相同的位置,而在另一台计算机上它被移动了。您对函数内部的
数据所做的更改不会影响函数外部的
array
,您需要一个
data
指向int(
int**data
)的指针。在我看来还可以。什么是“不同的计算机”?@IanAbbott说对了!