variable-length-array

Correctly allocating multi-dimensional arrays

为君一笑 提交于 2019-12-11 08:06:49
问题 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

Declared vs non-declared array in C

大兔子大兔子 提交于 2019-12-11 07:11:39
问题 I came across these two blocks of code: #include <stdio.h> int main() { int a[10], i; for (i = 1; i <= 10; i++) { scanf("%d", &a[i]); printf("%d\n", a[i]); } return 0 } When I run the first piece of code, the code runs well but it gets overridden at some point and I do not get the expected result. However, when I run the second piece of code the program runs perfectly fine with no error. #include <stdio.h> int main() { int size; scanf("%d", &size); int a[size], i; for (i = 1; i <= size; i++)

Multiple constructors with variable argument parameters

霸气de小男生 提交于 2019-12-11 06:08:30
问题 Given these two constructors: SomeClass(int... params) { // Do things } SomeClass(long... otherParams) { // Do other things } What happens when an object foo is instantiated? SomeClass foo = new SomeClass(); Is the undefined default constructor somehow called? Or is one of those constructors with an empty array called? If so, what’s the precedent? I’ve done some basic testing and found that if a constructor without parameters is defined then that will be called. Otherwise, it appears that an

Show a link based on a condition in angularJS

你。 提交于 2019-12-11 05:26:51
问题 I'm creating a navigation bar in AngularJS. I am showing and hiding the submenu 'div.farm-links' on mouseover and mouseleave respectively on 'div.menu-links'. Now, in my submenu whenever 'child.thirdLevelChildList.length' is more than 5, it should show 5th link as 'View all' and hide all the remaining links. Else, it should show all the links. For e.g., on mouse hovering 'about abc', under 'Company', I should see only : 'Strategy', 'Mission, Vision, Values', 'Leadership', 'Org chart' & the

How does the compiler resolve the address of variable declared after a variable-length array?

核能气质少年 提交于 2019-12-10 21:12:06
问题 Suppose I have the following function, which makes use of a variable-length array: void func(int size) { int var1; int arr[size]; int var2; ... } How does the compiler determine the address of var2 ? The only way that I can think of is by placing arr after var1 and var2 . But in that case, what if there were several variable-length arrays? Placing all of them after the "normal" variables would only help resolving the address of the first one. My implicit assumption here is that all the local

Assert the allocation of a variable-length array

六眼飞鱼酱① 提交于 2019-12-10 21:05:47
问题 I apologize for the possible duplicate (have not been able to find an answer to that): Do we need to ensure that the allocation of a variable-length array has completed successfully? For example: void func(int size) { int arr[size]; if (arr == NULL) { // Exit with a failure } else { // Continue as planned } } It seems obvious that the answer is yes , but the syntax arr == NULL feels a bit unusual. Thanks UPDATE: I admit to the fact that I haven't made sure that the code above even compiles

using restrict qualifier with C99 variable length arrays (VLAs)

混江龙づ霸主 提交于 2019-12-10 15:15:08
问题 I am exploring how different implementations of simple loops in C99 auto-vectorize based upon the function signature. Here is my code: /* #define PRAGMA_SIMD _Pragma("simd") */ #define PRAGMA_SIMD #ifdef __INTEL_COMPILER #define ASSUME_ALIGNED(a) __assume_aligned(a,64) #else #define ASSUME_ALIGNED(a) #endif #ifndef ARRAY_RESTRICT #define ARRAY_RESTRICT #endif void foo1(double * restrict a, const double * restrict b, const double * restrict c) { ASSUME_ALIGNED(a); ASSUME_ALIGNED(b); ASSUME

Is there a good reason why VLA are not permitted in pointers in structs?

有些话、适合烂在心里 提交于 2019-12-10 12:02:23
问题 Here is a way to define a Matrix type typedef struct { int nr, nc; double *elem; } Matrix; I would like to define this typedef struct { int nr, nc; double elem[nr][nc]; } Matrix; It would be nice, because I would not have to worry about indexes. That's the reason VLA are useful in the first place, since they only do transparently what would be easy with index arithmetic. Of course, the above is not possible, if only because the size of the struct would not be well defined. Then, I would still

Sizeof operator with variable-length array type

时光怂恿深爱的人放手 提交于 2019-12-09 08:33:25
问题 According to cppreference: If the type of expression is a variable-length array type, expression is evaluated and the size of the array it evaluates to is calculated at run time. It means: if the type of expression is a VLA type, then expression is evaluated. For example: #include <stdio.h> int main() { int i = 0; int a[i]; printf("%zu\n",sizeof(a[i++])); printf("%d\n",i); // Here, print 0 instead of 1 return 0; } So, according to the reference, here i becomes 1 . But, with my GCC compiler, i

Why Are Zero Length VLAs UB?

∥☆過路亽.° 提交于 2019-12-08 15:56:12
问题 The 2011 standard explicitly states... 6.7.6.2 Array declarators If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by * ; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime. Where a size expression is part of the operand of a sizeof operator and changing