Thread: Undefined behaviour doing an memcpy from long to float variable

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Undefined behaviour doing an memcpy from long to float variable

    I am not able to figure out the knowledge gap for the below piece of code.
    I assume that the float variable to have an value equal to that of i , but it always prints 0.000. Am I missing something?

    Code:
    int main()
    {
    
    
            unsigned long i =25,j=90;
            float d,m;
            short k;
    
            memcpy((void *)&d,(void *)&i,sizeof(float));
            memcpy((void *)&k,(void *)&i,sizeof(short));
            printf("Print......i:%d, d:%f, k:%d\n",i,d,k);
    
            return 0;
    }
    output
    Print......i:25, d:0.000000, k:25


    Thanks in Advace

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Something to ponder...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        union fu {
            unsigned u;
            float f;
        } foo;
    
        foo.f = 25;
    
        printf("%x\n", foo.u);
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by Hodor View Post
    Something to ponder...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        union fu {
            unsigned u;
            float f;
        } foo;
    
        foo.f = 25;
    
        printf("%x\n", foo.u);
    
        return 0;
    }

    Is it something to do with the way floating point numbers are represented?


    But why typecasting it like this d=(float)i; gives an right value?

    I am too confused......

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, it's all about how floats are represented in memory.

    Also, assignment makes the format conversion in a way that memcpy doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    If you want to expand on what Salem correctly mentioned (it's to do with how floating point numbers are represented in memory) you could read the Wikipedia article on them as a start but depending on your knowledge it may be quite complicated. That said it's probably good to read anyway

    Edit: Just in case you get bored before you scroll down enough, also see: Floating-point arithmetic - Wikipedia
    Last edited by Hodor; 12-06-2017 at 04:46 AM.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by Salem View Post
    Yes, it's all about how floats are represented in memory.

    Also, assignment makes the format conversion in a way that memcpy doesn't.
    > (In Assignment scenario)Does that mean the compiler will take care of converting from float to int, which means that it take care of converting existing representation [S][Exponential][Mantissa] double/precision ?

    > I came across this link
    LLVM Project Blog: What Every C Programmer Should Know About Undefined Behavior #1/3

    which recommends using memcpy instead of an assignment.
    It is valid to use memcpy() for copying from float to unsigned long int.

    > My understanding is since memcpy is byte wise copy now the unsigned long int variable contains an float represented data which is invalid, Is this true?


    Thanks in advance

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. The article makes no such recommendation.
    2. It only works because floating point 0.0 is typically all bits zero. It won't work for almost any other float.
    3. It's an assumption that floating point 0.0 has to be all bits zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    > (In Assignment scenario)Does that mean the compiler will take care of converting from float to int, which means that it take care of converting existing representation [S][Exponential][Mantissa] double/precision ?


    > My understanding is since memcpy is byte wise copy now the unsigned long int variable contains an float represented data which is invalid, Is this true?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Yes, the compiler generates the right instructions to perform the necessary conversions between int and float representations.

    2. It's not an invalid FP number, but it is a denormalised value exceedingly close to 0. So close that any attempt to print it will just display 0.
    Double-precision floating-point format - Wikipedia
    Your float will have a bit pattern that strongly resembles "Min. subnormal positive double".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined variable behaviour
    By MartinR in forum C Programming
    Replies: 10
    Last Post: 11-12-2015, 08:13 AM
  2. undefined behaviour
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 01-09-2013, 08:45 PM
  3. undefined behaviour.
    By juice in forum C Programming
    Replies: 3
    Last Post: 12-21-2011, 01:03 PM
  4. strange behaviour with memcpy
    By denimjeans in forum C Programming
    Replies: 7
    Last Post: 08-16-2011, 02:39 PM
  5. Is this undefined behaviour?
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 01-15-2006, 12:55 PM

Tags for this Thread