Trouble placing variables at specific locations in memory

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

Trouble placing variables at specific locations in memory

Jump to solution
749 Views
RWey
Contributor I

Using CW 10.2,  I've figured out how to create and locate a "section" in order to place variables,  as such ...

 

 

#pragma force_active on

#pragma define_section version ".version" far_abs R

#pragma section version begin

 

__declspec( version ) const U16 fwMajor = SOFTWARE_VERSION_MAJOR;

__declspec( version )  const U16 fwMinor = SOFTWARE_VERSION_MINOR;

__declspec( version )  const U16 fwBuild = SOFTWARE_VERSION_BUILD;

__declspec( version )  const U16 fwRevision = SOFTWARE_VERSION_REVISION;

 

#pragma section version end

 

 

When I inspect the MAP file, the constants are placed where expected ( as defined in my LCF file ).   The order of the constants, however, is not the same order as they appear in their declaration.

 

Question:  Is there another pragma or attribute that can be used to keep the order of variables within a section the same as their declaration order ?

 

Thanks,

R.W.

Labels (1)
Tags (2)
0 Kudos
1 Solution
410 Views
CrasyCat
Specialist III

Hello

In ANSI C there is no guarantee at all that the variables will be allocated in the sequence in which they are defined,

The linker can decide to allocate them in any order.

If you need the different values to be allocated in a specific sequence, you can define them in a structure and then access the various fields inside of the structure using macros.

For example:

__declspecc(version ) const struct {

  UI16 major;

  UI16 minor;

  UI16 build;

  UI16 revision

} fw_version_nbr = {SOFTWARE_VERSION_MAJOR, SOFTWARE_VERSION_MINOR, SOFTWARE_VERSION_BUILD, SOFTWARE_VERSION_REVISION};

#define fwMajor fw_version_nbr.major

#define fwMinor fw_version_nbr.minor

#define fwBuild fw_version_nbr.build

#define fwRevision fw_version_nbr.revision

This should be equivalent and should keep the existing code compatible,


CrasyCat

View solution in original post

0 Kudos
2 Replies
411 Views
CrasyCat
Specialist III

Hello

In ANSI C there is no guarantee at all that the variables will be allocated in the sequence in which they are defined,

The linker can decide to allocate them in any order.

If you need the different values to be allocated in a specific sequence, you can define them in a structure and then access the various fields inside of the structure using macros.

For example:

__declspecc(version ) const struct {

  UI16 major;

  UI16 minor;

  UI16 build;

  UI16 revision

} fw_version_nbr = {SOFTWARE_VERSION_MAJOR, SOFTWARE_VERSION_MINOR, SOFTWARE_VERSION_BUILD, SOFTWARE_VERSION_REVISION};

#define fwMajor fw_version_nbr.major

#define fwMinor fw_version_nbr.minor

#define fwBuild fw_version_nbr.build

#define fwRevision fw_version_nbr.revision

This should be equivalent and should keep the existing code compatible,


CrasyCat

0 Kudos
410 Views
RWey
Contributor I

Yes, I was thinking a structure might be the only way, and after changing it to that method, it will work.  I was just wondering if there was some sort of "Order" pragma.

A structure it is.

0 Kudos