tags:

views:

913

answers:

5

How does c find at run time the size of array? where is the information about array size or bounds of array stored ?

+4  A: 

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.

Henk Holterman
fixed size arrays are not necessarily stack based. C has no new operator.
ysth
You're right, I'll edit a little.
Henk Holterman
You've forgotten about C99 VLA - variable-length arrays.
Jonathan Leffler
+2  A: 

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
+3  A: 

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
+13  A: 

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.

David Zaslavsky
lol! That'll teach me to not check that I put my hands in the right place... thanks for the fix Rob
David Zaslavsky
Ah, I see now, David must be a blind touch typist with a tactile dysfunction in the ends of his fingers :-) Funny how the c and e were okay - I had to check that every other letter was one to the left of the correct position.
paxdiablo
I guess my left hand was in the right place but my right hand was displaced one key to the left... seems like an easy mistake to make - if you're typing up an answer while watching TV ;-)
David Zaslavsky
+5  A: 

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 at runtime, because n is not yet known at compile time. That only applies to variable length arrays: Other operands or types still make sizeof evaluate at compile time. In particular, arrays with dimensions known at compile time are still handled like in C++ and C89. As a consequence of the runtime evaluation of sizeof for VLAs, the value returned by it is not a compile time constant (constant expression) anymore (again only applies when sizeof is used with VLAs). You can't use it where such a value is required - for example when initializing static variables.

Johannes Schaub - litb
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
oh right. didn't see that ambiguity in my answer.
Johannes Schaub - litb
ok, bed time for me now. later i will wake up and scream about all those typos in my answers :p
Johannes Schaub - litb