Skip to main content
Filter by
Sorted by
Tagged with
184 votes
11 answers
400k views

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 ...
Balkania's user avatar
  • 1,905
155 votes
10 answers
616k views

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 ...
user299648's user avatar
  • 2,819
132 votes
8 answers
613k views

How to create a dynamic array of integers in C++ using the new keyword?
Sudantha's user avatar
  • 16.3k
126 votes
13 answers
113k views

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.?
Llamageddon's user avatar
  • 3,606
119 votes
12 answers
34k views

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 ...
Joseph Garvin's user avatar
109 votes
3 answers
18k views

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 ...
Lundin's user avatar
  • 220k
88 votes
11 answers
376k views

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 ...
kamikaze_pilot's user avatar
65 votes
7 answers
8k views

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 ...
jarauh's user avatar
  • 2,026
45 votes
3 answers
10k views

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, ...
KnowItAllWannabe's user avatar
43 votes
6 answers
8k views

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 ...
Xeo's user avatar
  • 132k
37 votes
2 answers
87k views

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 ...
Arthur's user avatar
  • 3,478
33 votes
2 answers
9k views

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 ...
orlp's user avatar
  • 119k
32 votes
6 answers
32k views

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 : ...
Maurice Rodriguez's user avatar
31 votes
6 answers
202k views

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 ...
user2280041's user avatar
31 votes
5 answers
22k views

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 ...
Casey Patton's user avatar
  • 4,131
31 votes
3 answers
2k views

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 ...
Itachi Uchiwa's user avatar
30 votes
5 answers
253k views

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 ...
jor's user avatar
  • 2,151
23 votes
2 answers
6k views

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) { ...
BiagioF's user avatar
  • 9,795
23 votes
11 answers
157k views

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 +...
Ouerghi Yassine's user avatar
22 votes
5 answers
8k views

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) ...
nee's user avatar
  • 3,435
21 votes
1 answer
12k views

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 ...
Ally Mitchell's user avatar
21 votes
1 answer
104k views

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 ...
evergreen's user avatar
  • 8,255
20 votes
3 answers
20k views

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 ...
or9ob's user avatar
  • 2,392
18 votes
1 answer
4k views

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 ...
Raj Felix's user avatar
  • 709
17 votes
4 answers
2k views

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} ...
Hugh Jones's user avatar
  • 2,764
17 votes
3 answers
18k views

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: ...
EProgrammerNotFound's user avatar
16 votes
7 answers
11k views

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) ...
Belphegor's user avatar
  • 1,863
16 votes
4 answers
34k views

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 ...
AncientSwordRage's user avatar
16 votes
3 answers
4k views

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, ...
David Heffernan's user avatar
16 votes
1 answer
8k views

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 ...
Swiftslide's user avatar
  • 1,357
16 votes
2 answers
13k views

in this example procedure foobar; var tab:array of integer; begin setlength(tab,10); end; is the array destroyed or the memory is leaking?
Azarien's user avatar
  • 201
15 votes
3 answers
13k views

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: ...
Developer's user avatar
  • 8,410
15 votes
1 answer
569 views

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 ...
Oliv's user avatar
  • 18.2k
14 votes
6 answers
26k views

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 //...
alesdario's user avatar
  • 1,904
13 votes
7 answers
26k views

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 ...
Marco Aviles's user avatar
  • 5,626
13 votes
3 answers
7k views

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 ...
haris's user avatar
  • 2,053
13 votes
3 answers
20k views

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> ...
user664303's user avatar
  • 2,074
13 votes
2 answers
2k views

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 ...
Gad D Lord's user avatar
  • 6,802
12 votes
1 answer
4k views

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 ...
user avatar
12 votes
2 answers
8k views

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&...
user383352's user avatar
  • 5,273
12 votes
1 answer
14k views

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&...
droidchef's user avatar
  • 2,317
12 votes
1 answer
7k views

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 ...
Keiji's user avatar
  • 245
12 votes
4 answers
2k views

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, ...
Flyto's user avatar
  • 696
11 votes
5 answers
10k views

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 ...
urdearboy's user avatar
  • 14.6k
11 votes
4 answers
2k views

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 ...
Maulrus's user avatar
  • 1,847
11 votes
2 answers
32k views

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() ) { ++...
user1334858's user avatar
  • 1,955
11 votes
1 answer
720 views

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; ...
Fabrizio's user avatar
  • 8,073
11 votes
4 answers
4k views

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 ...
ryboe's user avatar
  • 2,872
11 votes
3 answers
3k views

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 ...
Kenneth Cochran's user avatar
11 votes
2 answers
366 views

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 << ...
petat_irrumator's user avatar

1
2 3 4 5
38