C++ cast question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: C++ cast question

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    C++ cast question

    Hi!

    I'm wondering if its possible to cast an unsigned int to a struct ??

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    No, you can't do that directly. You cna however use pointers to cheat the compiler:

    unisgned int x;
    struct Foo {..};

    Foo f;

    x= *(unsigned int*) &f;

    Use it at your peril...

    BTW, in C++ you rarely need such hacks. Conversion operators and templates are a superior alternative.
    Danny Kalev

  3. #3
    Join Date
    Oct 2005
    Posts
    173
    why in the world would you ever NEED to do this?
    does this suggests that foo has to be the same size as an unsigned int?

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by rssmps
    why in the world would you ever NEED to do this?
    does this suggests that foo has to be the same size as an unsigned int?
    Not necessarily. If you want only to access the first 4 bytes of Foo, this will work, too. However, the two objects i.e., Foo and unsigned x, must have the same alignment, and this is really a dangerous terrain!

    Why would one want that? I can think of a several reasons: serializing an object to a file, transmitting Foo to a remote machine, encryption, compression...
    Danny Kalev

  5. #5
    Join Date
    Oct 2005
    Location
    Bangalore, India
    Posts
    16
    So you mean to say casting a pointer of premetive data type to non-permetive (struct) data type or vise a versa? Well, see the following code snippet:


    typedef struct
    {
    unsigned int x;
    }X;



    int main()
    {
    X* sxp= new X;
    sxp->x = 1024;

    //Casting struct* to unsigned int*.
    unsigned int* pui = (unsigned int*) sxp;
    std:cout << " * pui=" << *pui << std::endl;

    //Casting unsigned int* to struct*.
    X* sxp1= (X*) pui;
    std::cout << " * sxp1=" << sxp1- >x << std::endl;
    // Here what will happen if struct contains another data member next to unsigned int?


    return 0;
    }

    Output:
    *pui=1024
    *sxp1=1024

    So as long as memory allignment is ok, this will work.

    -Sudip

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    I wouldn't be so sure about it. First of all, you need to make sure that this struct is of POD type. More specifically, that there are no virtual functions, virtual base classes, and no non-public data members in that struct. Otherwise, all bets are off. But I assume that the OP meant to use a pure C struct, so as long as the struct has the same alignment as unsigned int, and that the unsigned int member is declared first, this hack should work. Not that I'm going to recommend it to anyone...
    Danny Kalev

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    Back to those bitfields:
    turned out that a struct like

    struct bit_int
    {
    int field1:2;
    int field2:6;
    int field3:8;
    };

    mapped directly into a 16 bit integer and the bits went where we wanted them. Nice, but we were afraid to rely on this and used logic operations instead for safety.

  8. #8
    Join Date
    Oct 2005
    Location
    Bangalore, India
    Posts
    16
    Yes, this solution is not recommended for NON-POD data type or with a struct where compiler will synthesis some additional data member (like VPTR/Offset of virtual base). Before doing this kind of hack we have to make sure both the source & destination memory alignment are identical.

    -Sudip.

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by jonnin
    Back to those bitfields:
    turned out that a struct like

    struct bit_int
    {
    int field1:2;
    int field2:6;
    int field3:8;
    };

    mapped directly into a 16 bit integer and the bits went where we wanted them. Nice, but we were afraid to rely on this and used logic operations instead for safety.
    Rightly so! Some compilers may align bit fields on a word boundary, and one also has to bare in mind that not all implementations use 8-bit bytes. They're pretty rare nowadays but they do exist.
    Danny Kalev

  10. #10
    Join Date
    Nov 2005
    Posts
    1
    To avoid padding and alignment use

    typedef unsigned short uint16_t;

    struct bit_int
    {
    uint16_t field1 :2;
    uint16_t field2 :6;
    uint16_t field3 :8;
    }__attribute__((__packed__));

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    this code isn't portable since the __attribute__ directive isn't standard (in C++ at least). Also, not that certain RISC architectures can't align a data structure on two-byte boundaries, so this code might cause a crash.
    Danny Kalev

Similar Threads

  1. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 07:17 PM
  2. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 07:14 AM
  3. Reflection easy question
    By Markus in forum Java
    Replies: 4
    Last Post: 03-01-2001, 10:21 AM
  4. VB,STORED PROC, COM, DTC QUESTION?
    By melvin ng in forum VB Classic
    Replies: 1
    Last Post: 11-10-2000, 03:46 AM
  5. Performance question
    By John in forum VB Classic
    Replies: 2
    Last Post: 04-05-2000, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center