简体   繁体   English

从不兼容的void *中分配int *

[英]Assigning int* from incompatible void *

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: 在前一个代码上标记为//1//2 realloc行都给出了以下错误消息:

assigning to int * from incompatible type void * 从不兼容的类型void *分配给int *

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 因为我不希望这段代码表现得奇怪或者让我崩溃,显然,我会问:如果我只是将它转换为int * ,我将来会遇到一些问题

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

Thanks 谢谢

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. 在C中,可以将类型为void*的值(例如realloc返回的值)分配给int*类型的变量或任何其他对象指针类型。 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++. 对错误消息最可能的解释是您将代码编译为C ++而不是C。确保源文件名以.c结尾,而不是.C.cpp ,并确保编译器配置为编译作为C而不是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.) (在C中,转换reallocmalloc的结果被认为是不好的样式。在C ++中,强制转换是必要的,但你通常不会在C ++中使用reallocmalloc 。)

This should work in C. Are you perhaps using a C++ compiler to compile this? 这应该在C中工作。你是否可能使用C ++编译器来编译它? For example, some big company based in Redmond refuses to properly support a contemporary C implementation. 例如,一些位于雷德蒙德的大公司拒绝正确支持当代的C实现。 Their compiler is C++ by default and needs some option to whack it into a C compiler. 他们的编译器默认是C ++,需要一些选项才能将它打入C编译器。

You have stdlib.h included? 你有stdlib.h吗? Then you don't need the casts. 那你就不需要演员了。 In fact, it is best practice to not cast the malloc return. 实际上,最好不要强制转换malloc。

C中的所有alloc style函数都返回具有最严格对齐的内存地址,因此强制转换不能给出不是有效int指针的指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从“int (int *)”分配给“void (*)(void *)”的不兼容指针类型 - incompatible pointer types assigning to 'void (*)(void *)' from 'int (int *)' 错误:语义问题从不兼容类型“ void”分配给“ int” - Error: Semantic issue Assigning to 'int' from incompatible type 'void' 为什么我从“不兼容类型&#39;void *&#39;”错误中“分配&#39;int *&#39;? - Why do I get “assigning to 'int *' from incompatible type 'void *' ” error? 从不兼容的类型&#39;int&#39;分配[custom typdef] - Assigning to [custom typdef] from incompatible type 'int' 错误:从“void *”类型分配类型“值”时出现不兼容的类型? - Error: incompatible types when assigning to type 'values' from type 'void *'? 从可可项目中的不兼容类型“ void *”错误分配给“ MyPrivateData *” - Assigning to “MyPrivateData *” from incompatible type 'void *' error in cocoa project 从int类型分配给char []类型时,类型不兼容 - Incompatible types when assigning to type char[] from type int 从Int类型分配给&#39;struct PartyInfo&#39;类型时不兼容的类型 - Incompatible types when assigning to type 'struct PartyInfo' from type Int 从“ int”类型分配给“ memstruct”类型时不兼容的类型 - incompatible types when assigning to type ‘memstruct’ from type ‘int’ 从“ int”类型分配给“ struct protein”类型时不兼容的类型 - incompatible types when assigning to type 'struct protein' from type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM