Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 4th, 2008, 12:26 AM   #1
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1 JD-Salinger is on a distinguished road
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
c++ Syntax (Toggle Plain Text)
  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
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 4th, 2008, 12:40 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old May 4th, 2008, 12:48 AM   #3
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 16
Rep Power: 0 Sorrofix is on a distinguished road
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...
Sorrofix is offline   Reply With Quote
Old May 4th, 2008, 5:35 AM   #4
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1 JD-Salinger is on a distinguished road
Re: sizeof question

Quote:
Originally Posted by Sorrofix View Post

And of course, don't use it on a pointer. Even if they do seem a lot like arrays...
it returns 4bytes as always? even in a pointer? and y cant i use it in a pointer?
anyway tnx sane
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old May 4th, 2008, 11:19 AM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old May 4th, 2008, 7:22 PM   #6
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 59
Rep Power: 1 JD-Salinger is on a distinguished road
Re: sizeof question

ok... thnx sane
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual pet site MySQL question proudnerd PHP 6 May 13th, 2006 7:22 PM
C++ template and namespace question Soulstorm C++ 3 Jan 22nd, 2006 2:46 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC