sizeof operator to get indice to last element of array

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

sizeof operator to get indice to last element of array

2,503 Views
YeOldeBDM
Contributor III
I cant seem to get the syntax right for the sizeof operator, or it is not implemented on CodeWarrior 5.1 for HCS08.
 
To get the number of bytes in an array of char called array_name, isn't it:
 
#define NUM_BYTES sizeof(array_name[])
 
I am using NUM_BYTES to determine how many elements are in the array. Of course I know this at compile time for my simple app, but if I add another element, I don't want to have to adjust all my for() loops that reference the array, ie
 
for(i=0;i<NUM_BYTES-1;i++)
{ ....foo...}
does not need to be updated if the array size is changed
Labels (1)
0 Kudos
5 Replies

463 Views
YeOldeBDM
Contributor III
ok, here is a made up array
foo(a)
{
#pragma INTO_ROM
 const unsigned char somearray[]        = {     0x01,
                                            0x02,
                                            0x03,
                                            0x04,
                                            0x05,
                                            0x06,
                                            0x07,
                                            0x08,
                                            0x09,
                                            0x0A}; /* just some made up numbers*/
 
#define NUM_BYTES sizeof(somearray)
 
int i;
i=NUM_BYTES;
...
...
...
}
 
i gets loaded with 0.
 
Are you saying that somearray must be declared as global or static in order for i to be computed correctly?
 
0 Kudos

463 Views
Lundin
Senior Contributor IV
I don't use INTO_ROM myself, so I'm not sure if it does anything helpful to you:

"This pragma forces the next (non-constant) variable definition to be const (together with the -Cc compiler option)."

Since "somearray" is const, maybe it is making the 'i' variable const instead?
0 Kudos

463 Views
CompilerGuru
NXP Employee
NXP Employee
Pragma INTO_ROM does not affect the sizeof operator, so these are two different issues.
About pragma INTO_ROM: this pragma is only for the HIWARE object file format which noone I know is using these days.
So my simple advice: Don't use it. Using it wont help. It wont hurt either, but it does confuse.

About the sizeof issue. The Freescale compiler uses sizeof according to the ANSI-C standard. There are a few common traps to look out for when using sizeof tough.
Don't use sizeof for argument arrays. C does not support array to be passed by value, so the thing which looks like an argument array is in reality a pointer. I'm not even sure what sizeof returns for this case, I guess it returns the size of the pointer.
And a second ANSI C issue is to use sizeof with undefined objects/types. I guess this is what happened here orininally, although not sure.
This setup looks like this:

header.h:
extern const char arr[];
header.c:
#include "header.h"
const char arr[]= "Hallo";
main.c:
#include "header.h"
const int arr_length= sizeof(arr)/sizeof(arr[0]);

For this pattern, the arr_lengt does not work as sizeof is needing an array definition, the declaration in the header is not enough to compute the size.
If this is the setup, I use usually something like this:

header.h:
extern const char arr[];
header.c:
extern const int arr_length;
#include "header.h"
const char arr[]= "Hallo";
const int arr_length= sizeof(arr)/sizeof(arr[0]);
main.c:
#include "header.h"


As additional note, using a local, const and non static array should actually work fine. However it really is inefficient as it has to be allocated and initialized on the stack. Making it global or static will be a lot faster and generate less code.

The original code snippet (after making it compile) does give me a 10, so I'm not able to reproduce the problem with it.

Daniel



3: #pragma INTO_ROM
4: const unsigned char somearray[] = { 0x01,
5: 0x02,
6: 0x03,
7: 0x04,
8: 0x05,
9: 0x06,
10: 0x07,
11: 0x08,
12: 0x09,
13: 0x0A}; /* just some made up numbers*/
14:
15: #define NUM_BYTES sizeof(somearray)
16:
17: int i;
18: i=NUM_BYTES;
19: return i;
0000 a60a [2] LDA #10
0002 5f [1] CLRX
20: }
0003 81 [4] RTS
0 Kudos

463 Views
YeOldeBDM
Contributor III
Thanks Daniel for your insight on sizeof and INTO_ROM. (and also thanks for another reply you gave regarding checksumming a banked architecture in the Codewarrior Forum a few weeks ago)
 
I have to plead guilty regarding my last post. I checked the results using the debugger and did not inspect the assembly. The 'i' in my post got loaded with 0 because it was optimized away. I just set up the snippet and never did anything with 'i'.
 
Next time I will look at the dis-assembled code.
0 Kudos

463 Views
Lundin
Senior Contributor IV
#define NUM_BYTES sizeof(array_name[])

Well, what does your compiler tell you? Syntax error. In case of a staticly allocated array, the syntax is sizeof(array_name).

In case of pointers or dynamic arrays, you need to keep track of the size with a variable since they aren't known at compile time.

Message Edited by Lundin on 2006-09-15 08:19 AM

0 Kudos