contiguous variable

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

contiguous variable

3,169件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Thu Apr 28 02:43:30 MST 2011
LPCXPresso 3.6 with LPC1758

If i write this defination:
uint16_t Var1, Var2;

in the map file the position it isn't contiguous.

COMMON         0x1000087c        0xc ./src/mpp/lib_xxxxx.o
                0x1000087c                Var2
                0x1000087e                Var1

How can I have the two variabile contiguous and ordered ?:
0 件の賞賛
返信
21 返答(返信)

3,120件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Fri May 06 07:26:42 MST 2011
The C standard does not make any guarantees regarding data order. Your assumption that you can control data order by manually placing variables in one particular order or another is bogus. Some compilers that you have used in the past may happen to have done this - but as the spec doesn't guarantee it, then it is not something you can rely upon and is not portable between toolchains.

Thus you must avoid writing code that depends on any assumed ordering.  If you require data ordering, then as already stated in this thread, you need to place the data items into a structure.

One way to minimise the amount of code change this might involve for you might be to use the preprocessor to map the variable name used in your code, onto the corresponding structure element. ARM used to have an application note that showed a method of doing this on their website, but as it related to a very old version of their toolchain - it has long been removed. However if you google for "application note 36 ARM", you can find copies of it floating around on the web. See the "globals.c" and "globals.h" part of section 4.

The other way of doing this might be to place the sections containing your variables explicitly using the linker script. If you want to do this, then I suggest starting your research on how to do so at:

http://support.code-red-tech.com/CodeRedWiki/EnhancedManagedLinkScripts

But to be honest, it would be best not to make the incorrect assumption in the first place.

Regards,
CodeRedSupport
0 件の賞賛
返信

3,120件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri May 06 07:21:58 MST 2011
There is nothing in the C language spec that says that variables defined consecutively have to be located consecutively, and thus compilers (and linkers) are free to do what they want with the locations. Very often, compilers/linkers will move things about for either size, performance or target architecture reasons.

Using a struct will cause the compiler to place your variables together. However, depending on the data types, they may not necessarily be consecutive, due to (possible) target architecture restrictions. For example, some architectures require data to be located on word or halfword boundaries. To get even better control of how to layout struct's, you will need to pack them. Packing is a compiler-specific extension and so you need to read your compiler documentation. For GNU compilers, you decorate your struct definition with  __attribute__ ((packed)). Most people define this as a macro so they can easily change it between compilers.

As I think you are defining two 16-bit variables, using GCC and ARM, you will be OK defining them in a struct and will not need to pack them. However, if you might want to use a different compiler or port to another architecture, you might want to consider packing.

Hope that helps,
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 06:16:59 MST 2011

Quote: igorsk

2. Why you "don't want" to use a structure? Is it a religion thing? Or do you suffer from structophobia?


I seek only an explanation, I have nothing against the structures that are very comfortable.
The problem is that I have to go to change the current program structure that is in an advanced state of development, but changes it, I'm not afraid.
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Fri May 06 06:08:21 MST 2011

Quote: blasiis
The problem is this:I'm at the end of the development and don't want make a structure, i find an alternative solution


1. Why do you [B]have to[/B] have the variables contiguous?
2. Why you "don't want" to use a structure? Is it a religion thing? Or do you suffer from structophobia?

It might be better if you describe the actual problem instead of how you think it should be solved.
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 06:07:24 MST 2011

Quote: Zero
I you aren't a newbie, why do you behave so :confused:
What did you develop for many years? Sorry to say this:


You ask if I know what an array . . . . :eek:

Quote: Zero

As experienced programmer you should know that since years (that's no Cortex issue). And if you are responsible for something in this project you could have used the simplest solution and changed your variables in an array within a few minutes / hours instead of wasting 8 days with kidding others.


I have worked with freescale, and the variable are everey time contiguous !
I know that the solution is to work with array, but I find a technical explanation befor change direction ! Only this, if it isn't possible i make an array, but it seems strange that there is no linker option to do this
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri May 06 05:54:13 MST 2011

Quote: blasiis
:D Yes of course, but I DON'T WANT USE A STRUCTURE.
Probably I cannot explain well in English
I'm not a newbie!
Development for many years on various processor, but recently with Cortex-M3
I need to order the address of single variabiles declared near, it is possible ?



If you aren't a newbie, why do you behave so :confused:
What did you develop for many years? Sorry to say this:
Even my 16 year old trainee knows what arrays are and how they are stored :mad:

As experienced programmer you should know that since years (that's no Cortex issue). And if you are responsible for something in this project you could have used the simplest solution and changed your variables in an array within a few minutes / hours instead of wasting 8 days with kidding others.
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 05:29:33 MST 2011

Quote: Zero
Do you know what an array is?


:D Yes of course, but I DON'T WANT USE A STRUCTURE.
Probably I cannot explain well in English
I'm not a newbie!
Development for many years on various processor, but recently with Cortex-M3

I need to order the address of single variabiles declared near, it is possible ?
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri May 06 05:08:50 MST 2011

Quote: blasiis
How do I say this to the compiler without using structures?




Quote:

That's a little bit long and therefore a short form was invented: arrays.

Do you know what an array is?
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 04:51:50 MST 2011

Quote: Zero

[B][COLOR=Red] "Please Compiler, put my variables in order !!"[/COLOR][/B]


How do I say this to the compiler without using structures?
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 04:34:53 MST 2011

Quote: Rob65

So this just leaves the array or struct as Zero suggested.
Use a struct or an array to define variables in contiguous memory.
This also is a nice way to identify all your configuration parameters:
Rob



The problem is this:I'm at the end of the development and don't want make a structure, i find an alternative solution
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Fri May 06 04:31:55 MST 2011

Quote:

It isn't' true that the compiler puts the variables in order!

Of course, it's a Compiler and not Harry Potter. If you want to put your variables ordered somewhere, you have to tell your Compiler:

[B][COLOR=Red] "Please Compiler, put my variables in order !!"[/COLOR][/B]

That's a little bit long and therefore a short form was invented: arrays.

Wiki:

Quote:

In computer science, an array data structure or simply array is a data structure consisting of a collection of elements (values or variables), each identified by at least one index. An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.
For example, an array of 10 integer variables, with indices 0 through 9, may be stored as 10 words at memory addresses 2000, 2004, 2008, … 2036, so that the element with index i has the address 2000 + 4 × i.

That's what described in #2 (28.04. = 8 days ago). So use arrays (for same data types) or otherwise structs.
Millions of C-Programmers are doing that and it's really working: you can access your data via address counter or pointer.
I know a lot wrong, bla-bla and Ga-Ga posts are confusing you (and me), but guessing isn't a solid way to solve problems.
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Fri May 06 03:27:05 MST 2011

Quote: blasiis

If I initialize the variabiles all is OK !
In order from left to right !



If you look at the example I posted I have initialized the variables and the addresses I received were contiguous.

I did test cleaning and building again many times and had it reverse the order of the variables twice in 30 builds.  They always remained contiguous though and that is probably because they are initialized.  Yes I am very patient.

It is good that we have done this exercise.  We now know that this compiler/linker does not do what we expect and have to code carefully to avoid this.

I have always used arrays or structs to keep my data contiguous/grouped and ordered and would not have noticed this problem with the compiler/linker without this thread.

To access the variables in the struct in Rob's example use this syntax:

a = ConfigData->param1;
b = ConfigData->param2;
...

where a and b are defined as:

uint8_t a,b;

Thank you blasiis for starting this thread.    I hope we have given you a solution that satisfies your requirements.

Larry
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Fri May 06 02:49:41 MST 2011
I am completely flabbergasted by this result, this is not what I would have expected from the linker.

I had to see this for myself to believe it :eek:

So this just leaves the array or struct as Zero suggested.
Use a struct or an array to define variables in contiguous memory.
This also is a nice way to identify all your configuration parameters:

typedef struct {
    uint8_t param1;
    uint8_t param2;
    uint16_t param3;
    uint32_t param4;
} ConfigParameters_t;

ConfigParameter_t ConfigData;
You have to take care of the sizes of the variables: an uint32_t always starts at a 4 byte boundary and an uint16_t on a 2 byte boundary.
If you leave out param2 from the structure above, this will just leave a 1 byte gap.
If order is not important and you want to prevent this from happening, specify all uint32_t variables first, followed by all unit16_t and then uint8_t. This also prevents gaps in your date (read: unused memory).

I'll stop reading (computer) books. Either the compiler or the linker optimizes the data in a way that is not clear to me.

Rob
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 02:09:51 MST 2011

Quote: larryvc
Hi blasiis,
However Rob65:
It could be that this occurs randomly and would require several cleans and builds to observe a swap of the variable placement in memory.



I have try this code :

uint8_t ProvaVar1, ProvaVar2;
In the map file :
                0x10000b54                ProvaVar2
                ......
                0x10000d1e                ProvaVar1

Total different !!!
It isnt' true that the compiler puts the variables in order!
I have also try to clen project many times

If I initialize the variabiles all is OK !

uint8_t ProvaVar1 = 0, ProvaVar2 = 0;


Maps:
.bss.ProvaVar1
                0x10000585        0x1 ./src/Poltrona.o
                0x10000585                ProvaVar1
.bss.ProvaVar2
                0x10000586        0x1 ./src/Poltrona.o
                0x10000586                ProvaVar2

In order from left to right !
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Fri May 06 01:39:23 MST 2011
Hi blasiis,

The answer that Rob65 gave you above is correct.  For what you described as your need for contiguous and ordered use Zero's recommendation of using an Array or a Struct for your 2 variables.

However Rob65:

I put this together to try to show how the compiler should work as you explained and how I thought it would.  So much for our books.  It appears that the compiler doesn't follow the rules.  Even when I changed the order of definition of the two variables Var1 always had the lower address assignment followed by Var2 being placed 2 bytes above.

It could be that this occurs randomly and would require several cleans and builds to observe a swap of the variable placement in memory.

This example uses semihosting.

/*
===============================================================================
 Name        : main.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
#include <stdio.h>
// TODO: insert other definitions and declarations here
uint16_t Var1, Var2;
 
int main(void) {
 
 Var1 = 5;
 Var2 = 10;
 printf("Var1 is at %08X and = %d\n", &Var1, Var1);
 printf("Var2 is at %08X and = %d\n", &Var2, Var2);
 
 // Enter an infinite loop, just incrementing a counter
 volatile static int i = 0 ;
 while(1) {
  i++ ;
 }
 return 0 ;
}


Larry
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 01:14:34 MST 2011

Quote: Rob65
A simple one?

You are stating that the two variables are not contiguous in memory but they are. each uint16_t variable contains 16 bits, that about as many as there are bits in 2 bytes :eek:
And it is a common known fact (it's documented in all the standard books about C I have read) that comma separated lists are parsed right to left.

Thus; your variables will be place in memory in the order as which you put them in your code but parsed right to left.
So:
will results in memory being reserved first for [B]Var2[/B] followed by 2 bytes reserver for [B]Var1[/B].
If [B]Var2[/B] is placed at address 0x1000087c then [B]Var1[/B] will be placed directly after this on the address that is exactly the number of bytes more than the size of your variable.
So what address would you expect the [B]Var1[/B] variable to be at?
Hint: just add 2 to 0x1000087c.

now look at your original posting again and tell me why you think the variables are not contiguous.
If you want them ordered, have a look at Zero's smashing answer or read above :p



Now I understand, I did not know that the variables are analyzed from right to left!
I invert the declaration and all is OK !!
All clear !

Thanks all
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Fri May 06 00:38:41 MST 2011

Quote: blasiis
What kind of question is that? :confused: How many bits are in 2 bytes ?:confused:



A simple one?

You are stating that the two variables are not contiguous in memory but they are. each uint16_t variable contains 16 bits, that about as many as there are bits in 2 bytes :eek:
And it is a common known fact (it's documented in all the standard books about C I have read) that comma separated lists are parsed right to left.

Thus; your variables will be place in memory in the order as which you put them in your code but parsed right to left.
So:

Quote:
uint16_t Var1, Var2;

will results in memory being reserved first for [B]Var2[/B] followed by 2 bytes reserver for [B]Var1[/B].
If [B]Var2[/B] is placed at address 0x1000087c then [B]Var1[/B] will be placed directly after this on the address that is exactly the number of bytes more than the size of your variable.
So what address would you expect the [B]Var1[/B] variable to be at?
Hint: just add 2 to 0x1000087c.

now look at your original posting again and tell me why you think the variables are not contiguous.
If you want them ordered, have a look at Zero's smashing answer or read above :p
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by blasiis on Fri May 06 00:13:45 MST 2011

Quote: larryvc
I was debating whether or not to give blasiis the answer.  In the end I also felt that it was more important that he try to understand what Zero was asking him.


I need the variabiles contiguous  because ther are the configuration parameters and read through the usb, having contiguous, I can refer to a single start address.
If they are scattered, I need many address !



Quote: larryvc
@blasiis,  A clue on contiguous. How many bits are in 2 bytes?:)


What kind of question is that? :confused: How many bits are in 2 bytes ?:confused:
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Fri Apr 29 13:11:04 MST 2011

Quote: Rob65
Whop, Zero is hitting you in the face with the correct solution :eek:



I was debating whether or not to give blasiis the answer.  In the end I also felt that it was more important that he try to understand what Zero was asking him.

@blasiis,  A clue on contiguous. How many bits are in 2 bytes?:)
0 件の賞賛
返信

3,122件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Fri Apr 29 07:51:36 MST 2011

Quote: Zero
Ordered: Try array or struct

Contiguous: Do you know what uint16_t is? If so, what do you expect?


Whop, Zero is hitting you in the face with the correct solution :eek:

The question of course is why you want this?

Rob
0 件の賞賛
返信