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
177
votes
10 answers

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 like to avoid using too much memory or CPU for…
Balkania
  • 1,835
  • 3
  • 13
  • 6
154
votes
10 answers

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 can't do that with Python, since the Python list is…
user299648
  • 2,769
  • 6
  • 34
  • 43
126
votes
19 answers

Java dynamic array sizes?

I have a class - XClass, that I want to load into an array of XClasses: XClass myClass[] = new XClass[10]; myclass[0] = new XClass(); myclass[9] = new XClass(); However, I don't know if I will need 10, 8 or 12 classes or any other number for that…
Paul
  • 1,335
  • 3
  • 10
  • 5
123
votes
8 answers

How to create a dynamic array of integers in C++?

How to create a dynamic array of integers in C++ using the new keyword?
Sudantha
  • 15,684
  • 43
  • 105
  • 161
118
votes
13 answers

NumPy style arrays for C++?

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
  • 3,306
  • 4
  • 25
  • 44
109
votes
12 answers

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 and the old values are copied into the new array.…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
100
votes
2 answers

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 programming books. Therefore even seasoned C…
Lundin
  • 195,001
  • 40
  • 254
  • 396
88
votes
11 answers

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 in Java?
kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171
63
votes
7 answers

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 specification of C++, or is there a technical reason for…
jarauh
  • 1,836
  • 22
  • 30
45
votes
3 answers

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, end, size, etc.), while runtime-sized arrays do…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
40
votes
5 answers

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 dynamic type, the static type shall be a base class of…
Xeo
  • 129,499
  • 52
  • 291
  • 397
36
votes
2 answers

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 need it to be global therefore need a way to define…
Arthur
  • 3,376
  • 11
  • 43
  • 70
33
votes
2 answers

Why doesn't C++ support dynamic arrays on the stack?

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 made with C compatibility in mind, so I wondered…
orlp
  • 112,504
  • 36
  • 218
  • 315
31
votes
5 answers

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 dynamic array and explain why you made the best…
Casey Patton
  • 4,021
  • 9
  • 41
  • 54
31
votes
3 answers

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 one-past-end element. I can print the address…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
1
2 3
99 100