dynamic-arrays

How to get size of dynamic array in C++ [duplicate]

蹲街弑〆低调 提交于 2019-11-26 09:01:32
问题 This question already has an answer here: How to get size c++ dynamic array 3 answers 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 array cin >> n; // Read in the size a = new int[n]; // Allocate n ints and save ptr in a. for (int i=0; i<n; i++) { a[i] = 0; // Initialize all elements to zero. } . . . //

How can I dynamically add items to a Java array?

梦想的初衷 提交于 2019-11-26 08:51:37
问题 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? 回答1: Look at java.util.LinkedList or java.util.ArrayList List<Integer> x = new ArrayList<Integer>(); x.add(1); x.add(2); 回答2: Arrays in Java have a fixed size, so you can't "add something at the end" as you could do in PHP. A bit similar to the PHP behaviour is this: int[] addElement(int[]

Variable Length Array (VLA) in C++ compilers

烂漫一生 提交于 2019-11-26 05:56:49
问题 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 clang++ ) accepts the code as valid syntax, producing just a warning in case -pedantic flag is enable . ISO C++ forbids variable length array ‘vla’ [-Wvla] My questions are: Why does the compiler accept that declaration? The compiler cannot just reject an

Why is it undefined behavior to delete[] an array of derived objects via a base pointer?

一曲冷凌霜 提交于 2019-11-26 05:22:26
问题 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 the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative ( delete array ) if the dynamic type of the object to be deleted differs from its static type, the behavior is

What is the ideal growth rate for a dynamically allocated array?

血红的双手。 提交于 2019-11-26 03:48:25
问题 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. A question central to the performance of such an array is how fast the array grows in size. If you always only grow large enough to fit the current push, you\'ll end up reallocating every time. So it makes sense to double the array size, or

C dynamically growing array

给你一囗甜甜゛ 提交于 2019-11-26 01:26:30
问题 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 keeping such indexes... A quick and dirty solution I use so far is to declare, in the main processing function (local focus) the array with a size of the maximum game entities, and another integer to keep track of how many have been added to the

Correctly allocating multi-dimensional arrays

大憨熊 提交于 2019-11-25 22:52:52
问题 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 programmers struggle to get it right. I have been taught from my programming teacher/book/tutorial that the correct way to dynamically allocate a multi-dimensional array is by using pointer-to-pointers. However, several high rep users on SO now tell