简体   繁体   中英

invalid cast from type 'void*' to type 'int*'

Added2 : After change to static , it can correctly work in gcc 4.8.4 (ubuntu 4.8.4-2ubuntu1~14 .04.3) within bash on ubuntu on windows . But it can't work in windows10 using gcc 4.9.2(tdm-1). And i change the compiler to cygwin which has gcc 4.9.2,not the tdm-1 version. Weird, it works! So i think the complier also has some bug!

Added1 : I'm very sorry i find my compiler is gcc not g++, i'm new to the program world,please forgive me. I used .c to build, but it has some memory-leak bug that i can't handle. So i change to .cpp to build, and these error come out. So this is my situation.

windows 10 elipse gcc 4.8.4 . I use C language.

elipse give me an error: invalid cast from type 'void*' to type 'int*' [-fpermissive] .

elipse suggest these lines have the error

FullList = malloc(N * sizeof(int));
l = malloc(N * sizeof(int));

I don't know how to correct it. Any lead would be appreciate!

this is the function which involve this sentence.

#define ALLCHK(x) if (x == NULL) {printf ("allocation error\n"); assert(x);}
void Generate_Permutation(int N, int *p)
/* generate permutation of length N in p */
/* p is zero based, but the permutation is not */
{
  int i,j;         /* to loop */
  int lspot;         /* offset in l */
  int *FullList;       /* unpermuted */
  int *l;          /* left to be used */

  FullList = malloc(N * sizeof(int));
  ALLCHK(FullList)
  for (i=0; i<N; i++) *(FullList+i) = i+1;
  l = malloc(N * sizeof(int));
  ALLCHK(l)

  memcpy(l, FullList, sizeof(int)*N);
  for (i=0; i < N; i++) {
    lspot = (int)(URan(&seed) * (N - i));
    *(p+i) = *(l+lspot);
    for (j=lspot; j<N-i; j++) *(l+j) = *(l+j+1);
  }
  free(l); free(FullList);
}

In C++, a void* is not implicitly convertible to a int* . You need to cast the result of malloc explicitly:

fullList = static_cast<int*>(malloc(N * sizeof(int)));
l = static_cast<int*>(malloc(N * sizeof(int)));

Since you are using C++, you would be better off with the new operator:

fullList = new int[N];
l = new int[N];

// some code...

delete[] fullList;
delete[] l;

While were at it, you can use a unique pointer:

std::unique_ptr<int[]> fullList = new int[N];
std::unique_ptr<int[]> l = new int[N];

// no delete at the end of the scope

But even simpler, simply use a vector:

std::vector<int> fullList(N);
std::vector<int> l(N);

To me, it seems like you are porting a C code snippet to C++ (for inclusion in a C++ project?).

Consider this answer as a quick fix to your problem, not as the best solution.

When using malloc / free in C++, you have to cast the void* pointer as returned by malloc to your desired pointer type:

FullList = static_cast<int*>(malloc(N * sizeof(int)));
l = static_cast<int*>(malloc(N * sizeof(int)));

The reason for this (when simply copying over C code) is that in C these casts are allowed to be performed implicitly .

Other options would be to compile that file with a C compiler, for which it might be enough to simply rename the file to an extension of .c instead of .cpp . Then, g++ (a C++ compiler) would automatically use gcc (the C compiler) to compile your code.

Again another option would be to rewrite this to "real" C++ code using new / delete instead of malloc / free , but then you could equally well also rewrite it to use modern memory management (like std::vectors etc.).

In C, you do not have to cast (void*) result of malloc. In C++, compiler cries, unless you cast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM