c - C 中两个动态增长数组的指针复制

标签 c arrays pointers malloc lvalue

更新:我想我已经回答了我自己的问题,除了一些可能的内存泄漏问题。

原始问题在此,答案如下。

背景:我正在做一些数值计算,但我几乎从不使用需要我自己管理内存的语言。我现在正在将某些内容通过管道传输到 C,并且遇到了(我认为)指针引用问题。

我有两个 double 组,它们在 while 循环中增长,并且在每次迭代时,我想释放较小的较旧数组的内存,将“旧”设置为指向较新的数组,然后设置“new' 指向更大的内存块。

环顾四周后,似乎我应该使用指向指针的指针,所以我尝试了这个,但遇到了“需要左值作为一元‘&’操作数”错误。

我从以下开始:

double ** oldarray;
oldarray = &malloc(1*sizeof(double));
double ** newarray;
newarray = &malloc(2*sizeof(double));

这些初始化给我一个“需要左值作为一元‘&’操作数”错误,我不确定是否应该用

替换它
*oldarray = (double *) malloc(1*sizeof(double));

当我这样做时,我可以编译一个简单的程序(它只有上面的行并返回 0),但我遇到了段错误。

程序的其余部分如下:

while ( <some condition> ) {

// Do a lot of processing, most of which is updating
// the values in newarray using old array and other newarray values. 

// Now I'm exiting the loop, and growing and reset ing arrays.
free(*oldarray)        // I want to free the memory used by the smaller, older array.
*oldarray = *newarray  // Then I want oldarray to point to the larger, newer array.
newarray = &malloc( <previous size + 1>*sizeof(double))
}

因此,我希望在每次迭代时,使用其自身和旧的大小为 (n-1) 的数组来更新大小为 (n) 的数组。然后我想释放大小为(n-1)的数组的内存,设置'oldarray'指向我刚刚创建的数组,然后设置'newarray'指向一个大小为(n+1)的新 block ) double 。

我真的需要使用指向指针的指针吗?我认为我的主要问题是,当我将旧设置为新时,它们共享一个指针,然后我不知道如何将新设置为新数组。我认为使用指向指针的指针可以让我摆脱这个困境,但是,我不确定,并且我仍然遇到指向指针的指针的左值错误。

我已经查看了C dynamically growing array以及其他一些堆栈问题,并且已经在谷歌上搜索指针、malloc 和复制了大约半天。

谢谢!

这是我自己的答案

我现在有了一个可行的解决方案。我唯一担心的是它可能包含一些内存泄漏。

使用 realloc() 可以工作,而且我还需要小心确保我只释放使用 malloc 或 realloc 初始化的指针,而不是使用 double * oldarray; 初始化的指针。 .

工作版本如下:

double * olddiagonal = (double *) malloc(sizeof(double));
olddiagonal[0] = otherfunction(otherstuff);
int iter = 1;

// A bunch of other stuff
while (<error tolerance condition>) {

  double * new diagonal = (double *) malloc((iter+1)*sizeof(double));
  newdiagonal[0] = otherfunction(moreotherstuff);

  // Then I do a bunch of things and fill in the values of new diagonal using
  // its other values and the values in olddiagonal.

  // To finish, I free the old stuff, and use realloc to point old to new.    
  free(olddiagonal);
  olddiagonal = (double *) realloc(newdiagonal, sizeof(double) * (iter+1));

  iter++
}

这似乎适合我的目的。我唯一担心的是可能存在内存泄漏,但目前,它表现良好并获得了正确的值。

最佳答案

以下是一些解释:

<小时/>
double ** oldarray;
oldarray = &malloc(1*sizeof(double));

是错误的,因为您没有将 malloc() 的结果存储在任何地方,并且由于它没有存储在任何地方,所以您无法获取它的地址。您可以通过添加中间变量来获得您想要的效果:

double* intermediatePointer;
double** oldarray = &intermediatePointer;
intermediatePointer = malloc(1*sizeof(*intermediatePointer);

oldarray现在是指向intermediatePointer内存位置的指针,它依次指向分配的内存slap。

<小时/>
*oldarray = (double *) malloc(1*sizeof(double));

是错误的,因为您正在取消引用未初始化的指针。当您使用 double** oldarray; 声明 oldarray 时,您仅为一个指针保留内存,而不是为该指针应该指向的任何内容保留内存(内存保留是独立的)指针指向的内容!)。您在该指针变量中找到的值是未定义的,因此当您将某些内容分配给 *oldarray 时,您完全无法控制要写入的内存地址。

每当声明一个指针时,必须在取消引用该指针之前初始化该指针:

int* foo;
*foo = 7;    //This is always a bug.

int bar;
int* baz = &bar;   //Make sure the pointer points to something sensible.
*baz = 7;    //OK.
<小时/>

您的答案代码确实正确。不过,它的风格还可以改进:

组合

int iter = 1;
while (<error tolerance condition>) {
    ...
    iter++
}

调用了for()循环,它将循环变量的定义和递增封装到循环控制语句中:

for(int iter = 1; <error tolerance condition>; iter++) {
    ...
}
<小时/>

在 C 中,malloc() 返回值的转换完全是多余的,它只会让您的代码变得困惑。 (但请注意,C++ 不允许像 C 那样对 void* 进行隐式转换,因此 int *foo = malloc(sizeof(*foo)) 是完全有效的 C ,但在 C++ 中不合法。但是,在 C++ 中,您首先不会使用 malloc()。)

关于c - C 中两个动态增长数组的指针复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22332913/

相关文章:

c - makefile 在 Scons 中定义等效项

c - C中的内存泄漏

c - 如何单独使用指针遍历字符串数组

arrays - 动态数组的惯用初始化是否会调用未定义的行为?

python - 我可以从字典键数组创建字典值的 numpy 数组吗?

PHP PDO MySQL 数组仅返回列名

C结构,指针

c - C 中指针和子进程的问题

c++ - 对符号 '_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4' 的 undefined reference 在 Ubuntu 上构建 OpenCV

c - Sobel 过滤后的奇怪(倍增)PPM 图像