Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning int* from incompatible void *

Tags:

c

I have this code:

int * generate_code(int *bits, int Fs, int size, int *signal_size, float frameRate)
{
  int sign_prev, i;
  int bit, t, j=0;
  int *x;
  float F0, N, t0, prev_i, F1;
  int temp = 0, temp1, temp2;

  F0 = frameRate * BITS_PER_FRAME;      // Frequency of a train of '0's = 2.4kHz
  F1 = 2*F0;       // Frequency of a train of '1's = 4.8kHz
  N = 2*(float)Fs/F1;   // number of samples in one bit

  sign_prev = -1;
  prev_i = 0;
  x = (int *)malloc(sizeof(int));
  for( i = 0 ; i < size ; i++)
  {

    t0 = (i + 1)*N;
    bit = bits[i];
    if( bit == 1 )
    {
      temp1 = (int)round(t0-N/2)-(int)round(prev_i+1)+1;
      temp2 = (int)round(t0)-(int)round(t0-N/2+1)+1;
      temp =j + temp1 + temp2;
      //printf("%d\n", (int)temp);
      x = realloc(x, sizeof(int)*temp);  // 1

      for(t=(int)round(prev_i+1); t<=(int)round(t0-N/2); t++)
      {
        *(x + j) = -sign_prev;
        j++;
      }
      prev_i = t0-N/2;
      for(t=(int)round(prev_i+1); t <= (int)round(t0); t++)
      {
        *(x + j) = sign_prev;
        j++;
      }
    }
    else
    {
      // '0' has single transition and changes sign
      temp =j + (int)round(t0)-(int)round(prev_i);
      //printf("%d\n",(int)temp);
      x = realloc(x, sizeof(int)*(int)temp);  // 2
      for(t=(int)round(prev_i); t < (int)round(t0); t++)
      {
        *(x + j) = -sign_prev;
        j++;
      }
      sign_prev = -sign_prev;
    }
    prev_i = t0;
  }

  *signal_size = j;
  return x;
}

Both realloc lines, marked with //1 and //2 on the previous code, give me this error message:

assigning to int * from incompatible type void *

Because I don't want this code behaving weirdly or crashing on me, obviously, I ask will: I have some problem in the future if I simply cast it to int * by doing

x = (int*)realloc(x, sizeof(int)*(int)temp);

Thanks

like image 276
Bobby Avatar asked Dec 19 '15 21:12

Bobby


2 Answers

In C, a value of type void* (such as the value returned by realloc) may be assigned to a variable of type int*, or any other object pointer type. The value is implicitly converted.

The most likely explanation for the error message is that you're compiling the code as C++ rather than as C. Make sure the source file name ends in .c, not .C or .cpp, and make sure your compiler is configured to compile as C rather than as C++.

(Casting the result of realloc or malloc is considered poor style in C. In C++, the cast is necessary, but you normally wouldn't use realloc or malloc in C++ in the first place.)

like image 109
Keith Thompson Avatar answered Oct 20 '22 15:10

Keith Thompson


This should work in C. Are you perhaps using a C++ compiler to compile this? For example, some big company based in Redmond refuses to properly support a contemporary C implementation. Their compiler is C++ by default and needs some option to whack it into a C compiler.

You have stdlib.h included? Then you don't need the casts. In fact, it is best practice to not cast the malloc return.

like image 20
Jens Avatar answered Oct 20 '22 15:10

Jens