I don’t understand why the array t1 is empty. As I know, memcpy shouldn’t care about the underlying types of the objects. What do you think? %)
cout << sizeof(float) << sizeof(int) << endl;
float *t1= (float *)malloc(20*sizeof(float));
memset(t1,0x00,20*sizeof(float));
int *t2= (int *)malloc(20*sizeof(int));
for (int i=0; i<20; i++)
t2[i]=i;
memcpy(t1,t2,20*sizeof(int));
for (int i=0; i<20; i++)
printf("%f\t", (float)t1[i]);
I know what type casting means. Ok I did a little mistakes. It was carelessness!
P.S. It was sample for understanding how memcpy works!!
Your array is not “empty”, despite your protestations. It just holds a very, very small value: Your machine uses the IEEE754 standard for representing floating points. In that standard, the word with all zeros represents the value 0.0. The next bigger word (i.e. the one obtained by adding 1 to the underlying bits) represents the next biggest floating point value, which is an extremely tiny, denormal value. When you
printfthis value to standard precision (6 decimal places?), it’s just rounded to zero.Here’s a tangentially related answer of mine on a similar question.