Global structure variable elements have different addresses

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Global structure variable elements have different addresses

761 Views
EricM
Contributor I

- Which CPU are you targeting:                                    MCF52258
- Which version of CodeWarrior are you using?      10.0 and 10.1 (we see problem on both)

 

Hi!  We are seeing some unexpected global variable behavior:

In one file the code writes a value into an element of a global structure, but when the code reads the same value in another file it isn't what was written. This is because it's reading (the same structure element) from a different memory location. For example: When the variable 'tva.options.probe_type' is accessed in the probe.c function 'probe_init()' - the address shown by the debugger is 0x20004f69; the address shown in the assembly code window, and what is changed in the memory is 0x20004eb4. This means the function 'probe_init()’ accesses and changes the wrong variable.

Does anyone have any ideas on how we can get to the bottom of this behavior?

Labels (1)
0 Kudos
Reply
3 Replies

566 Views
EricM
Contributor I

One of my colleagues found a setting in the project properties that appears to have fixed the issue:

Properties --> C/C++ Build --> Settings --> Coldfire Compiler --> Processor --> set the "Struct align" setting to "byte".

 

This got all the modules to look at the same addressing for the global variable structure, as far as we can tell.  It did take care of the instances we were aware of, anyway.

 

Thanks to those of you who posted suggestions!

0 Kudos
Reply

566 Views
pgo
Senior Contributor V

Dear Eric,

 

It's a bit hard to respond without an example of the code.  Can you do a simple test case demonstrating the problem?

One question - Is it the entire structure that is at multiple addresses or is it that it is treating the fields at different offsets within the structure?

 

Some things to check:

  • Have you declared the structure as "extern" in a header file included in both source files.  The declaration should not have any initialization.
  • Have you defined it in ONE file only? It's a good idea to provided an initializer - even a dummy.
  • Have a look at the map file to see how many 'copies' of the global are being created.

bye

 

0 Kudos
Reply

566 Views
FridgeFreezer
Senior Contributor I

Declaring it static can also help.

0 Kudos
Reply