74

How does C find the size of an array at runtime? Where is the information about the array size or bounds stored ?

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Kazoom
  • 5,659
  • 16
  • 56
  • 69

5 Answers5

86

sizeof(array) is implemented entirely by the C compiler. By the time the program gets linked, what looks like a sizeof() call to you has been converted into a constant.

Example: when you compile this C code:

#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
    int a[33];
    printf("%d\n", sizeof(a));
}

you get

    .file   "sz.c"
    .section        .rodata
.LC0:
    .string "%d\n"
    .text
.globl main
    .type   main, @function
main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $164, %esp
    movl    $132, 4(%esp)
    movl    $.LC0, (%esp)
    call    printf
    addl    $164, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.1.2 (Gentoo 4.1.2 p1.1)"
    .section        .note.GNU-stack,"",@progbits

The $132 in the middle is the size of the array, 132 = 4 * 33. Notice that there's no call sizeof instruction - unlike printf, which is a real function.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
David Z
  • 128,184
  • 27
  • 255
  • 279
  • This answer is correct for fixed-size arrays, but is wrong for [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) which exist since C99. – Andreas Wenzel Feb 12 '23 at 10:38
21

sizeof is pure compile time in C++ and C prior to C99. Starting with C99 there are variable length arrays:

// returns n + 3
int f(int n) {
    char v[n + 3];

    // not purely a compile time construct anymore
    return sizeof v;
}

That will evaluate the sizeof operand, because n is not yet known at compile time. That only applies to variable length arrays: Other operands or types still make sizeof compute at compile time. In particular, arrays with dimensions known at compile time are still handled like in C++ and C89. As a consequence, the value returned by sizeof is not a compile time constant (constant expression) anymore. You can't use it where such a value is required - for example when initializing static variables, unless a compiler specific extension allows it (the C Standard allows an implementation to have extensions to what it treats as constant).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 4
    Good that you mention C99 VLAs. However, your answer should emphasize that even in C99, fixed size arrays have their size computed at compile time -- it is only VLAs that have their size computed at runtime. And maybe, as a consequence, you can't always use 'sizeof(array)' as a constant in C99. – Jonathan Leffler Mar 23 '09 at 06:17
  • ok, bed time for me now. later i will wake up and scream about all those typos in my answers :p – Johannes Schaub - litb Mar 23 '09 at 06:55
  • 2
    +1 for distinguishing between compile-time and non-compile-time evaulations of `sizeof` in C99, but how is the length of a variable-length-array actually computed? Is it stored in an array descriptor or something? – Kyle Strand Apr 23 '13 at 01:10
14

sizeof() will only work for a fixed size array (which can be static, stack based or in a struct).

If you apply it to an array created with malloc (or new in C++) you will always get the size of a pointer.

And yes, this is based on compile time information.

Delimitry
  • 2,987
  • 4
  • 30
  • 39
H H
  • 263,252
  • 30
  • 330
  • 514
  • fixed size arrays are not necessarily stack based. C has no new operator. – ysth Mar 22 '09 at 23:26
  • 5
    You've forgotten about C99 VLA - variable-length arrays. – Jonathan Leffler Mar 23 '09 at 06:15
  • "Array" has a quite specific meaning in C type system. If you have an _array_ (not a pointer), `sizeof` should work, even if the array is in memory allocated with `malloc` (possible via using pointer to array instead of pointer to element). – hyde Feb 01 '22 at 10:02
10

sizeof gives the size of the variable, not the size of the object that you're pointing to (if there is one.) sizeof(arrayVar) will return the array size in bytes if and only if arrayVar is declared in scope as an array and not a pointer.

For example:

char myArray[10];
char* myPtr = myArray;

printf("%d\n", sizeof(myArray)) // prints 10 
printf("%d\n", sizeof(myPtr)); // prints 4 (on a 32-bit machine)
Dan Breslau
  • 11,472
  • 2
  • 35
  • 44
  • can you elaborate why the top one gives 10? is that in the stack or heap? – Coder Jan 28 '21 at 04:08
  • 1
    It doesn't matter if it's in the stack or on the heap (although, to be on the heap, it would have to be embedded in a `struct` definition.) The key is that the variable `myArray` is declared as an *array*, not as a *pointer*. So, the compiler sees that an array of 10 characters are declared, and allocates 10 bytes of storage to hold those 10 characters. – Dan Breslau Jan 29 '21 at 05:31
  • In the case of `myPtr`, all the compiler sees is that a pointer is being declared; it allocates 4 bytes to hold `myPtr` (on a 32-bit machine), but does not allocate any storage for it to point to. Instead of having its own unique memory for storing data, `myPtr` starts out by pointing to the memory allocated for `myArray`. Does that help? – Dan Breslau Jan 29 '21 at 05:32
4

sizeof(Array) is looked up at compile time, not at run time. The information is not stored.

Are you perhaps interested in implementing bounds checking? If so, there are a number of different ways to go about that.

ysth
  • 96,171
  • 6
  • 121
  • 214