0
votes

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);
}
3
The error is not from Eclipse (wich is an editor) but from the compiler. BTW are you coding in C or in C++, they are different programming languages ; and C++11 is very different from C++98 - Basile Starynkevitch
The rules are different for C and C++. Which one are you using? - Bo Persson
You need to decide, as a first step, which language you are using, C or C++. - Sam Varshavchik
What is the definition of ALLCHK? - dbush
@Yu-FengQin You should include <cstdlib> if you want to use C library. - Guillaume Racicot

3 Answers

5
votes

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);
1
votes

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.).

0
votes

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