438 questions
7
votes
3
answers
569
views
Variable length array parameter size expression with side effects
This question arose from a remark Eric Postpischil made in another thread.
I have a hard time understanding the use of variable length arrays (VLAs) as function parameters:
The array size is not ...
1
vote
2
answers
1k
views
Multiple constructors with variable argument parameters
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 = ...
1
vote
2
answers
407
views
Is there "implicit" allocation of arrays in c++ by declaration?
I would like to allocate memory for arrays, when their sizes are not known until run time, in C++.
I often use constant (compile-time) allocation; but would like to make progress towards "always" ...
0
votes
1
answer
161
views
variable-length array in struct with TI compiler in C (socket programming)
I'm writing code that deals with data received from the UDP protocol in the application layer.
This is the function prototype used to receive data from the UDP protocol:
int my_recv_UDP(int s,
...
2
votes
2
answers
407
views
VLA prototype and multidimensional array argument
I created a C99 VLA function as such :
void create_polygon(int n, int faces[][n]);
I want to call this function in another function where I would allocate my two-dimensional array :
void parse_faces(...
6
votes
2
answers
479
views
Why do I need dynamic memory allocation if I can just create an array? [duplicate]
I was reading about dynamic memory allocation and static memory allocation and found the following about dynamic memory allocation:
In the programs seen in previous chapters, all memory needs were ...
0
votes
3
answers
1k
views
Number of Variables in an array
I have a code;
x = np.linspace(0, 12, 200, False, True, None)
when I print x, I get;
(array([ 0. , 0.06, 0.12, 0.18, 0.24, 0.3 , 0.36, 0.42, 0.48,
0.54, 0.6 , 0.66, 0.72, 0.78,...
8
votes
1
answer
428
views
Pointers to VLA's
As you may know, VLA's haves pros and cons and they are optional in C11.
I suppose that the main reason to make VLA's optional is: "the stack can blow up":
int arr[n]; /* where n = 1024 * 1024 * ...
2
votes
5
answers
201
views
myArray[N] where N = 1,000,000 returns an error whereas myArray[1,000,000] doesn't
File extension: .cpp
I have the following code:
int main() {
int N; cin >> N;
int myArray[N];
return 0;
}
I get an error when I'm trying to run that program if I input N as 1,000,...
4
votes
1
answer
3k
views
C++ variable-sized object may not be initialized
I have the following simple source
#include <iostream>
int main() {
int nv;
nv = 3;
int arr[nv] = { 0, 2, 5 };
return 0;
}
When compiling with GCC on system 1 I get
error: ...
13
votes
3
answers
1k
views
Incorrect values when initializing a 2D array to 0 in gcc
#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 = ...
15
votes
3
answers
2k
views
Why is it allowed to declare an automatic array with size depending on user input? [duplicate]
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 ...
-1
votes
1
answer
157
views
Segmentation fault 11: while trying to input B[1] using cin
I'm having a problem in running the following code. It is giving me a segmentation fault as a runtime error.
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "...
1
vote
1
answer
74
views
What is the difference between the parameter c[a][b] and c[][2]
Given the following two declarations, what is the difference?
int foo ( int a, int b, int c[a][b] ) {
int foo ( int a, int b, int c[][2] ) {
I understand the top statement is a "Variable Length Array"...
-4
votes
2
answers
1k
views
Creating array with non constant sizes
I'm currently working on an assignment where I must find a way to output the longest common subsequence of two strings. In all of the other places I have found implementations of this code, they all ...
3
votes
1
answer
395
views
sizeof operator in conjunction with variable-length array as function arguments
According to GNU's documentation on Arrays of Variable Length, one can use the sizeof operator for determining the size of a variable length array that was passed into a function:
You can also use ...
-4
votes
1
answer
72
views
Why variable sized arrays cause Wrong Answer on Codechef?
int n;cin>>n;
int arr[n]{};
I have a small problem ,why is this decleration of array wrong?I have used it on Codechef several times until recently i got a WA!
After this I declared array as,
...
1
vote
2
answers
164
views
Do calling conventions prevent variable size return values?
As noted here, when returning variable-size data from a C function, you either:
Pass a pointer and max length. Return a flag indicating if max is reached.
Return a pointer to dynamically allocated ...
6
votes
2
answers
2k
views
If a compiler defines __STDC_NO_VLA__, does it still have to support flexible array members?
In C99, flexible array members (of a structure) and variable length arrays were mandatory parts of the standard — conforming C99 compilers (implementations) have to support them both.
In C11, an ...
3
votes
2
answers
453
views
Find the length of the array that was passed to you in Q#
I have an operation as follows to which the driver needs to send an array of qubits.
operation myOp(qubits: Qubit[]) : () {
// uses elements from the qubit array
}
How do I find the ...
1
vote
1
answer
783
views
c, runtime buffer size by function argument
I'm writing a function that needs to test some external SPI flash memory.
By accident during development, I used this code
void __TO_FLASH__ slcTestCache(uint8_t len)
{
uint16_t wcrc = 0xFFFF, ...
0
votes
4
answers
310
views
Define array globally with variable parameter in C
Here's the code:
int EdgeCount = 0;
int numOfEdges = 0;
void addEdge() {
// some code
numOfEdges++;
}
int EdgeWeightArray[numOfEdges]; // error
I want that global array with variable ...
1
vote
2
answers
104
views
accessing variable length array after its memory should have been deallocated
I'm currently studying variable length array and automatic storage.
I have the following code that allocate memory for an variable length array myArray inside function vla, and return a pointer to ...
0
votes
5
answers
1k
views
Arrays created in functions,compile time or run time?
When we create an array on the stack most compilers will want to know the size of the array which will be determined at compile time so generally we can't get a user to enter the size of the array ...
3
votes
2
answers
496
views
Are conformant array parameters VLAs?
CERT's Secure Coding Standard includes an item (API05-C) which encourages the use of conformant array parameters, which is a recommendation I've implemented in a lot of my code (hidden behind a macro ...
3
votes
1
answer
353
views
Return a malloc’ed matrix while being able to use subscript notation
I have an exercise where I am supposed to use fixed-size arrays and in/out parameters to do stuff on matrices (add, scanf, print, etc.), but I’d like to do it on arbitrary-length matrices and return ...
0
votes
1
answer
67
views
Just for kicks: Creating/deep-copying multidimensional arrays in JavaScript and accounting for variable length?
I volunteer teaching coding to young girls to code and one of the more advanced ones was trying to use a 2D array for her JavaScript project, but was struggling with the concept of multidimensional ...
0
votes
0
answers
37
views
Nested for loop saving last input not whole input
I'm back again with that C question heh. So I'm trying to get the user to input a whole 2d array (size, values, everything), with VLA arrays (im using the latest compiler). Everything is fine up until ...
0
votes
1
answer
311
views
What is an expression of VLA type?
I was trying to understand the working of sizeof operator and I came across this
question. Following is the code from that question
#include <stdio.h>
int main() {
int i = 0;
int a[i];...
13
votes
4
answers
2k
views
Sizeof operator with variable-length array type
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 ...
0
votes
3
answers
805
views
Can't get length of array/object in an Object of Javascript
box = {
curBox: 0,
boxes: document.getElementsByClassName('box'),
size: this.boxes.length, //this one won't work
orSize: Object.keys(this.boxes).length, //as well as this one
preBox: ...
0
votes
4
answers
974
views
Is there a VLAs (variable length arrays) support workaround for VS2017?
So simple code like:
int n;
cin >> n;
int s[n], p[2*(n-1)][3];
I have to translate to:
int n;
cin >> n;
vector<int> s(n, 0);
vector<vector<int>> p(2 * (n - 1), vector&...
3
votes
3
answers
485
views
Initialize array size in C at File Scope
I'd like to initialize an array based on a calculation, but the compiler gives me an error when I try this (I'm using GCC version 6.3.0):
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
...
3
votes
3
answers
1k
views
How to pass a two-dimensional array to a function in C?
My function prototype is
int** rotate(int **arr, int row, int col, int fl);
where arr is the two dimensional array, row and col is the number of row and columns of the 2D array respectively, fl is a ...
4
votes
4
answers
3k
views
Why doesn't work Variable length array as a globally? [duplicate]
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 ...
1
vote
0
answers
868
views
Keras variable length input for regression
Everyone!
I am trying to develop a neural network using Keras and TensorFlow, which should be able to take variable length arrays as input and give either some single value (see the toy example below)...
1
vote
3
answers
933
views
C struct definition containing 2-dimensional array type
I want to include a variable length two-dimensional array pointer as part of a struct, but the c99 compiler gives me the following error on the Array anArray line: "flexible array member in otherwise ...
0
votes
1
answer
306
views
Posting data from variable length 2 dimensional array
I am having some issues with posting data from a 2 dimensional array and echoing them.
I have read several topics and tutorials on multidimensional array, but most of them used fixed length of ...
0
votes
3
answers
98
views
How to initialize multidimentional array in C Programming
I am getting error when i am running this code
int row1=2,col1=2;
int mat1[row1][col1]=
{
{1,5},
{4,6}
};
What is wrong with this code??
IDE: CodeBlocks
error: variable-sized object may ...
0
votes
2
answers
2k
views
Dynamic Arrays and Static Arrays Compile Time or Runtime? [duplicate]
I understand the basic concept of Static Arrays and dynamic arrays. The main difference between these two are one(Static Array) allocates memory at compile time, and the other at runtime. However, we ...
0
votes
1
answer
54
views
Bug in jQuery array (i), when adding a +1 to a negative value
I guess I'm fighting a bug. I build a small carousel in jquery. It's more some boxs stacked on each other, that will change their z-index (jquery.css) when my user clicks the next and prev arrows. So ...
19
votes
1
answer
3k
views
What is the type of a pointer to a variable-length array in C?
Here's a short C program that prompts the user for a number, creates a variable-length array of ints of that size, and then uses pointer arithmetic to step over the elements that were allocated:
#...
2
votes
1
answer
445
views
C99 How to cast simple pointer to a multidimensional-array of variable length?
In my function bodies reduce() and initialize(...) I want to work with the array globalArray as a three-dimensional one, where the dimensions are not known until runtime.
How do I cast the ...
68
votes
5
answers
7k
views
How does the compiler allocate memory without knowing the size at compile time?
I wrote a C program that accepts integer input from the user, that is used as the size of an integer array, and using that value it declares an array of given size, and I am confirming it by checking ...
0
votes
2
answers
53
views
Variable length string duplication in Python
The following short python script:
var1 = '7f0000000000000000000000000000000000000000000000000000000000000002'
var2 = '01'
output = 'evm --code ' + var1 + var1 + var2 + ' run'
print(output)
Is ...
1
vote
1
answer
74
views
Using variable to initialize an array in C++ [duplicate]
As C++ primer said, we can't use variable as the dimension of builtin array, so the following code did not work
int length = 3;
int array[length] = {0, 1, 2};
the error is
error: variable-sized ...
-2
votes
2
answers
80
views
array size initialized dynamically example
I was having trouble understanding why
int n;
cin>>n;
int arr[n];
works. I was told that this code should not run because the value of 'n' could only be declared during runtime and therefore ...
1
vote
3
answers
137
views
Should I declare an array after I read the size of it?
I recently discovered that this kind of declaration works with my C++ compiler
int h, w;
cin>>h>>w;
int a[h + 1][w + 1], f[h + 1][w + 1];
Should I use this type of declaration to ...
4
votes
2
answers
252
views
Dynamic memory allocation in C++?
Why does this work?
#include <iostream>
int main()
{
std::cout << "Enter a number: ";
int arraySize;
std::cin >> arraySize;
int ...
1
vote
2
answers
2k
views
Show a link based on a condition in angularJS
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....