variable-length-array

Why is it allowed to declare an automatic array with size depending on user input? [duplicate]

假如想象 提交于 2019-12-08 15:55:21
问题 This question already has answers here : Why aren't variable-length arrays part of the C++ standard? (12 answers) Closed last year . I'm using MinGW to compile for C++11 and I found out that this doesn't throw an error: int S; cin>>S; char array[S]; While this does ("storage size of 'array' isn't known"): char array[]; To me, the size is also unknown in the first case, as it depends on what the user input is. As far as I knew, automatic arrays are allocated at compile time in stack memory. So

Incorrect values when initializing a 2D array to 0 in gcc

元气小坏坏 提交于 2019-12-06 19:00:23
问题 #include <iostream> using namespace std; int main() { int rows = 10; int cols = 9; int opt[rows][cols] = {0}; for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { std::cout << opt[i][j] << " "; } std::cout << "\n"; } return 0; } Output: 0 32767 1887606704 10943 232234400 32767 1874154647 10943 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I'm using gcc 6.3, in

C99 Variable Length Array Max sizes and sizeof Function

自闭症网瘾萝莉.ら 提交于 2019-12-02 08:56:40
问题 I am experimenting with the use of Variable Length Arrays (VLAs) in my C code and trying to iron out my understanding of what they should and shouldn't do. I have the following snippet from my function: void get_pdw_frame_usb(pdws_t *pdw_frame, pdw_io_t *pdw_io) { ... unsigned char buf[pdw_io->packet_size]; unsigned char *pdw_buf; memset(buf, '\0', sizeof(buf)); pdw_io is a data structure containing, amongst other things, packet_size , which is of type size_t the char array buf is to be used

C99 Variable Length Array Max sizes and sizeof Function

浪尽此生 提交于 2019-12-02 04:59:16
I am experimenting with the use of Variable Length Arrays (VLAs) in my C code and trying to iron out my understanding of what they should and shouldn't do. I have the following snippet from my function: void get_pdw_frame_usb(pdws_t *pdw_frame, pdw_io_t *pdw_io) { ... unsigned char buf[pdw_io->packet_size]; unsigned char *pdw_buf; memset(buf, '\0', sizeof(buf)); pdw_io is a data structure containing, amongst other things, packet_size , which is of type size_t the char array buf is to be used to store the contents of a usb bulk transfer packet I'm trying to instantiate it here as an automatic

Can I use a C Variable Length Array in C++03 and C++11?

会有一股神秘感。 提交于 2019-12-01 22:24:45
C has a really cool feature called variable length arrays. Its available in C90 and above, and it allows deferring the size of the array until runtime. See GCC's manual 6.19 Arrays of Variable Length . I'm working in C++. At std=c++11 , I'm catching a compile failure due to the use of alloca under Cygwin. I want to switch to variable length arrays, if possible. I also want to try and avoid std::vector and std::array because I want to stay out of the memory manager. I believe every little bit helps, so I'm happy to take these opportunities (that some folks consider peepholes). Can I use a

Function that creates and returns an array is causing problems

删除回忆录丶 提交于 2019-12-01 10:33:29
I tried to write a function that returns a random array of pixel colors, so when I call randomPalette(i) , the function will create a random array of i colors. Below are my codes. It says error at random[colors] that expression must have constant value. I don't know why. How to fix it? pixel* randomPalette(int colors){ pixel random[colors] ; int i, x; srand(time(NULL)); //generate a random seed for (i = 0; i < colors; i++){ x = rand() % 256; random[i].r = x; random[i].g = x; random[i].b = x; } return random; } In your code, firstly pixel random[colors] ; syntax is called variable length array

C++ what is the difference between static and dynamic allocation of this array? [duplicate]

爷,独闯天下 提交于 2019-12-01 09:19:39
问题 This question already has answers here : Difference between array type and array allocated with malloc (9 answers) Closed 6 years ago . int length = 5; int hi[length]; vs int length = 5; int *hi = new int[length]; I was taught compilers complain in C whenever you try to statically allocate arrays w/ size that's not constant. So if you ever needed array of unknown size, you needed to dynamically allocate memory. However, now that there are compilers that allow the first example, what exactly

Function that creates and returns an array is causing problems

﹥>﹥吖頭↗ 提交于 2019-12-01 08:54:58
问题 I tried to write a function that returns a random array of pixel colors, so when I call randomPalette(i) , the function will create a random array of i colors. Below are my codes. It says error at random[colors] that expression must have constant value. I don't know why. How to fix it? pixel* randomPalette(int colors){ pixel random[colors] ; int i, x; srand(time(NULL)); //generate a random seed for (i = 0; i < colors; i++){ x = rand() % 256; random[i].r = x; random[i].g = x; random[i].b = x;

Why doesn't work Variable length array as a globally? [duplicate]

£可爱£侵袭症+ 提交于 2019-12-01 08:20:54
This question already has an answer here: declaring a variable-length array as a global variable in C 4 answers Cannot compile static array with fixed size [duplicate] 1 answer If I write variable length array as a locally, like this: #include <stdio.h> int main() { int i=1; int a[i]; printf("%zu",sizeof(a)); } It working fine in GCC compiler. But, If I write Variable length array as a globally, like this: #include <stdio.h> int i=1; int a[i]; int main() { printf("%zu",sizeof(a)); } Then, GCC compiler gives following an error: prog.c:4:5: error: variably modified 'a' at file scope int a[i];

C standard regarding sizeof overflowing size_t

感情迁移 提交于 2019-11-30 19:28:47
Is this undefined behavior? The relevant parts of the standard don't say much. size_t n = SIZE_MAX / sizeof(double) + 1; size_t m = sizeof(double[n]); The C standard does not explicitly state that the size_t type is sufficient for working with the sizes of all objects or types, especially for hypothetical types that are not actually instantiated. In C 2018 7.19 2, the standard says that size_t “is the unsigned integer type of the result of the sizeof operator”. That tells us about the type size_t but not about the values that may arise during computation. In 5.2.4, the standard recognizes that