4

The sizeof operator is a compile time operator but in the program below it is changing at run time.

#include <stdio.h>

void func (int i) { 
    int a[i]; 
    printf("%d \n", sizeof(a)); 
} 

main() { 
    int i = 0; 
    while(i <= 5) { 
        func(i); 
        i++; 
    } 
}

memory will be allocated at runtime. how the compiler will calculate structure size with out structure padding?

Dileep Nunna
  • 117
  • 3
  • 11
  • 2
    Please post some syntactically valid code. – juanchopanza Aug 11 '14 at 07:23
  • Is this program executing?? – shashank Aug 11 '14 at 07:25
  • 2
    This code is neither valid C nor valid C++. – Kerrek SB Aug 11 '14 at 07:25
  • 2
    `int a[i];` is illegal in C++. Some compiler may offer it as an extension; you'd have to consult that compiler's documentation to find out what the consequences of using that array is. – M.M Aug 11 '14 at 07:27
  • @MattMcNabb http://stackoverflow.com/questions/10062621/difficulty-in-understanding-variable-length-arrays-in-c – Peter Clark Aug 11 '14 at 07:27
  • 1
    VLAs are valid for C99 or later, but then C99 or later also disallows implicit `int`...yay? – T.C. Aug 11 '14 at 07:27
  • @MattMcNabb Acording to [this answer](http://stackoverflow.com/a/10062633/2507444) variable length arrays are valid in C99 or later (as T.C. stated) – Peter Clark Aug 11 '14 at 07:29
  • 1
    @PeterClark C++ is not a "later" version of C99 – M.M Aug 11 '14 at 07:34
  • Each time you call the function with different parameter,you are invoking the same array with different length. In C you cannot change the length of array once you declare it. So this is invalid code. – shashank Aug 11 '14 at 07:44
  • @shashank Not true. As already mentioned C99 and later allow variable length arrays so that part is completely valid (although the implicit int on main is dodgy). – Nigel Harper Aug 11 '14 at 08:19
  • This code _is_ invalid, but only (for C) the first call to `func` with `i` being 0. There are no zero-size objects in C. – mafso Aug 11 '14 at 10:05

3 Answers3

6

Your information is outdated. a is a variable-length array; for those, sizeof is determined at runtime. Variable-length arrays are a C99 feature that did not exist when the source of your information was written.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • i think sizeof is a compile time operator. is it executed at run time? can u please tell me.main(){int a=10,b;b=sizeof(++a);printf("%d %d \n",a,b);} – Dileep Nunna Aug 11 '14 at 07:34
  • The C standard doesn't distinguish "compile time" and "run time", that is a pedagogical distinction that is useful sometimes... when talking about `sizeof` since 1999, this is not one of those times :) `sizeof` on an object gives you the number of bytes in that object , regardless of what sort of object it is. – M.M Aug 11 '14 at 07:39
  • @user3637903 `sizeof` is a compile time operator, but VLA is an exception (and the only exception) to that rule, i.e, `sizeof` is only not compile time operator when the operand is a VLA. – Yu Hao Aug 11 '14 at 07:40
2

VLA-s are pretty much the only case where sizeof is not a compile time constant.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

a[i] in not legal in Standard C++, though it's supported as an extension by some compilers (e.g. GCC documents it here) - such an extension might arrange whatever behaviour it likes for the sizeof operator, but would most sensibly base it on the C behaviour.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • memory will be allocated at runtime. how the compiler will calculate structure size with out structure padding? – Dileep Nunna Sep 23 '14 at 16:45
  • @user3637903 the compiler must include any padding, but to do so it simply needs to multiply `i` by the size of the array elements *rounded up if necessary to a multiple of the element's alignment*. Put another way, `sizeof(a)` can be calculated as `sizeof(int[2])/2*i`, as the compile-time calculation of the size of `int[2]` must already factor in padding. In your case `int` on the huge majority of systems has size 4, needs alignment of 4, so it's just `i * 4`. Note it's runtime evaluated based on `i`, but not particularly hard to do. – Tony Delroy Sep 24 '14 at 02:09