Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   sizeof question (http://www.programmingforums.org/showthread.php?t=15773)

JD-Salinger May 4th, 2008 1:26 AM

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
:

  1.  
  2. const int maxIdFun=14;
  3.  
  4. FunctionEntry FunctionArray [] =
  5. {
  6.     log,  "log",
  7.     log10,"log10",
  8.     exp,  "exp",
  9.     sqrt, "sqrt",
  10.     sin,  "sin",
  11.     cos,  "cos",
  12.     tan,  "tan",
  13.     CoTan,"cotan",
  14.     sinh, "sinh",
  15.     cosh, "cosh",
  16.     tanh, "tanh",
  17.     asin, "asin",
  18.     acos, "acos",
  19.     atan, "atan",
  20. };
  21.  
  22. FunctionTable::FunctionTable(SymbolTable& symTab,FunctionEntry* funArr)
  23.     :_size(0)
  24. {
  25.  
  26.     assert(maxIdFun==sizeof FunctionArray/sizeof FunctionArray[0])
  27.  
  28.     for(int i=0; i<maxIdFun; ++i)
  29.     {
  30.         int len=strlen(funArr[i].strFun);
  31.         if(len==0)
  32.             break;
  33.         _pFun[i]=funArr[i].pFun;
  34.         cout<< funArr[i].strFun<<endl;
  35.         int j=symTab.ForceAdd(funArr[i].strFun,len);
  36.         assert(i==j);
  37.         ++_size;
  38.     }
  39. }


thanks a lot

Sane May 4th, 2008 1:40 AM

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.

Sorrofix May 4th, 2008 1:48 AM

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:

JD-Salinger May 4th, 2008 6:35 AM

Re: sizeof question
 
Quote:

Originally Posted by Sorrofix (Post 144825)

And of course, don't use it on a pointer. Even if they do seem a lot like arrays... :icon_rolleyes:

it returns 4bytes as always? even in a pointer? and y cant i use it in a pointer?
anyway tnx sane

Sane May 4th, 2008 12:19 PM

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.

JD-Salinger May 4th, 2008 8:22 PM

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