Thread: Enforcing structure order?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Enforcing structure order?

    Do compilers guarantee that the structure generated by your code will place the variables in the exact order you declared them? And if not, can you give an example for how to do this on, say, your compiler?

    I am having this problem:


    Code:
    struct something {
    int a;
    int b;
    int c;
    int d;
    };
    
    
    struct something s;
    
    memset(&s, 0, sizeof(s));
    int value = 22;
    s.a = value;
    
    if(memcmp(&s, &value, sizeof(int)) == 0) {
      cout << "Indeed..." << endl;
     }
    else {
      cout << "Afraid not..." << endl;
     }

    The output is always negative.

    However, I found the value at offset + 8, and thus this printed positive:


    Code:
    
    if(memcmp(&s + 8, &value, sizeof(int)) == 0) {
      cout << "Indeed..." << endl;
     }
    else {
      cout << "Afraid not..." << endl;
     }
    How can I enforce the order of placement?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ahhh! cout in the C board!!!!

    /me shuns the code to the C++ board


    anyway, the test snippet prints "indeed" on my machine, which is what i expected it to print. so i can't really help you....
    hello, internet!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, to be honest, that was not the whole story. In essence, I am using a template to hold any type of object, which can vary in size, of course. The template structure also has variable which stores the size of the object contained within, such that I might be able to query the template at runtime, by simply calling memcmp on the structure starting at the 0th offset, using the size variable as a ruler, so to speak. Naturally, the compliler wants to put known types first, since it will make generating the code for accessing that structure much simpler. What I want to do is overide this behaviour such to align the stored object on the 0th offset.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    on your platform, does this trimmed down code still exhibit the undesidered behavior like the original? (it doesnt on mine).
    hello, internet!

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Salem
    between members of the structure. Though this is the first I've heard of a structure being padded before the first element.
    the (very good) C book i have in front of me says that padding before first element is NOT allowed. so i would expect that this is one of those class thingies, with hidden class info.
    hello, internet!

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thank you Salem for your help. It worked!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Reference to a Structure
    By boschow in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 02:37 PM
  3. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  4. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM