0

I have following cpp code

#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=%d\n", 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);
}

When I compile this code following error occurres

invalid conversion from 'int' to 'void*' [-fpermissive]|

that is about following line of code

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

How can I remove this error?

Teivaz
  • 5,462
  • 4
  • 37
  • 75
  • 2
    The `realloc()` function could be apply only to a still allocated array with the function as 'malloc()` or `calloc()`. The variable `kE` is a integer allocated on the stack. – J. Piquard Nov 02 '16 at 21:40

2 Answers2

1

You are passing int kE as the first parameter here:

realloc(kE , sizeof(int))

However, realloc is declared like this:

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

In other words it expects a pointer! Please read the manual page I link to above to get more details. In short, you probably want something like this for the error line:

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

Note that this is slightly bad, because if realloc fails, you lose the original value of dec, causing a memory leak. It doesn't really matter if you are going to exit on error, but otherwise you should keep the original value of dec, so you can handle error more gracefully than just exiting.

Another note, you really should use C++ container classes like vector instead of fooling around with C memory allocation functions.

There are probably other issues in your code, but this answer doesn't try to be code review, but just explain why you get the error you get.

hyde
  • 60,639
  • 21
  • 115
  • 176
-1

Try without do it like this :

#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);
    }
}
Cherkesgiller
  • 520
  • 4
  • 17
  • 1
    Casting from `malloc` and `realloc` is *required* in C++. I think you may be thinking of `C`, where it is both *not* required, [and strongly discouraged](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – WhozCraig Nov 02 '16 at 21:39