A
Andreas Eibach
Hi,
when I had in mind to turn this code (which worked fine in main()) to a
separate function, I stumbled upon this...(of course, it's way bigger than
that, but I hope this short snippet can demonstrate the issue anyway)
-begin code (Ì) -
int main(void)
{
unsigned char rawdata[96] =
{0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */
unsigned long x = 0;
int i;
for(i=0; i < sizeof(rawdata)/(sizeof(unsigned long)); i++)
{
...
x = ((rawdata[i*4+0] << 8) | .....;
/* ... */
}
return 0;
}
-end code (I)-
The for() line contains an i < sizeof(rawdata). In main(), this causes no
problems, however, if we (commonly) assume that the function-internal
variables are completely different, we *may* run into trouble...
-begin code (II)-
unsigned long doMagic (unsigned char[]);
int main(void)
{
unsigned char rawdata[64] = {
0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */
unsigned long x = doMagic(rawdata);
... /* e. g. printf output */
return 0;
}
unsigned long doMagic (unsigned char arr[])
{
int i;
for(i=0; i < sizeof(arr)/(sizeof(unsigned long)); i++)
{
...
x = ((arr[i*4+0] << 8) | ....... ;
}
}
-end code (II)
For an absolute beginner, this looks "working fine", but it does not.
sizeof(arr) is *NOT* the size of the array (just as I too thought before),
but merely the size of the small pointer (here it was 4). This caused the
for loop to get exited prematurely.
The only way out with the portable solution was to write a different
doMagic():
unsigned long doMagic (unsigned char arr[], unsigned int len);
and then *externally* enter the function using sizeof(rawdata) as length
"info".
This worked fine.
But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it "pre-chewed" by
the calling routine?
-Andreas
when I had in mind to turn this code (which worked fine in main()) to a
separate function, I stumbled upon this...(of course, it's way bigger than
that, but I hope this short snippet can demonstrate the issue anyway)
-begin code (Ì) -
int main(void)
{
unsigned char rawdata[96] =
{0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */
unsigned long x = 0;
int i;
for(i=0; i < sizeof(rawdata)/(sizeof(unsigned long)); i++)
{
...
x = ((rawdata[i*4+0] << 8) | .....;
/* ... */
}
return 0;
}
-end code (I)-
The for() line contains an i < sizeof(rawdata). In main(), this causes no
problems, however, if we (commonly) assume that the function-internal
variables are completely different, we *may* run into trouble...
-begin code (II)-
unsigned long doMagic (unsigned char[]);
int main(void)
{
unsigned char rawdata[64] = {
0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */
unsigned long x = doMagic(rawdata);
... /* e. g. printf output */
return 0;
}
unsigned long doMagic (unsigned char arr[])
{
int i;
for(i=0; i < sizeof(arr)/(sizeof(unsigned long)); i++)
{
...
x = ((arr[i*4+0] << 8) | ....... ;
}
}
-end code (II)
For an absolute beginner, this looks "working fine", but it does not.
sizeof(arr) is *NOT* the size of the array (just as I too thought before),
but merely the size of the small pointer (here it was 4). This caused the
for loop to get exited prematurely.
The only way out with the portable solution was to write a different
doMagic():
unsigned long doMagic (unsigned char arr[], unsigned int len);
and then *externally* enter the function using sizeof(rawdata) as length
"info".
This worked fine.
But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it "pre-chewed" by
the calling routine?
-Andreas