variable-length-array

Difference between array type and array allocated with malloc

妖精的绣舞 提交于 2019-11-26 01:13:57
问题 Today I was helping a friend of mine with some C code, and I\'ve found some strange behavior that I couldn\'t explain him why it was happening. We had TSV file with a list of integers, with an int each line. The first line was the number of lines the list had. We also had a c file with a very simple \"readfile\". The first line was read to n, the number of lines, then there was an initialization of: int list[n] and finally a for loop of n with a fscanf. For small n\'s (till ~100.000),

C compile error: “Variable-sized object may not be initialized”

本秂侑毒 提交于 2019-11-26 00:28:39
问题 Why do I receive the error \"Variable-sized object may not be initialized\" with the following code? int boardAux[length][length] = {{0}}; 回答1: I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length is not a compile time constant). You must

Does “int size = 10;” yield a constant expression?

白昼怎懂夜的黑 提交于 2019-11-25 23:43:36
问题 The following code compiles under gcc 4.8 and Clang 3.2: int main() { int size = 10; int arr[size]; } 8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a bug in both compilers, or am I missing something? The latest VC++ CTP rejects the code with this interesting message: error C2466: cannot allocate an array of constant size 0 The interesting part is how it seems to think that size is zero. But at least

Correctly allocating multi-dimensional arrays

大憨熊 提交于 2019-11-25 22:52:52
问题 The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C programming books. Therefore even seasoned C programmers struggle to get it right. I have been taught from my programming teacher/book/tutorial that the correct way to dynamically allocate a multi-dimensional array is by using pointer-to-pointers. However, several high rep users on SO now tell

Why aren't variable-length arrays part of the C++ standard?

断了今生、忘了曾经 提交于 2019-11-25 21:34:02
问题 I haven\'t used C very much in the last few years. When I read this question today I came across some C syntax which I wasn\'t familiar with. Apparently in C99 the following syntax is valid: void foo(int n) { int values[n]; //Declare a variable length array } This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted? Some potential reasons: Hairy for compiler vendors to implement Incompatible with some other part of