Take the 2-minute tour ×

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

int *t2= (int *)malloc(20*sizeof(int));


    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!!

share|improve this question
You've tagged this as "C", yet you're using cout? –  Damien_The_Unbeliever Aug 24 '12 at 8:04
sorry, it is mix code )) –  Myosotis Aug 24 '12 at 8:05
What do you mean by "empty"? –  Kerrek SB Aug 24 '12 at 8:05
The final (float) cast is completely pointless: variable function arguments are default-promoted. (And t1[i] is already of type float!) Are you copy/pasting bits of code from somewhere else? –  Kerrek SB Aug 24 '12 at 8:06
there is zero in every cell –  Myosotis Aug 24 '12 at 8:06
show 5 more comments

6 Answers

up vote 3 down vote accepted

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 printf this 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.

share|improve this answer
I see that you are right. I can see these small values with cout << t1[i]; –  Myosotis Aug 24 '12 at 8:33
Totally nitpicky point: the latest revision of IEEE 754 (2008) changed "denormal" to "subnormal". –  Pete Becker Aug 24 '12 at 12:13
@PeteBecker: Haha, interesting :-) –  Kerrek SB Aug 24 '12 at 12:14
add comment

The function memcpy copies bits, verbatim. So you're copying bits from an integer array to a float array: there's very little chance the contents will match the floating point representation. So you get an array filled with stuff that makes little sense to your floating point format.

In a nutshell, at the lowest level, 1 and 1.0f look wildly different.

The cast (float)t1[i]) doesn't really do anything since t1[i] is already float.

share|improve this answer
What should he use instead? –  Default Aug 24 '12 at 8:09
Ok. So memcpy is useful function –  Myosotis Aug 24 '12 at 8:09
@Default, std::vector<float>, std::vector<int>, std::copy()... –  Nim Aug 24 '12 at 8:09
@Leila if it was useless, I don't think it would exist. –  Default Aug 24 '12 at 8:10
@Leila It's not useless, it's made for other uses :-) –  cnicutar Aug 24 '12 at 8:10
show 4 more comments

You are forcing the integer values 0, 1, 2, 3, 4, 5 .. 20 into floats. Floats and ints use different underlying representations, and offhand, I think those values are just very very small and will print as 0 unless you change the format to include a lot of precision.

share|improve this answer
add comment

Bit arrangement is different in an int and a float.Even if things get copied well you wont be able to understand it.

Change to

  printf("%d\t", (int)t1[i]);

and you will see things copied.

share|improve this answer
But I see only zeros –  Myosotis Aug 24 '12 at 8:30
add comment

memcpy copies memory as bytes and doesn't care what underlying types have been stored at the memory location. Integers in memory are stored in a different format than floats so what basically ends up in t1 could be but must not necessarily be valid floating point values.

share|improve this answer
add comment

Since you are using C++ you can use the C++ features instead, like std::vector

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;
int main(){
    std::vector<float> floatVector;
    std::vector<int> intVector;


    std::copy(intVector.begin(), intVector.end(), floatVector.begin());
        cout << "element[" << i << "]: " << floatVector[i] << std::endl;
share|improve this answer
It is good. But I use Memory_alloc from c6accel library (sharing memory with DSP). I can't represent this data with vector object. –  Myosotis Aug 24 '12 at 8:35
add comment

Your Answer


