Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof operator changing during runtime? [duplicate]

Tags:

c++

c

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?

like image 439
Dileep Nunna Avatar asked Jan 11 '23 01:01

Dileep Nunna


1 Answers

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.

like image 191
user2357112 supports Monica Avatar answered Jan 18 '23 22:01

user2357112 supports Monica