如何在cpp中使用realloc

How to use realloc in cpp

本文关键字:realloc cpp      更新时间:2023-10-16

我有以下cpp代码

#include <stdio.h>         /*utiliser printf*/
#include <fcntl.h>
#include <math.h>          /*utiliser pour les formules de math*/
#include <malloc.h>
#include <iostream.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
  /* Le type nombre complexe */
typedef struct {
  double Preel;
  double Pimaginaire;
} COMPLEXE;
#define ALLOC_ERROR 1
void indienne(double *MatE, int tE, int nE, double *SortieExp, double *Tempbis)
{
  double *TempE=NULL, *SortieE=NULL;
  int *dec=NULL;
  int i, tampon, kE;
  kE=(int)(log(nE)/log(2));
  if(nE==8)
    kE=3;
  /* ALLOCATION DES MATRICES*/
  if (!(TempE = (double *)calloc(tE * tE, sizeof(double))))
    exit(ALLOC_ERROR);
  printf("check1  te=%d, nE=%d",tE,nE);
  if (!(dec = (int *)realloc(kE , sizeof(int))))
    exit(ALLOC_ERROR);
  if (!(SortieE = (double *)calloc(tE * tE, sizeof(double))))
    exit(ALLOC_ERROR);
  printf("check2  te=%d",tE);
  memcpy(TempE,MatE,tE * tE * sizeof(double));
  for (i=0; i<tE; i++)
    *(Tempbis+(tE * i) + i) = 1.0;
  if (nE==1) 
  {
    memcpy(SortieExp, MatE, tE*tE*sizeof(double));
  }
  else
  {
    printf("kE=%d, nE=%dn", kE, nE);
    if (nE%2==0) 
      decompose(kE, nE,dec);
    else 
      decompose(kE, nE-1, dec);
    for (i=0; i<kE; i++)
    {
      carre(TempE, tE, SortieE);
      memcpy(TempE, SortieE, tE*tE*sizeof(double));
      tampon=*(dec+i);
      if (tampon==1)
      {
        mult(Tempbis, tE, tE, SortieE, tE, tE, SortieExp);
        memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
      }
    }
    if (nE%2 !=0)
    {
      memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
      mult(Tempbis, tE, tE, MatE, tE, tE, SortieExp);
    }
  }
  free(TempE);
  free(SortieE);
  free(dec);
}

当我编译这段代码时,出现以下错误

从'int'转换为'void*'无效[-fpermissive]|

这是关于下面的代码行

if (!(dec = (int *)realloc(kE , sizeof(int))))

如何删除此错误?

您将int kE作为这里的第一个参数:

realloc(kE , sizeof(int))

但是,realloc是这样声明的:

void *realloc(void *ptr, size_t size);

换句话说,它需要一个指针!请阅读我链接到上面的手册页以获得更多细节。简而言之,您可能想要这样处理错误行:

if (!(dec = (int *)realloc(dec , sizeof(int))))

请注意,这有点糟糕,因为如果realloc失败,您将失去dec的原始值,从而导致内存泄漏。如果在出错时退出并不重要,但除此之外,您应该保留dec的原始值,这样您可以更优雅地处理错误,而不仅仅是退出。

另一个注意事项,你真的应该使用像vector这样的c++容器类,而不是在C的内存分配函数上瞎胡闹。

你的代码中可能还有其他问题,但这个答案并不是代码审查,而只是解释为什么你会得到你得到的错误。

试着不要这样做:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int *pa = malloc(10 * sizeof *pa); // allocate an array of 10 int
    if(pa) {
        printf("%zu bytes allocated. Storing ints: ", 10*sizeof(int));
        for(int n = 0; n < 10; ++n)
            printf("%d ", pa[n] = n);
    }
    int *pb = realloc(pa, 1000000 * sizeof *pb); // reallocate array to a larger size
    if(pb) {
        printf("n%zu bytes allocated, first 10 ints are: ", 1000000*sizeof(int));
        for(int n = 0; n < 10; ++n)
            printf("%d ", pb[n]); // show the array
        free(pb);
    } else { // if realloc failed, the original pointer needs to be freed
        free(pa);
    }
}