CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    incrementing value in sizeof

    Why can't we increment the value of i in sizeof like this:-

    Code:
    sizeof(i++)
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: incrementing value in sizeof

    Quote Originally Posted by sunnypalsingh
    Why can't we increment the value of i in sizeof like this:-

    Code:
    sizeof(i++)
    The sizeof() is a compile-time constant value depending on the argument. The argument to sizeof() must be a known type at compile time. The "i++" is not a type, it's an executable statement.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: incrementing value in sizeof

    This expression is valid (and returns the size of the return value of the expression i++), but it does not increment i.
    In fact, most expressions can be used with sizeof operator, but sizeof operator never evaluates the expression!
    -1- The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is not evaluated, or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (basic.fundamental) is implementation-defined. [Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.*
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: incrementing value in sizeof

    Thanx a lot
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  5. #5
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: incrementing value in sizeof

    Quote Originally Posted by Paul McKenzie
    The sizeof() is a compile-time constant value depending on the argument. The argument to sizeof() must be a known type at compile time. The "i++" is not a type, it's an executable statement.
    Regards,
    Paul McKenzie
    According to the C99 standard..isn't this statement becomes wrong.....Since C99 introduced VLAs (variable length arrays) so the sizeof operator had to be modified to support VLAs.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: incrementing value in sizeof

    Quote Originally Posted by sunnypalsingh
    According to the C99 standard..isn't this statement becomes wrong.....Since C99 introduced VLAs (variable length arrays) so the sizeof operator had to be modified to support VLAs.
    // EDIT : What follows is the original post, which is false!

    No, variable length arrays are pointers in C99.
    That is, sizeof(VLA)==sizeof(Type *)==4 for a 32 bits compiler!
    Last edited by SuperKoko; October 18th, 2005 at 09:38 AM. Reason: What i said is false
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: incrementing value in sizeof

    okie.....got it
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: incrementing value in sizeof

    Quote Originally Posted by sunnypalsingh
    According to the C99 standard..isn't this statement becomes wrong.....Since C99 introduced VLAs (variable length arrays) so the sizeof operator had to be modified to support VLAs.
    Sorry, i made an error:
    That code:
    Code:
    #include <stdio.h>
    
    int main()
    	{
    	unsigned n;
    	
    	scanf("%u",&n);
    	int VLA[n];
    	printf("%u",sizeof(VLA));
    	}
    Outputs 4*the_number_entered!

    So, VLA are really some different thing!
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  9. #9
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: incrementing value in sizeof

    Quote Originally Posted by SuperKoko
    Sorry, i made an error:
    That code:
    Code:
    #include <stdio.h>
    
    int main()
    	{
    	unsigned n;
    	
    	scanf("%u",&n);
    	int VLA[n];
    	printf("%u",sizeof(VLA));
    	}
    Outputs 4*the_number_entered!

    So, VLA are really some different thing!
    You know what...i am actually feeling great....
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured