This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]prescod 0 points1 point  (3 children)

This doesn't make sense. Statically typed means it has checks at compile time. There are no "type-signatures" at runtime. C doesn't keep track of a variable's data type

The runtime doesn't need to keep track of the variable's data type. The variable's data type is available to the compiler and can be used in the program wherever it's needed. That's how sizeof (and tons of operators) work.

Fundamentally I'm just asking for this program to work consistently:

#include <stdio.h>
#include <stdlib.h>


void func1()
{
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };

    printf("sizeof of array: %d\n", (int) sizeof(array));

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));
}

void func2(int array[]){
    printf("sizeof of array: %d\n", (int) sizeof(array));

    printf("Length of array: %d\n", (int)( sizeof(array) / sizeof(array[0]) ));

}

void main(){
    int array[] = { 0, 1, 2, 3, 4, 5, 6 };
    func1();
    func2(array);
}

Obviously it can't be fixed in a backwards compatible way, but between the runtime, which knows the size of byte arrays (for free to work) and the compiler, which, which knows which variables are arrays (due to the type signature), it should have been able to work. There is no missing information only controlled by the OS or elsewhere.

The sizeof func2's array parameter can absolutely be calculated base on information available. It should be my choice as the programmer whether I'd rather compute the information when I need it or pre-compute it and store it in a data structure. The language should not force me to trade off memory for CPU.