EmbeddedRelated.com
Forums

filling remaining array elements with fixed value

Started by blisca June 12, 2014
Hi to all here


Using Codewarrior,GNU i want to declare an  array in program memory like 
this:

const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF};

Is there any convenient way to do it if the array is 1024 elements wide?

Many thanks

Diego
> Using Codewarrior,GNU i want to declare an array in program memory like > this: > > const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; > > Is there any convenient way to do it if the array is 1024 elements wide?
#define TEN_FFS 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF const unsigned char my_array[8]={ 0xA, 0xB, 0xC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS }; #undefine TEN_FFS Wouter van Ooijen
> #define TEN_FFS 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF > const unsigned char my_array[8]={ > 0xA, 0xB, 0xC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF > TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, > TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS, TEN_FFS > }; > #undefine TEN_FFS
I misread, you want 1000, not 100, but that requires only two more lines ;)
blisca <blisca@tiscali.it> writes:

> Hi to all here > > > Using Codewarrior,GNU i want to declare an array in program memory > like this: > > const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; > > Is there any convenient way to do it if the array is 1024 elements wide?
You could write a piece of code that pretty prints the C code you want to use to init your array? Alternatively, there's a GNU extension in gcc to specify ranges in arrays. Something along the lines of const unsigned char my_array[1024] = {0xA, 0xB, 0xC, [3 ... 1023] = 0xFF};
On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote:

> Hi to all here > > > Using Codewarrior,GNU i want to declare an array in program memory like > this: > > const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; > > Is there any convenient way to do it if the array is 1024 elements wide? > > Many thanks > > Diego
my_array[1024] = {[0 ... 1023] = 0xFF, [0]=0x0A, [1]=0x0B, [2]=0x0C}; From the gcc docs "If the same field is initialized multiple times, it will have value from the last initialization." search for gcc array initializer -- Chisolm Republic of Texas
On Thu, 12 Jun 2014 09:31:33 -0500, Joe Chisolm wrote:

> On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote: > >> Hi to all here >> >> >> Using Codewarrior,GNU i want to declare an array in program memory >> like this: >> >> const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; >> >> Is there any convenient way to do it if the array is 1024 elements >> wide? >> >> Many thanks >> >> Diego > > my_array[1024] = {[0 ... 1023] = 0xFF, [0]=0x0A, [1]=0x0B, [2]=0x0C}; > > From the gcc docs "If the same field is initialized multiple times, it > will have value from the last initialization." > > search for gcc array initializer
I wouldn't want to trust that for code that needs to live a long life. If your product life cycle is only a year or two, then that looks like a nifty feature to exploit. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote:

> Hi to all here > > > Using Codewarrior,GNU i want to declare an array in program memory like > this: > > const unsigned char my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; > > Is there any convenient way to do it if the array is 1024 elements wide?
First, remember that what goes into memory is an image of the whole array, so you're not "wasting" any space in memory or the hex file by defining it -- at worst you're just making a .c file bigger. Second, it's not uncommon in code that I've authored to find arrays that are generated by an application or a script into a .c file, and then compiled. This really works best if you're using a makefile: you define a .o file to be dependent on a .c (or .cpp) file, then you define that .c (or .cpp) file to be dependent on the script, then you give a rule for generating the .c file from the script. That way, you're keeping your source straight. I usually have my script generate a comment at the head of the .c file along the lines of "machine generated file, do not put under version control!!" Third, if you're only ever going to define it once, and if you don't like Wouter's suggestion, why not just define all 1024 characters? At 8 characters per line, that's only 128 lines. That's long, but not absurdly so. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On Thu, 12 Jun 2014 11:12:02 -0500, Tim Wescott wrote:

> On Thu, 12 Jun 2014 09:31:33 -0500, Joe Chisolm wrote: > >> On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote: >> >>> Hi to all here >>> >>> >>> Using Codewarrior,GNU i want to declare an array in program memory >>> like this: >>> >>> const unsigned char >>> my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; >>> >>> Is there any convenient way to do it if the array is 1024 elements >>> wide? >>> >>> Many thanks >>> >>> Diego >> >> my_array[1024] = {[0 ... 1023] = 0xFF, [0]=0x0A, [1]=0x0B, [2]=0x0C}; >> >> From the gcc docs "If the same field is initialized multiple times, it >> will have value from the last initialization." >> >> search for gcc array initializer > > I wouldn't want to trust that for code that needs to live a long life. > If your product life cycle is only a year or two, then that looks like a > nifty feature to exploit.
Why would you not trust it for a long life product. Granted the OP would be better with some defines with better names. The array init feature has been in gcc for something like 10 years now. I dont think they are going to rip it out. You have to be careful about initializer values with side effects but that is not the issue here. -- Chisolm Republic of Texas
On Thu, 12 Jun 2014 12:27:05 -0500, Joe Chisolm wrote:

> On Thu, 12 Jun 2014 11:12:02 -0500, Tim Wescott wrote: > >> On Thu, 12 Jun 2014 09:31:33 -0500, Joe Chisolm wrote: >> >>> On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote: >>> >>>> Hi to all here >>>> >>>> >>>> Using Codewarrior,GNU i want to declare an array in program memory >>>> like this: >>>> >>>> const unsigned char >>>> my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; >>>> >>>> Is there any convenient way to do it if the array is 1024 elements >>>> wide? >>>> >>>> Many thanks >>>> >>>> Diego >>> >>> my_array[1024] = {[0 ... 1023] = 0xFF, [0]=0x0A, [1]=0x0B, [2]=0x0C}; >>> >>> From the gcc docs "If the same field is initialized multiple times, it >>> will have value from the last initialization." >>> >>> search for gcc array initializer >> >> I wouldn't want to trust that for code that needs to live a long life. >> If your product life cycle is only a year or two, then that looks like >> a nifty feature to exploit. > > Why would you not trust it for a long life product. Granted the OP > would be better with some defines with better names. The array init > feature has been in gcc for something like 10 years now. I dont think > they are going to rip it out. You have to be careful about initializer > values with side effects but that is not the issue here.
Because even if I could trust gcc not to rip it out, I couldn't trust that, over the lifetime of the code, the need to change tools wouldn't arise. I've been in industry for over 20 years. Using some clever non-standard compiler feature -- or even some feature that's in some clever but dusty corner of the standard -- makes you look like a hero for about a year, then makes you look like a fool. Over time, one becomes allergic. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On 12/06/14 19:54, Tim Wescott wrote:
> On Thu, 12 Jun 2014 12:27:05 -0500, Joe Chisolm wrote: > >> On Thu, 12 Jun 2014 11:12:02 -0500, Tim Wescott wrote: >> >>> On Thu, 12 Jun 2014 09:31:33 -0500, Joe Chisolm wrote: >>> >>>> On Thu, 12 Jun 2014 10:30:37 +0200, blisca wrote: >>>> >>>>> Hi to all here >>>>> >>>>> >>>>> Using Codewarrior,GNU i want to declare an array in program memory >>>>> like this: >>>>> >>>>> const unsigned char >>>>> my_array[8]={0xA,0xB,0xC,0xFF,0xFF,0xFF,0xFF,0xFF}; >>>>> >>>>> Is there any convenient way to do it if the array is 1024 elements >>>>> wide? >>>>> >>>>> Many thanks >>>>> >>>>> Diego >>>> >>>> my_array[1024] = {[0 ... 1023] = 0xFF, [0]=0x0A, [1]=0x0B, [2]=0x0C}; >>>> >>>> From the gcc docs "If the same field is initialized multiple times, it >>>> will have value from the last initialization." >>>> >>>> search for gcc array initializer >>> >>> I wouldn't want to trust that for code that needs to live a long life. >>> If your product life cycle is only a year or two, then that looks like >>> a nifty feature to exploit. >> >> Why would you not trust it for a long life product. Granted the OP >> would be better with some defines with better names. The array init >> feature has been in gcc for something like 10 years now. I dont think >> they are going to rip it out. You have to be careful about initializer >> values with side effects but that is not the issue here. > > Because even if I could trust gcc not to rip it out, I couldn't trust > that, over the lifetime of the code, the need to change tools wouldn't > arise. > > I've been in industry for over 20 years. Using some clever non-standard > compiler feature -- or even some feature that's in some clever but dusty > corner of the standard -- makes you look like a hero for about a year, > then makes you look like a fool. > > Over time, one becomes allergic. >
I also have been in the industry for 20 years. And over the lifetime of a project, I almost never change tools. Just a few weeks ago I had to make changes to a program that I wrote 18 years ago - I compiled it with the same compiler I used 18 years ago (despite having much newer and much better tools for the same target). So if I write code that takes advantage of a particular feature of a compiler, I know that it will still work in the future. And in practice, I seldom write code that does not involve at least some compiler-specific feature somewhere in the project (such as pragmas or function attributes for interrupt functions, etc.). Using compiler features like this one is perfectly safe. Within the project, you will be (or should be!) using the same tools all the time - thus it still works in the same project. And if you re-use the code in a different project, then it either works fine (because you are using gcc again, or a compiler that supports the same extensions), or you get an error from your compiler. It is /highly/ unlikely that some other compiler will silently accept this syntax but interpret it in a different way. Of course if you know (or strongly suspect) that the code you are writing has to work across a range of compilers, then you must avoid extensions. But if you are writing project-specific code, or just use the one compiler, then take advantage of any extensions that lead to cleaner and neater code - just as you use other features of your tools.