Trouble placing variables at specific locations in memory

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Trouble placing variables at specific locations in memory

ソリューションへジャンプ
763件の閲覧回数
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.

ラベル(1)
タグ(2)
0 件の賞賛
1 解決策
424件の閲覧回数
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 件の賞賛
2 返答(返信)
425件の閲覧回数
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 件の賞賛
424件の閲覧回数
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 件の賞賛