![]() |
sizeof question
in line 26
assert(maxIdFun==sizeof FunctionArray/sizeof FunctionArray[0]) what is the size of functionArray and size of functionArray[0] i cant understand that... and how come it would equal to maxIdFun... and the point of dividing it :
thanks a lot |
Re: sizeof question
The purpose of that statement is to assert that there are actually 14 elements in the array "FunctionArray". It accomplishes this by dividing the total number of bytes in the array (sizeof FunctionArray) by the number of bytes in the first element of the array (sizeof FunctionArray[0]). Since the size of the first element is the same as every other element, the resulting quotient is the number of elements in the array. If this doesn't equal maxIdFun, we have a problem, since maxIdFun is used to determine which elements in the array are accessable.
But beware, this kind of sizeof trick can only be used on arrays where the compiler knows the size of it in advance. |
Re: sizeof question
>But beware, this kind of sizeof trick can only be used on
>arrays where the compiler knows the size of it in advance. And of course, don't use it on a pointer. Even if they do seem a lot like arrays... :icon_rolleyes: |
Re: sizeof question
Quote:
anyway tnx sane |
Re: sizeof question
Yes. You can't "use it on a pointer" because the compiler doesn't know the size of its array in advance. If you need the size of a dynamically allocated array, or a pointer to an array, you need to keep track of the size yourself.
|
Re: sizeof question
ok... thnx sane
|
| All times are GMT -5. The time now is 4:20 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC