variable-length-array

Array of variable length in struct

泄露秘密 提交于 2019-11-27 03:06:19
问题 I've created 2 structures to represent images (a pixel and an image one) in C. typedef struct pixel { unsigned char red; unsigned char green; unsigned char blue; }; typedef struct image { int width; int heigth; struct pixel pixels[width][heigth]; }; I am getting an error saying that width and heigth are not defined in the definition of image. I don't get why I'm getting that error and how I can solve it 回答1: i would suggest an alternative like this: typedef struct { int width; int heigth;

ISO C90 forbids variable length array

浪子不回头ぞ 提交于 2019-11-27 02:59:25
问题 I'm dynamically calculating the size of an array. Something like: void foo(size_t limit) { char buffer[limit * 14 + 1]; } But just GCC compiler says: error: ISO C90 forbids variable length array ‘buffer’ searching on SO I found this answer: C99 §6.7.5.2: If the size is an expression that is not an integer constant expression... ...each time it is evaluated it shall have a value greater than zero. So, I did the re-declaration of size limit type variable to: void foo(const size_t limit) But it

Variable length array in the middle of struct - why this C code is valid for gcc

穿精又带淫゛_ 提交于 2019-11-27 02:34:27
问题 There is some strange code using VLA (Variable Length Arrays) which is treated as Valid C (C99, C11) by gcc 4.6: $ cat a.c int main(int argc,char**argv) { struct args_t{ int a; int params[argc]; // << Wat? // VLA in the middle of some struct, between other fields int b; } args; args.b=0; for(args.a=0;args.a<argc;args.a++) { args.params[args.a]=argv[0][0]; args.b++; } return args.b; } This code compiled without warnings: $ gcc-4.6 -Wall -std=c99 a.c && echo $? 0 $ ./a.out ; echo $? 1 $ ./a.out

Dynamic array allocation on stack in C

房东的猫 提交于 2019-11-26 21:39:48
问题 I just did a experiment yesterday, and find something confusing: #include <stdio.h> int main() { int j; scanf("%d",&j); const int i = j; int arr[i]; return 0; } The number j is read from keyboard and it’s used to allocate the array arr on the stack. The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible? 回答1: Variable length arrays were added to C99. It's described in the C99 rationale: 6.7.5.2 Array

Which compiler should I trust?

萝らか妹 提交于 2019-11-26 21:04:08
This is going to be some what of a newbie question but I was trying to work on a small exercise in the C Language (not C++ ) and I was running into some issues. Say I wanted to use an array within a method whose size depended on one of the arguments: void someFunc(int arSize) { char charArray[arSize]; // DO STUFF ... } When I try to compile this as a .c file within Visual Studio 2013 I get an error saying that a non-constant array size is not allowed. However the same code works within CodeBlocks under a GNU Compiler. Which should I trust? Is this normal for compilers to behave so differently?

Is it safe to use variable-length arrays?

岁酱吖の 提交于 2019-11-26 20:56:58
I have a concern about variable-length arrays. When I want to allocate an array dynamically, I'll get null, if it is not possible to allocate enough memory and I can respond to this properly in my program. With a variable length array I don't get this information. What should I do with this? You are right that VLA's are basically always unsafe. The only exception is if you ensure that you never make them larger than a size you would feel safe making a fixed-size array, and in that case you might as well just use a fixed-size array. There is one obscure class of recursive algorithms where VLA's

Is the operand of `sizeof` evaluated with a VLA?

两盒软妹~` 提交于 2019-11-26 20:37:13
问题 An argument in the comments section of this answer prompted me to ask this question. In the following code, bar points to a variable length array, so the sizeof is determined at runtime instead of compile time. int foo = 100; double (*bar)[foo]; The argument was about whether or not using sizeof evaluates its operand when the operand is a variable length array, making sizeof(*bar) undefined behavior when bar is not initialized. Is it undefined behavior to use sizeof(*bar) because I'm

Are variable length arrays there in c++?

和自甴很熟 提交于 2019-11-26 18:00:47
I had always thought that variable length arrays were not allowed in c++(Refer : Why aren't variable-length arrays part of the C++ standard? ) .But than why does this code compile and work? #include <iostream> using namespace std; int main () { int n; cin >> n; int a[n]; for (int i=0; i<n; i++) { a[i] = i; } for (int i=0; i<n; i++) { cout << a[i] << endl; } } David Heffernan The current C++ standard does not require that compilers VLAs. However, compiler vendors are permitted to support VLAs as an extension. It was originally proposed that VLAs would appear in C++14, however the proposal did

Initializing variable length array [duplicate]

只愿长相守 提交于 2019-11-26 15:28:57
This question already has an answer here: C compile error: “Variable-sized object may not be initialized” 7 answers C error “variable-sized object may not be initialized” [duplicate] 3 answers gcc complains: variable-sized object may not be initialized 3 answers On initializing a Variable length array compiler gives an error message: [Error] variable-sized object may not be initialized Code snippet: int n; printf("Enter size of magic square: "); scanf("%d",&n); int board[n][n] = {0}; How should Variable Length arrays be initialized? And why it's all elements are not initialized to 0 in the way

malloced array VS. variable-length-array [duplicate]

懵懂的女人 提交于 2019-11-26 15:21:17
问题 This question already has an answer here: What's the difference between a VLA and dynamic memory allocation via malloc? 4 answers There are two ways to allocate memory to an array, of which the size is unknown at the beginning. The most common way is using malloc like this int * array; ... // when we know the size array = malloc(size*sizeof(int)); But it's valid too in C99 to define the array after we know the size. ... // when we know the size int array[size]; Are they absolutely the same?