How do you initialise a dynamic array in C++?
char* c = new char[length]();
Initialize values of dynamic array in a function (C language)
You have the address of a pointer passed to the function:
array
You want to dereference that to get the pointer:
*array
Then apply the array subscript operator to the result:
(*array)[2]
Or equivalently:
*((*array) + 2)
The parenthesis are required because the array subscript operator []
has higher precedence than the dereference operator *
.
Generally speaking, you should use the array subscript operator whenever possible, as it tends to be easier to read.
Dynamic array initialization in c
can we do this?
No. But you can do this:
int arr[n];
memset(arr, 0, sizeof(arr));
You lose the syntactic sugar for initialization, but you get the functionality.
Initialize a dynamic array in O(1) time in C/C++
No, there is not, not in c++, nor in c, not in any language or CPU I know of. But it can be done with 1 line of code.
in C: for a array of char:
char a[N];
memset(a, '1', N); // only works for data 1 byte long
in C++
std::vector<T> v;
std::fill(v.begin(), v.end(), value);
How to initialize a dynamic int array elements to 0 in C
In this case you would use calloc()
:
array = (int*) calloc(n, sizeof(int));
It's safe to assume that all systems now have all zero bits as the representation for zero.
§6.2.6.2 guarantees this to work:
For any integer type, the object representation where all the bits are
zero shall be a representation of the value zero in that type.
It's also possible to do a combination of malloc()
+ memset()
, but for reasons discussed in the comments of this answer, it is likely to be more efficient to use calloc()
.
Initialize a dynamic C array within a C++ initializer list
I don't understand how the C array is being initialized within the initializer list.
here:
myArray(int size) : size(size), elements(new int[size])
elements
are basicly assigned value returned by new int[size]
You could rewrite it as :
myArray(int size)
{
this->size = size;
elements = new int[size];
std::cout << "Constructed array of size " << size << std::endl;
}
[edit]
This:
// elements(new int[size]); // why will line only work in initializer list?
will not work in the function body, because in this place compiler thinks that you want to call a function named elements
, but there is no such function.
The fact that it works in the initializer list is because that is how the language syntax was made.
Seg faulting with 4D arrays & initializing dynamic arrays
Even though I have not understood why do you use 4D arrays to store shapes for a tetris game, and I agree with bolov's comment that such an array should not overflow the stack (7*4*4*4*1 = 448 bytes), so you should probably check other code you wrote.
Now, to your question on how to manage 4D (N-Dimensional)dynamically sized arrays. You can do this in two ways:
The first way consists in creating an array of (N-1)-Dimensional arrays. If N = 2 (a table) you end up with a "linearized" version of the table (a normal array) which dimension is equal to R * C where R is the number of rows and C the number of columns. Inductively speaking, you can do the very same thing for N-Dimensional arrays without too much effort. This method has some drawbacks though:
- You need to know beforehand all the dimensions except one (the "latest") and all the dimensions are fixed. Back to the N = 2 example: if you use this method on a table of C columns and R rows, you can change the number of rows by allocating C * sizeof(<your_array_type>) more bytes at the end of the preallocated space, but not the number of columns (not without rebuilding the entire linearized array). Moreover, different rows must have the same number of columns C (you cannot have a 2D array that looks like a triangle when drawn on paper, just to get things clear).
- You need to carefully manage the indicies: you cannot simply write
my_array[row][column]
, instead you must access that array withmy_array[row*C + column]
. If N is not 2, then this formula gets... interesting
You can use N-1 arrays of pointers. That's my favourite solution because it does not have any of the drawbacks from the previous solution, although you need to manage pointers to pointers to pointers to .... to pointers to a type (but that's what you do when you access
my_array[7][4][4][4]
.
Solution 1
Let's say you want to build an N-Dimensional array in C using the first solution.
You know the length of each dimension of the array up to the (N-1)-th (let's call them d_1, d_2, ..., d_(N-1)). We can build this inductively:
- We know how to build a dynamic 1-dimensional array
- Supposing we know how to build a (N-1)-dimensional array, we show that we can build a N-Dimensional array by putting each (N-1)-dimensional array we have available in a 1-Dimensional array, thus increasing the available dimensions by 1.
Let's also assume that the data type that the arrays must hold is called T
.
Let's suppose we want to create an array with R (N-1)-dimensional arrays inside it. For that we need to know the size of each (N-1)-dimensional array, so we need to calculate it.
- For N = 1 the size is just
sizeof(T)
- For N = 2 the size is
d_1 * sizeof(T)
- For N = 3 the size is
d_2 * d_1 * sizeof(T)
You can easily inductively prove that the number of bytes required to store R (N-1)-dimensional arrays is R*(d_1 * d_2 * ... * d_(n-1) * sizeof(T))
. And that's done.
Now, we need to access a random element inside this massive N-dimensional array. Let's say we want to access the item with indicies (i_1, i_2, ..., i_N). For this we are going to repeat the inductive reasoning:
- For N = 1, the index of the i_1 element is just
my_array[i_1]
- For N = 2, the index of the (i_1, i_2) element can be calculated by thinking that each d_1 elements, a new array begins, so the element is
my_array[i_1 * d_1 + i_2]
. - For N = 3, we can repeat the same process and end up having the element
my_array[d_2 * ((i_1 * d_1) + i_2) + i_3]
And so on.
Solution 2
The second solution wastes a bit more memory, but it's more straightforward, both to understand and to implement.
Let's just stick to the N = 2 case so that we can think better. Imagine to have a table and to split it row by row and to place each row in its own memory slot. Now, a row is a 1-dimensional array, and to make a 2-dimensional array we only need to be able to have an ordered array with references to each row. Something like the following drawing shows (the last row is the R-th row):
+------+
| R1 -------> [1,2,3,4]
|------|
| R2 -------> [2,4,6,8]
|------|
| R3 -------> [3,6,9,12]
|------|
| .... |
|------|
| RR -------> [R, 2*R, 3*R, 4*R]
+------+
In order to do that, you need to first allocate the references array (R elements long) and then, iterate through this array and assign to each entry the pointer to a newly allocated memory area of size d_1.
We can easily extend this for N dimensions. Simply build a R dimensional array and, for each entry in this array, allocate a new 1-Dimensional array of size d_(N-1) and do the same for the newly created array until you get to the array with size d_1.
Notice how you can easily access each element by simply using the expression my_array[i_1][i_2][i_3]...[i_N]
.
For example, let's suppose N = 3 and T
is uint8_t
and that d_1
, d_2
and d_3
are known (and not uninitialized) in the following code:
size_t d1 = 5, d2 = 7, d3 = 3;
int ***my_array;
my_array = malloc(d1 * sizeof(int**));
for(size_t x = 0; x<d1; x++){
my_array[x] = malloc(d2 * sizeof(int*));
for (size_t y = 0; y < d2; y++){
my_array[x][y] = malloc(d3 * sizeof(int));
}
}
//Accessing a random element
size_t x1 = 2, y1 = 6, z1 = 1;
my_array[x1][y1][z1] = 32;
I hope this helps. Please feel free to comment if you have questions.
Initializing dynamic array of unions in C
To define a union type do
union element
{
int digit;
char letter;
float number;
};
To dynamical allocated an array with n
elements of type union element
do:
union element * pu = malloc(n * sizeof (union element));
or even better
union element * pu = malloc(n * sizeof *pu);
The code you show here
union element
{
int digit;
char letter;
float number;
} f;
does two things:
- Define a type
union element
- Define the variable
f
to be of typeunion element
Note
If this
element* arr = malloc(n * ...
compiles, then you are not using a C compiler, but a C++ compiler.
A C compiler required
union element* arr = malloc(n * ...
A final comment on your wording: "Initiallizing ... array ...":
The code dynamical creates n elements of a union. Those element do not get "initialized".
To have them being initialised during allocation of calloc()
instrad of malloc()
;
... = calloc(n, sizeof ...); /* Note the different number of parameters. */
C allocating dynamic array of doubles and initializing it with memset
because memset works with bytes
sizeof(double)==8,
so each double in your array is filled with the value 0x0101010101010101
just replace the memset with:
for (int i=0;i< numElements;i++) d[i]=1;
Related Topics
What Is the Simplest Way to Convert Array to Vector
Common Array Length MACro for C
Qgraphicsview Zooming in and Out Under Mouse Position Using Mouse Wheel
How to Get the Class Name from a C++ Object
Error: Use of Deleted Function
Linking Different Libraries for Debug and Release Builds in Cmake on Windows
Problems with Singleton Pattern
Floating Point Comparison Revisited
Pass by Value VS Pass by Rvalue Reference
Does C++ Pass Objects by Value or Reference