[英]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中,转换
realloc
或malloc
的结果被认为是不好的样式。在C ++中,强制转换是必要的,但你通常不会在C ++中使用realloc
或malloc
。)
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.