migrating from C to C++: struct into object? memory footprint

Discussion in 'Programmer's Corner' started by russian, Mar 19, 2014.

  1. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    I have some code in C which I am trying to make nicer by adding a little bit of C++.

    I have a struct of structs:
    struct CltSensor{
    float A, B, C;
    }
    struct MafSensor{
    float min, max;
    }

    struct GodStruct {
    CltSensor clt;
    MafSensor maf;
    }

    what is great about current GodStruct - I know the exact footprint. I know that the total size is 20 bytes, I know that A lives at offset 0 and min lives at offset 12. This is useful because the whole data structure is persisted as is.

    Question: is there a way for me to make CltSensor and MafSensor objects but still persist the GodStruct directly?

    I do not mind leaving some gaps - say if there would be a 8 bytes header that's fine. If it matters, I compile in fully static context - there is no heap, everything is allocated on the compile time.
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,750
    4,797
    You only think that you know the footprint. The compiler has some flexibility in assigning padding bytes and it might choose to align the two structure elements on 8-byte boundaries which would place min at 16. Different versions of the same compiler or even the same version with different options set, such as speed vs space, might do it differently.
     
  3. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    My target architecture is 32bits and my GCC has the __packed__ magic so for the sake of the argument let's assume I know the footprint. With assumptions like that, do I have a chance to know my C++ class footprint?
     
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,686
    2,747
    You could allocate memory in the constructor as an array of 20 bytes. Then, define pointers that recast each variable within the proper subset of bytes in the array.

    Think about what you are doing, and why. One of the major benefits of C++ is it allows you to abstract the functionality away from the actual implementation. A class should look like a black box...it's got inputs and outputs -- but the internal implementation should be hidden and irrelevant. By forcing a particular implementation, and exposing it, you are breaking a fundamental rule of C++.
     
    ErnieM and russian like this.
  5. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    Let's stay on topic :)

    While theory is great, unfortunately this is all happening in a particular legacy environment - I cannot change the plain binary persistence protocol, I am looking for a reasonable compromise of adding at least come C++ sugar in it. I just do not see much point in a OOP-perfect 100 parameter contstuctor in order to hide the internal structure by injecting values field by field. Let's stay real.
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,686
    2,747
    I gave you a solution. I believe that was on topic.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    Getting C++ sugar? Oh no, that's next door.

    It's being-hit-on-the-head lessons in here.
     
Loading...