1,877 questions
184
votes
11
answers
400k
views
C dynamically growing array
I have a program that reads a "raw" list of in-game entities, and I intend to make an array holding an index number (int) of an indeterminate number of entities, for processing various things. I would ...
155
votes
10
answers
616k
views
How do I get an empty list of any size in Python?
I basically want a Python equivalent of this Array in C:
int a[x];
but in python I declare an array like:
a = []
and the problem is I want to assign random slots with values like:
a[4] = 1
but I ...
132
votes
8
answers
613k
views
How to create a dynamic array of integers in C++?
How to create a dynamic array of integers in C++ using the new keyword?
126
votes
13
answers
113k
views
NumPy style arrays for C++? [closed]
Are there any C++ (or C) libs that have NumPy-like arrays with support for slicing, vectorized operations, adding and subtracting contents element-by-element, etc.?
119
votes
12
answers
34k
views
What is the ideal growth rate for a dynamically allocated array?
C++ has std::vector and Java has ArrayList, and many other languages have their own form of dynamically allocated array. When a dynamic array runs out of space, it gets reallocated into a larger area ...
109
votes
3
answers
18k
views
Correctly allocating multi-dimensional arrays
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 ...
88
votes
11
answers
376k
views
How can I dynamically add items to a Java array?
In PHP, you can dynamically add elements to arrays by the following:
$x = new Array();
$x[] = 1;
$x[] = 2;
After this, $x would be an array like this: {1,2}.
Is there a way to do something similar ...
65
votes
7
answers
8k
views
C++ doesn't tell you the size of a dynamic array. But why?
I know that there is no way in C++ to obtain the size of a dynamically created array, such as:
int* a;
a = new int[n];
What I would like to know is: Why? Did people just forget this in the ...
45
votes
3
answers
10k
views
Why both runtime-sized arrays and std::dynarray in C++14?
Draft C++14 includes both runtime-sized arrays and the std::dynarray container. From what I can tell, the only real difference between the two is that std::dynarray has an STL interface (e.g., begin, ...
43
votes
6
answers
8k
views
Why is it undefined behavior to delete[] an array of derived objects via a base pointer?
I found the following snippet in the C++03 Standard under 5.3.5 [expr.delete] p3:
In the first alternative (delete object), if the static type of the object to be deleted is different from its ...
37
votes
2
answers
87k
views
How do I declare an array when I don't know the length until run time?
I originally had an array[1..1000] that was defined as a global variable.
But now I need that to be n, not 1000 and I don't find out n until later.
I know what n is before I fill the array up but I ...
33
votes
2
answers
9k
views
Why doesn't C++ support dynamic arrays on the stack? [closed]
In C99 this was legal:
void f(size_t sz) {
char arr[sz];
// ...
}
However, this - dynamically sized stack arrays - has been dropped in C++, and not seeing a return in C++11.
AFAIK C++ was ...
32
votes
6
answers
32k
views
Range-based for loop on a dynamic array?
There is a range-based for loop with the syntax:
for(auto& i : array)
It works with constant arrays but not with pointer based dynamic ones, like
int *array = new int[size];
for(auto& i : ...
31
votes
6
answers
202k
views
Pointer-to-pointer dynamic two-dimensional array
First timer on this website, so here goes..
I'm a newbie to C++ and I'm currently working through the book "Data structures using C++ 2nd ed, of D.S. Malik".
In the book Malik offers two ways of ...
31
votes
5
answers
22k
views
Linked list vs. dynamic array for implementing a stack
I've started reviewing data structures and algorithms before my final year of school starts to make sure I'm on top of everything. One review problem said "Implement a stack using a linked list or ...
31
votes
3
answers
2k
views
Is incrementing a pointer to a 0-sized dynamic array undefined?
AFAIK, although we cannot create a 0-sized static-memory array, but we can do it with dynamic ones:
int a[0]{}; // Compile-time error
int* p = new int[0]; // Is well-defined
As I've read, p acts like ...
30
votes
5
answers
253k
views
Fastest way to add an Item to an Array
What is the fastest way to add a new item to an existing array?
Dim arr As Integer() = {1, 2, 3}
Dim newItem As Integer = 4
(I already know that when working with dynamic list of items you should ...
23
votes
2
answers
6k
views
Why does my C++ compiler accept Variable Length Arrays (VLAs) if the feature is not standard?
As we already know, VLA (standardized in C99) are not part of the standard in C++, so the code below is "illegal" in C++:
void foo(int n) {
int vla[n];
for (int i = 0; i < n; ++i) {
...
23
votes
11
answers
157k
views
ReDim Preserve to a multi-dimensional array in VB6
I'm using VB6 and I need to do a ReDim Preserve to a Multi-Dimensional Array:
Dim n, m As Integer
n = 1
m = 0
Dim arrCity() As String
ReDim arrCity(n, m)
n = n + 1
m = m +...
22
votes
5
answers
8k
views
Making a dynamic array that accepts any type in C
I'm trying to find a way to make a struct to hold a dynamic array that can work with any data type (Including user defined data types), so far this is what I came up with.
#define Vector(DATATYPE) ...
21
votes
1
answer
12k
views
How to add dynamic array formula
I am adding a formula to a worksheet via VBA which should be:
=UNIQUE(IF(TableA[ColumnA]=A1,TableA[ColumnB],""))
This utilises the new SPILL feature in Excel to give me a list of column B values ...
21
votes
1
answer
104k
views
How to get size of dynamic array in C++ [duplicate]
Code for dynamic array by entering size and storing it into "n" variable, but I want to get the array length from a template method and not using "n".
int* a = NULL; // Pointer to int, initialize ...
20
votes
3
answers
20k
views
Initialize a 2d dynamic array in Go
I am trying to create a 2d array in Go:
board := make([][]string, m)
for i := range board {
board[i] = make([]string, n)
}
However, given the verbosity of that, I am wondering if there is a ...
18
votes
1
answer
4k
views
How to return a vector of structs from Rust to C#?
How is it possible to write Rust code like the C code below? This is my Rust code so far, without the option to marshal it:
pub struct PackChar {
id: u32,
val_str: String,
}
#[no_mangle]
pub ...
17
votes
4
answers
2k
views
Why can't two TBytes use overlapping data?
Consider the following XE6 code. The intention is that ThingData should be written to the console for both Thing1 & Thing2, but it is not. Why is that?
program BytesFiddle;
{$APPTYPE CONSOLE}
...
17
votes
3
answers
18k
views
Do I need to finalize array of records in Delphi?
In my application I have the following record:
TTransaction = record
Alias: string
Description: string
Creation: TDateTime
Count: Integer
end;
and I'm using this record in this array:
...
16
votes
7
answers
11k
views
Why is deletion of an item at end of Dynamic array O(n) time complexity?
I am currently reading my textbook and I am totally confused why a dynamic array would require O(n) time to delete an item at the end. I understand that deleting an item from any other index is O(n) ...
16
votes
4
answers
34k
views
How to increase array size on-the-fly in Fortran?
My program is running though 3D array, labelling 'clusters' that it finds and then doing some checks to see if any neighbouring clusters have a label higher than the current cluster. There's a second ...
16
votes
3
answers
4k
views
Is it safe to type-cast TArray<X> to array of X?
Today I discovered a compiler bug (QC#108577).
The following program fails to compile:
program Project1;
{$APPTYPE CONSOLE}
procedure P(M: TArray<TArray<Integer>>);
begin
SetLength(M, ...
16
votes
1
answer
8k
views
VBA: Iteration speed of variant array vs. typed array vs. non-keyed collection
My project requires a bunch of dynamically-resizable arrays for different objects. An array may hold any number of objects, potentially thousands, of a single class, but not objects of multiple ...
16
votes
2
answers
13k
views
Is a dynamic array automatically deallocated when it goes out of scope?
in this example
procedure foobar;
var tab:array of integer;
begin
setlength(tab,10);
end;
is the array destroyed or the memory is leaking?
15
votes
3
answers
13k
views
How to get priorly-unknown array as the output of a function in Fortran
In Python:
def select(x):
y = []
for e in x:
if e!=0:
y.append(e)
return y
that works as:
x = [1,0,2,0,0,3]
select(x)
[1,2,3]
to be translated into Fortran:
...
15
votes
1
answer
569
views
Is pointer arithmetic on allocated storage allowed since C++20?
In the C++20 standard, it is said that array types are implicit lifetime type.
Does it mean that an array to a non implicit lifetime type can be implicitly created? The implicit creation of such an ...
14
votes
6
answers
26k
views
php - create,dynamically, an array initialized with N null elements
I want to create dynamically an array with N (without knowking N) elements.
Something like a function
public function create_array($num_elements){
.....
}
that return me something like
//...
13
votes
7
answers
26k
views
3D array C++ using int [] operator
I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this
It's supposed to be a 3D dynamic array using pointers.
I started like this, but got stuck ...
13
votes
3
answers
7k
views
No array allocated using new can have an initializer?
In the book I am reading at the moment (C++ Complete Reference from Herbert Schildt), it says that no array allocated using new can have an initializer.
Can't I initialize a dynamically allocated ...
13
votes
3
answers
20k
views
Create a fixed size std::vector and write to the elements
In C++ I wish to allocate a fixed-size (but size determined at runtime) std::vector then write to the elements in this vector. This is the code I am using:
int b = 30;
const std::vector<int> ...
13
votes
2
answers
2k
views
Is it safe to replace array of XXX with TArray<XXX>
I have quite a few variables declared as
var
Something: array of XXX;
begin
SetLength(Something, 10);
try
...
finally
SetLength(Something, 0);
end;
end;
To what extend is safe to ...
12
votes
1
answer
4k
views
What is the status on dynarrays?
gcc 4.9 now has support for n3696 (Runtime-sized arrays with automatic storage duration). n3662 says:
In N3497 Runtime-sized arrays with automatic storage
duration, Jens Maurer proposes arrays ...
12
votes
2
answers
8k
views
Does std::vector use the assignment operator of its value type to push_back elements?
If so, why? Why doesn't it use the copy constructor of the value type?
I get the following error:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio
n `ClassWithoutAss&...
12
votes
1
answer
14k
views
Initialise ArrayList<ArrayList<Int>> with a size in kotlin
I am trying to initialise a list with a size in the constructor. But the size of my list is 0.
val seqList = ArrayList<ArrayList<Int>>(N) // This has the Problem
val queries = ArrayList&...
12
votes
1
answer
7k
views
How to "watch" a C++ dynamic array using gdb?
Consider the following example:
int size = 10, *kk = new int[size];
for (int i = 0; i < size; i++) {
kk[i] = i;
}
delete [] kk;
How can I add a watch for the whole array? I can add a watch ...
12
votes
4
answers
2k
views
When writing a large array directly to disk in MATLAB, is there any need to preallocate?
I need to write an array that is too large to fit into memory to a .mat binary file. This can be accomplished with the matfile function, which allows random access to a .mat file on disk.
Normally, ...
11
votes
5
answers
10k
views
Excel UNIQUE Across Columns
Is it possible for the new function UNIQUE to be used across various columns & have the output spill into a single column?
Desired output is UNIQUE values in one single column based on all of the ...
11
votes
4
answers
2k
views
Why does C++ allow variable length arrays that aren't dynamically allocated? [duplicate]
I'm relatively new to C++, and from the beginning it's been drilled into me that you can't do something like
int x;
cin >> x;
int array[x];
Instead, you must use dynamic memory. However, I ...
11
votes
2
answers
32k
views
How to use std::getline() to read a text file into an array of strings in C++?
I am trying to use std::getline() in my project to read in a text file into an array of strings.
Here is my code:
ifstream ifs ( path );
string * in_file;
int count = 0;
while ( !ifs.eof() )
{
++...
11
votes
1
answer
720
views
Why is dynamic array "constructor" much slower than SetLength and elements initialization?
I was comparing performances between these two ways of initializing a dynamic array:
Arr := TArray<integer>.Create(1, 2, 3, 4, 5);
and
SetLength(Arr, 5);
Arr[0] := 1;
Arr[1] := 2;
Arr[2] := 3;
...
11
votes
4
answers
4k
views
The array is static, but the array size isn't know until runtime. How is this possible?
This has been troubling me for a while. It goes to the heart of my (lack of) understanding of the difference between static and dynamic memory allocation. The following array is an ordinary static ...
11
votes
3
answers
3k
views
Is a dynamic array of Char allowed when the parameter type is open array of Char?
I was looking at Delphi: array of Char and TCharArray "Incompatible Types" and started experimenting. What I discovered is rather interesting.
procedure Clear(AArray: array of Integer);
var
...
11
votes
2
answers
366
views
Issue with aggregate initialization with the new operator
The following two codes behave as expected:
char* test1 = new char[20]{"abc"};
cout << test1;
Outputs: abc
int size = 20;
char* test1 = new char[size]{'a','b','c'};
cout << ...