Thread: Pointer conversion issues

  1. #1
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57

    Question Pointer conversion issues

    Hello there,
    I have a question regarding pointers conversions (not sure if i got that right in English). Lets say I am on a 32 bit system, little endian.

    There is an 8 bit variable:
    Code:
    uint8_t var8 = 0x12;
    This variable exists somewhere in the memory space.

    Now I create a 64 bit type pointer without memory allocation:
    Code:
    uint64_t* ptr64 = 0;
    And assign my 8 bit var address to it:
    Code:
    ptr64 = (uint64_t*)&var8;
    When I will look at the LSB of value under ptr64 it will be 0x12. But what will be under other 56 bits? For my tests, it gave 0's, but I feel i was lucky. By doing this conversion, am i playing with segmentation fault, as I am overlaping some memory next to my pointer?

    I would apreciate all help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The rule is:
    Quote Originally Posted by C11 Clause 6.3.2.3 Paragraph 7
    A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned for the referenced type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object. Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
    The way I see it, a pointer to uint64_t is not correctly aligned for a single uint8_t, related to your question about what happens to the rest of the bits. The behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    Hello thank you for answer,
    But what does it mean that it is not correctly aligned? Do you mean that uint8 is not placed as lsb of uint64 when converted?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bremenpl
    But what does it mean that it is not correctly aligned? Do you mean that uint8 is not placed as lsb of uint64 when converted?
    No, my interpretation of the standard is that this means that a pointer to a uint64_t object must be able to access what it points to in multiples of sizeof(uint64_t), i.e., to reinterpret the underlying object as an array of uint64_t. But this cannot be done for a single uint8_t (unless sizeof(uint64_t) == 1, but that would be so unusual that you should have mentioned it), so I reason that this means the behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    I think I understand... Also I meants sizeof(uint64_t) = 8. But I dont know why is it undefined? If lets say one creates a union like this:

    Code:
    typedef union
    {
         uint64_t var64;
         uint8_t bar8[8];
    } anUnion_t;
    
    anUnion_t myUnion;
    I can do:
    Code:
    uint64_t newVar64 = myUnion.var64;
    And this would work too:
    Code:
    newVar64 = 0;
    size_t i = 0;
    for (i = 0; i < sizeof(uint64_t); i++)
         newVar64 |= myUnion.var8[i] << (i * sizeof(uint64_t));
    Do I understand correctly?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bremenpl
    But I dont know why is it undefined?
    Because a uint64_t cannot validly point to a single uint8_t: there are missing bytes. If you have a box that fits 8 identical chocolates, putting in only one such chocolate and claiming that you have a full box of chocolates is a lie.

    Quote Originally Posted by bremenpl
    Do I understand correctly?
    Yes. Strictly speaking the standard does not make any such guarantee for unions, and for pointers it just says that you can safely convert to and from the destination pointer, but in practice it should work as expected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    Ok, thank you very much for explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float to long conversion issues?
    By Snaggletooth in forum C Programming
    Replies: 6
    Last Post: 04-06-2012, 10:27 AM
  2. Issues with Binary conversion program
    By angel48797 in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2011, 07:51 PM
  3. Issues with Binary conversion program
    By angel48797 in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2011, 04:44 PM
  4. pointer issues
    By Bobbel in forum C Programming
    Replies: 23
    Last Post: 08-14-2010, 05:11 PM
  5. Visual Age C++ to MS VC++ conversion issues?
    By Ruchikar in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2003, 09:54 PM

Tags for this Thread