Using Variable To Read Structure

This is a discussion on Using Variable To Read Structure within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by dwks Right, argument over. Oh well. I haven't had one like that for a while, it was ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by dwks View Post
    Right, argument over. Oh well. I haven't had one like that for a while, it was interesting.
    Yes, it quite certainly was!

    But that would be inefficient compared to <forcibly stops typing>
    I don't care! It's allows whatever-you-like variable names and it's safe to use, and portable too (or should be anyway).

  2. #32
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, very portable, safe, and good.

    Maybe you could lend some of your structure-member-accessing-by-offset expertise here . . . Problems with bmp-reading program
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Unfortunately, I think most about the structures has been said already... I can't think of a portable solution right now, either... Unless it's possible to use unions to group together chars, but I'm not too knowledgable about them.

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Sorry, it's not.

    Basically, as far as the standard is concerned, the ground rule is that if you access anything through a different type than it actually is, it's undefined. The only two exceptions are where
    1) we're talking about base and derived class pointers/references and
    2) we're talking about the extremely elusive structural conformance concept. Which is really still about the type being the same, but as members of different structures. And there are very stringent restrictions on these structures.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    I'm thinking of the unions that are usually declared (and used by) Windows API in its headers:

    Code:
    typedef union _ULARGE_INTEGER {
      
    struct {    
    DWORD LowPart;    
    DWORD HighPart;  
    };  
    struct {    
    DWORD LowPart;    
    DWORD HighPart;  
    } u;  
    ULONGLONG QuadPart;
    } ULARGE_INTEGER, 
    *PULARGE_INTEGER;
    If it's possible to declare all the members are char, then you might be able to avoid padding (at least until the end), while still being able to access the data through members with other type. Dunno if it's possible. Otherwise I can't think of another solution than to force padding.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I'm thinking of the unions that are usually declared (and used by) Windows API in its headers:

    Code:
    typedef union _ULARGE_INTEGER {
      
    struct {    
    DWORD LowPart;    
    DWORD HighPart;  
    };  
    struct {    
    DWORD LowPart;    
    DWORD HighPart;  
    } u;  
    ULONGLONG QuadPart;
    } ULARGE_INTEGER, 
    *PULARGE_INTEGER;
    If it's possible to declare all the members are char, then you might be able to avoid padding (at least until the end), while still being able to access the data through members with other type. Dunno if it's possible. Otherwise I can't think of another solution than to force padding.
    That union is fine as long as two DWORDs does not require a different alignment than a ULONGLONG. Which holds true as long as the machine can handle DWORD without it being aligned to anything bigger - which again holds true for all machines that currently can (or in the past have been able to) run Windows. By the way, unportable constructions are VERY common in header files that belong to the OS - because they can rely on the OS and the compiler having to adhere to certain standards.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2009, 04:21 AM
  2. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 05:45 AM
  3. Read multiple data for one declared variable
    By c++dummy in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2007, 01:13 PM
  4. Use variable to specify structure field
    By Rick87 in forum C Programming
    Replies: 10
    Last Post: 03-19-2006, 12:17 PM
  5. How to read from a file into a structure?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21