Thread: Using Variable To Read Structure

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If I'm not mistaken, the compiler is responsible for the padding, not something else. You can force a compiler or set it in the options to set padding. Or you can just write a mechanism to parse memory and figure it out. Either way, it does become portable.
    That's right -- "the compiler is responsible for padding". So any code you write that assumes something like this will be unportable, reliant on compiler settings. Sure, on practically any compiler you can set this sort of thing, but it's an extra step involved. Your code doesn't compile out of the box on a new compiler.
    And actually, I have once written something "unusual" that does something like this...

    I was writing a small library for DirectInput w/ action feedback or whatever it's called. The program registered what keys it wanted to intercept and passed along a struct of bool values and the number of keys it has registered, that were set to true if key was pressed down and false if not.
    Since the library had no idea of the contents of the struct, I used a raw memory approach. If key 4 was pressed, set beginning_of_struct + 4 to true, and so on.
    Why did I do this? Why not an array or something? The approach to this is that the calling program can define the structure however they want as long as there's one member for each key. Then you can name the members whatever instead of a generic "bool key4;"
    As far as I can see, it didn't help any. You'll have to explain a bit more, but I think you were using something like this
    Code:
    struct something {
        bool mystery_member_1;
        bool mystery_member_2;
        bool mystery_member_3;
    };
    
    something s;
    *(&s + 4) = 2;  /* to set mystery_member_2 */
    I'd guess you did that so that the mystery members could be named whatever the user wanted, right? Well, it still restricts the user to a data type that could fit into 4 bytes (or whatever the padding is).

    Plus, if you just wanted custom names, then you should probably be using an interface to access the data. Abstract away the names.

    Or if you really really wanted to do it, then you could have done something like this:
    Code:
    struct something {
        bool _something_1;
        bool _something_2;
    };
    
    #define _something_1 nonmystery
    In other words, use the preprocessor to map some custom name to whatever the real name is. I've seen this done before. And while it's not the best programming practise, I would certainly consider it better than code that relies on fixed structure padding.

    [edit] Another alternative would be to use unions to define custom names, possibly in conjunction with the preprocessor or whatever.
    Code:
    struct something {
        union { bool key1, num1, one; }
        union { bool key2, num2, two; }
    };
    [/edit]

    [edit=2] Besides, let's take a step back here. You're already restricting the user to a specific order of members. How much more difficult is it to do this?
    Code:
    struct something { 
        bool data[4];
    };
    Then the user can use
    Code:
    #define nonmystery data[2]
    or something of the like. Why should you care what they call the variables? You've given them one name: data[2]. If they don't like it, well, then they can change it.

    By your argument, if the user hated your names so much, and they were restricted to a certain order and type of arguments, it wouldn't be difficult to do this.
    Code:
    struct their_names {
        bool data[3];
    };
    
    struct my_names {
        bool one;
        bool two;
        bool three;
    };
    
    struct their_names theirs = their_function();
    struct my_names mine = (struct my_names)theirs;
    bool one = ((struct my_names)theirs).one;
    bool two = mine.two;
    [/edit]
    Last edited by dwks; 10-24-2007 at 11:39 AM.
    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.

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, 06:45 AM
  3. Read multiple data for one declared variable
    By c++dummy in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2007, 02:13 PM
  4. Use variable to specify structure field
    By Rick87 in forum C Programming
    Replies: 10
    Last Post: 03-19-2006, 01: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