sizeof

This is a discussion on sizeof within the C Programming forums, part of the General Programming Boards category; in the C book by kerninghan and ritchie (pg 111) it is written that "C provides a compile-time unary operator ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    sizeof

    in the C book by kerninghan and ritchie (pg 111) it is written that
    "C provides a compile-time unary operator called sizeof that can be used to compute the size of any object"

    does this mean that sizeof's are computed and replaced by integer values at the compile time??

    what does a compile time unary operator mean??

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    The argument to sizeof() must be a known type at compile time......It should not be executable statement....and it take one argument only...that is why it is called compile time unary operator

  3. #3
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Well since C99 this is not correct anymore. Some (propably most) sizeof-statements are evaluated during compile time. Others are not, since C99 introduced VLAs (variabel length arrays) so the sizeof operator had to be modified to support VLAs.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    unary operator just means it takes one operand. A binary operator for example is /, because you need something/something (two operands).

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by TactX
    Well since C99 this is not correct anymore. Some (propably most) sizeof-statements are evaluated during compile time. Others are not, since C99 introduced VLAs (variabel length arrays) so the sizeof operator had to be modified to support VLAs.
    This is a new concept for me...plz explain it with some example

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        unsigned int x;
        printf("Enter array size: ");
        fflush(stdout);
        scanf("%u", &x); /* check for valid input in real code */
        int k[x]; /* C99 variable array, C99 also allows declarations anywhere */
        printf("sizeof(k) computed at runtime: %zd\n", sizeof k);
        return 0;
    }
    My output:
    Code:
    Enter array size: 100
    sizeof(k) computed at runtime: 400
    Edit: change array from char to int, to illustrate better.
    Last edited by cwr; 10-18-2005 at 07:50 AM.

  7. #7
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Amazing.....it worked on dev C++..but didn't worked on VC6.0(i guess its old)......

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Microsoft don't seem to be interested in C99 support.

  9. #9
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by TactX
    Well since C99 this is not correct anymore. Some (propably most) sizeof-statements are evaluated during compile time. Others are not, since C99 introduced VLAs (variabel length arrays) so the sizeof operator had to be modified to support VLAs.
    i searched a bit more on this.....VLA's are pointers in C99.
    That is, sizeof(VLA)==sizeof(Type *)==4 for a 32 bits compiler!....so my statement was perfectly right
    Last edited by sunnypalsingh; 10-18-2005 at 08:33 AM.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by sunnypalsingh
    i searched a bit more on this.....VLA's are pointers in C99.
    That is, sizeof(VLA)==sizeof(Type *)==4 for a 32 bits compiler!
    Then you searched the wrong sites. VLA = variable length array. Arrays are not pointers.
    sizeof(VLA) can easily (and I'd bet probably) turned into:
    sizeof(type) * number of items. Though to be sure I'd have to look at the assembly outputted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 07:00 PM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21