|
|
View previous topic :: View next topic |
Author |
Message |
ünloco
Joined: 02 Oct 2011 Posts: 12 Location: Tunisia
|
weird behaviour of "size of" |
Posted: Wed Nov 16, 2011 7:46 am |
|
|
hello
i have this array
Code: |
int nums[10][6] = {
{0x38,0x44,0x44,0x44,0x38,0x0}, //0
{0x40,0x7C,0x0,0x0,0x0,0x0}, //1
{0x4C,0x54,0x54,0x54,0x34,0x0}, //2
{0x44,0x54,0x54,0x54,0x28,0x0}, //3
{0x70,0x10,0x10,0x10,0x7C,0x0}, //4
{0x74,0x54,0x54,0x54,0x48,0x0}, //5
{0x38,0x54,0x54,0x54,0x8,0x0}, //6
{0x40,0x40,0x4C,0x70,0x0,0x0}, //7
{0x28,0x54,0x54,0x54,0x28,0x0}, //8
{0x20,0x54,0x54,0x54,0x38,0x0} //9
};
|
when i use a variable that contains the index (n1) then "size of" gives me bad values !!
Code: | n1 = 2;
width = sizeof nums[2]; //gives width = 6 and that's ok
width = sizeof nums[n1]; //gives width = 2 !!! |
what's wrong ?? _________________ for(;;); |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Nov 16, 2011 8:37 am |
|
|
Hmm... in C nums[0] is a pointer. Normally sizeof(a pointer) returns the size of the pointer, i.e. 2 in CCS generally. Using sizeof() with an array is a special case, and should the size of the array, not the the size of the pointer. C implements arrays with pointers remember and C arrays can be indexed or accessed through pointers interchangably. Note, if you had:
n1 = 2;
int *P = nums[2]; // P is now interchangable with nums[2].
then the following should happen:
width = sizeof nums[2]; //gives width = 6 and that's ok
width = sizeof nums[n1]; //gives width = 2 !!!
width = sizeof(P); // should be 2, i.e. sizeof(int *)
So, for some reason, CCS is interpreting sizeof(nums[2]) as the special case - tick VG - but sizeof(nums[n2]) as the normal, i.e. pointer case - not so good, must do better. The use of the variable is breaking the special case of sizeof for arrays... However the two are evaluated at very different times. sizeof(nums[2]) is in this case known at compile time, whereas sizeof(nums[n1]) has to be evaluated at run time. If nums[] was a ragged array, i.e. its elements not defined at compile time then sizeof(nums[2]) would also not be known at compile time. This makes sizeof() more tricky for compiler writers than we might at first expect.
RF Developer |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19554
|
|
Posted: Wed Nov 16, 2011 8:41 am |
|
|
Sizeof, is computed at compile time, so trying to access an element indexed by a variable simply won't work. You get '2', because the compiler decides you want the storage size of the pointer to the row represented, and this (of course) is 2.
I see RF_Developer has pointed this out as well, while I'm typing.
Best Wishes |
|
|
ünloco
Joined: 02 Oct 2011 Posts: 12 Location: Tunisia
|
|
Posted: Wed Nov 16, 2011 1:59 pm |
|
|
thank you guys !
now it's clearer as to why
i'll avoid such use
greetz _________________ for(;;); |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|