Struct member manipulation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 6 of 6

Thread: Struct member manipulation

  1. #1
    Join Date
    Mar 2006

    Struct member manipulation

    I work on a system that communicates with other systems via messages. Those messages are defined in a spec and every word must be exactly as defined. To accomplish this Ada allows me to define the fields of my record to specific words in memory. Will C++ allow me to accomplish this same task and if so could someone post an example

    word = 4;
    type Msg_Type1 is record
       x: Some_Type;
       y: Another_Type;
       z: Another_Type;
    end record;
    // ada allows me to rep out my record using the 'for use' clause
    for Msg_Type1 use record
       x at 0*word range 0..31;
       y at 1*word range 0..15;
       z at 1*word range 16..31;
    end record;
    Last edited by tsnofvdr; December 5th, 2012 at 01:13 PM.

  2. #2
    Join Date
    Jan 2006

    Re: Struct member manipulation

    Sorry, I don't know Ada. You probably should explain just what you mean by "define how the compiler handles the fields of my record".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999

    Re: Struct member manipulation

    Quote Originally Posted by tsnofvdr View Post
    To accomplish this Ada allows me to define the fields of my record to specific words in memory. Will C++ allow me to accomplish this same task and if so could someone post an example
    I also don't know ADA, but what if this C++ example shows code that is hard-to-maintain or easily bug prone, so much so that no C++ programmer out in the field would recommend it? The point being that you should think on a higher level of abstraction. C++ isn't ADA, but the higher-level concept should be the same.

    On a high-level, what exactly is being accomplished by taking this approach? Whatever that is, then you use C++ paradigms to accomplish the goal, where those paradigms may not look anything like ADA, may not even use structs, etc..

    In general, attempting to translate language X to C++ almost verbatim usually doesn't work, is very cumbersome, or leads to serious runtime and maintenance issues (one such case is translating from Java to C++).


    Paul McKenzie
    Last edited by Paul McKenzie; December 5th, 2012 at 01:51 PM.

  4. #4
    Join Date
    May 2001

    Re: Struct member manipulation

    You might want to look up 'bitfields':

    But be aware that the implementation is compiler-dependent and as such not easily portable. Also, the memory layout differs on big-endian vs little-endian machines.


  5. #5
    Join Date
    Mar 2006

    Re: Struct member manipulation

    That's what I was looking for...Thanks for your help

  6. #6
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: Struct member manipulation

    if you need to have a system that will be portable (works for any c++ compiler), then don't use bitfields, instead do the actual maskign/shifting explicitely. How bits in a bitfield are assigned is up to the compiler and not all compilers work the same. Doing things yourself is a bit more work but it will ensure it works exactly how you need it to work.

    You may also need to use structure packing. When creating a structure in C/C++, the compiler will insert padding bytes to make sure each field in a structure follows it's proper alignment, this may or may not match what ADA is doing.

    for VS (and a few other compilers) this is arranged with the #pragma pack() directive. Other compilers solve this by other means.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center