This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Structure fields & RAM variables on the same address

I have a structure with various fields. And there are few global variables.
The structure fields and the global variables (basically status-flags) denote same information.
It doesnt make sense to have both of them in the RAM area. And i dnt wanna always use the '.' or '->' notations to access the structure fields as there are structure inside the structure.

Hence i feel that a way around is to use unions. But dont know how to use unions for structure in a structure type of variables. can somebody kindly give a small example or a good webpage detailing the same as i couldnt find any relevant thing on web highlighting such an example.

An example would be appreciable.
Thank you in advance.

Parents
  • It may seem silly, but i find that there would be some convenience, Given the scenario where i have to copy the structure from flash memory and then accessing the resources by copying the resources to the variables.

    I am just trying to ease my work of remembering each n every members of many different structures and also saving some code involved in copying the struct members into variables.

    If i dnt find any alternative, i will readily use one of the two methods (either use of '.' or copy into variables and then access the variables).

    Can't use C++ at this moment in the project.

Reply
  • It may seem silly, but i find that there would be some convenience, Given the scenario where i have to copy the structure from flash memory and then accessing the resources by copying the resources to the variables.

    I am just trying to ease my work of remembering each n every members of many different structures and also saving some code involved in copying the struct members into variables.

    If i dnt find any alternative, i will readily use one of the two methods (either use of '.' or copy into variables and then access the variables).

    Can't use C++ at this moment in the project.

Children
  • Note that the copy of a struct from flash to RAM doesn't require you to keep track of the contents of the struct.

    my_struct_in_ram = my_struct_in_flash;
    

    Aliasing by overlaying variables is probably the most common source of incorrect code generated by compilers - just because they have a very hard time figuring out how a variable assign affects another variable.

    And duplicating data manually is a great way to manually produce broken code.

    So your choice is to either try to get the compiler to produce incorrect code or to produce incorrect code yourself? Just because you feel it's too much job to write the full access name of the variables? Because you still haven't given a reason why you need to have any flag variables as directly globally accessible without use of the struct addressing.

    Modern programming languages works by data hiding and accessor functions. All to help writing software with consistent internal states. And you want to walk the different route. Explicit data duplication.

    If students hands in code that intentionally does data duplication then they should get points off for bad solution. And if a developer in a commercial team does it, then he should get a talk with team leader about good and bad software practices.

    Workarounds are used when someone is cornered. Only when there are really, really strong reasons to do it. And requiring lots of extra documentation to make sure that anyone who later tries to modify the code are extremely well aware about the special requirements. Time hammering text on the keyboard is nothing in the full life cycle of a software. And better variable names and function names reduces the cost of documenting and maintaining. Intentionally adding trap doors to the code is not an acceptable route in the software industry. And right now, your goal is to figure out the most clever trap door.

  • char *myvar1, *myvar2; // Global, as pointer
    //flag1 of st1 can be access in following manner
    myvar1 = &mainstruct.st1.flag1;
    myvar2 = &mainstruct.st1.flag2;
    // assuming mainstruct is also global, or adequate scope
    
    Access with *myvar1, or *myvar2
    
    if (*myvar2 == 0x12)
    {
      // ..
    }