Thread: Why "sizeof" is an operator, not a function?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why "sizeof" is an operator, not a function?

    Why "sizeof" is an operator, not a function?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because it wouldn't make sense as a function.

    http://en.wikipedia.org/wiki/Sizeof

    It's a compiler-related operator. The compiler replaces all sizeof "calls" or operations with the actual size_t value.

    Meaning if I write this:

    Code:
    char szBuffer[100];
    
    ...
    
    fgets(szBuffer,sizeof(szBuffer),stdin);
    The compiler will replace sizeof(szBuffer) with 100 before it's even compiled. Most of the time, sizeof() is required in C for functions calls where arrays are passed as pointers, and the size of the array in question is needed.

    C++ requires the usage of sizeof much less than C since these details are usually hidden by other means. For example, sizeof would be good to use in a malloc() call in C, but C++ has new for memory allocation which handles the sizing issues automatically based upon the type and sizeof is not needed.

    Back to your question, a function would be pointless since the discovery of types and their sizes is done at compile time. Now in cases where the size of something needs to be known at run time.... that's a different ballgame.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    From http://www.ibm.com/developerworks/po...ry/pa-ctypes2/:
    In C89, all arrays had to have a constant size at compile time; C99 introduces two exceptions. The first is variably modified array types. A variably-modified type produces a variable-length array. VLAs are new in C99, although many compilers supported them previously. A VLA is the one exception to the rule that sizeof is a compile-time constant expression. VLAs have mostly the same semantics as regular arrays. They cannot be declared at file scope.
    Edit: Never mind, I got this mixed up with the C forum. I think you're right that in C++, there are no exceptions to the rule. The linked article was talking about C.
    Last edited by robatino; 07-10-2007 at 11:30 AM.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Probably because there would be no way to code it as a function using the existing keywords and operators.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM