Questions tagged [dynamic-arrays]

Anything related to dynamic arrays, i.e. array-like data structures where the length of the array (the number of its elements) can be changed at runtime, thus allowing adding or removing elements without explicit memory allocation and management.

Anything related to dynamic arrays, i.e. array-like data structures where the length of the array (the number of its elements) can be changed at runtime, thus allowing adding or removing elements without explicit memory allocation and management.

1815 questions
31
votes
6 answers

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 creating a dynamic two-dimensional array. In the…
user2280041
  • 317
  • 1
  • 4
  • 7
31
votes
6 answers

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 : array) cout<< i << endl; It gives errors and warnings…
Maurice Rodriguez
  • 663
  • 2
  • 7
  • 17
29
votes
5 answers

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 rather use a List, ArrayList or similar…
jor
  • 2,058
  • 2
  • 26
  • 46
22
votes
5 answers

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) struct { DATATYPE* data; size_t size; size_t used;…
zee
  • 2,933
  • 2
  • 16
  • 28
22
votes
2 answers

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) { vla[i] = i; } } Despite of that the compiler (g++ and…
BiagioF
  • 9,368
  • 2
  • 26
  • 50
22
votes
11 answers

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 + 1 ReDim Preserve arrCity(n, m) Whenever I…
Ouerghi Yassine
  • 1,835
  • 7
  • 43
  • 72
21
votes
1 answer

How to get size of dynamic array in C++

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 to nothing. int n; // Size needed for…
evergreen
  • 7,771
  • 2
  • 17
  • 25
20
votes
1 answer

Excel VBA - 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 where the related value in column A matches what is…
Ally Mitchell
  • 213
  • 3
  • 7
19
votes
3 answers

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 better or more succinct way to handle this problem…
or9ob
  • 2,313
  • 4
  • 25
  • 45
17
votes
4 answers

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} {$R *.res} uses System.SysUtils; type TThing =…
Hugh Jones
  • 2,706
  • 19
  • 30
17
votes
1 answer

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 extern "C" fn get_packs_char(size: u32) ->…
Raj Felix
  • 685
  • 5
  • 16
17
votes
3 answers

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: Transactions = array of TTransaction; I'm keeping the…
EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59
16
votes
7 answers

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) because you have to copy all the data and move…
Belphegor
  • 1,683
  • 4
  • 23
  • 44
16
votes
2 answers

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?
Azarien
  • 201
  • 1
  • 2
  • 4
16
votes
3 answers

Is it safe to type-cast TArray 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>); begin SetLength(M, 1, 2); end; begin end. The compiler gags on the SetLength…
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490