简体   繁体   中英

C (Dynamic) Array (Fixed size)

Hello I am wondering why this works since on tutorials and such it always lists that arrays must be of fixed size except when dynamically making one with malloc.

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

int main(int argc, char **argv) {
        if(argc < 2)
        return 0;

        int tmp[ atoi(argv[1])  ];


        printf("sizeof tmp equals to %d\n", sizeof tmp);
        return 0;
}

What happens in the background at ASM-level when doing this? And how does it work? Does it allocate the size given at starting the program on the stack and what's the max for the stack?

Also is this more memory expensive than using malloc?

Thanks in advance.

C99 introduces variable length array whose length is not a constant expression. The declaration

int tmp[ atoi(argv[1]) ];  

declares tmp as VLA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM