Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ creating a static like array with "new" or another way of creating a dynamic array

I know that the common technique of creating a dynamic array using new in C++ is:

int * arr = new int[5];

A book also says:

short tell[10]; // tell is an array of 20 bytes
cout << tell << endl; // displays &tell[0]
cout << &tell << endl; // displays address of the whole array
short (*p)[10] = &tell; // p points to an array of 20 shorts

Now I wonder if there is a way to allocate memory for an array using new, so it can be then assigned to a pointer to the whole array. It might look like this:

int (*p)[5] = new int[5]; 

The above example doesn't work. The left side looks correct to me. But I don't know what should be on the right.

My intention is to understand if it's possible. And I know that there are std::vector and std::array.

Update:

Here is what I actually wanted to check:

int (*p1)[5] = (int (*)[5]) new int[5];
// size of the whole array
cout << "sizeof(*p1) = " << sizeof(*p1) << endl;

int * p2 = new int[5];
// size of the first element
cout << "sizeof(*p2) = " << sizeof(*p2) << endl;

And here is how to access these arrays:

memset(*p1, 0, sizeof(*p1));
cout << "p1[0] = " << (*p1)[0] << endl;

memset(p2, 0, sizeof(*p2) * 5);
cout << "p2[0] = " << p2[0] << endl;
like image 738
Kostiantyn Ponomarenko Avatar asked Oct 30 '16 14:10

Kostiantyn Ponomarenko


People also ask

What is static array and dynamic array in C?

Static arrays are allocated memory at compile time and the memory is allocated on the stack. Whereas, the dynamic arrays are allocated memory at the runtime and the memory is allocated from heap. This is static integer array i.e. fixed memory assigned before runtime int arr[] = { 1, 3, 4 };

Can you dynamically create an array in C?

We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.

Are arrays in C static or dynamic?

In C, it is possible to allocate memory to arrays at run time. This feature is known as dynamic memory allocation and the arrays created at run time are called dynamic arrays. Dynamic arraysare created using pointer variables and memory management functions malloc, calloc and realloc.

What is the difference between a static array and a dynamic array?

A static array variable holds a value of type, array. A dynamic array variable holds a pointer to an array value. Thanks to automatic pointer dereferencing and automatic index padding, there is very little difference in the code that you write to use either type of array.


2 Answers

know that the common technique of creating a dynamic array

In C++ that was written 20 years ago, maybe.

These days you should use std::vector for dynamic arrays and std::array for fixed size array.

If your framework or platform supplies additional array classes (like QT's QVector), they are fine too, as long as you don't mess with C-pointers directly, and you have RAII-based array class.

and as for concrete answer, new T[size] always returns T* , so you cannot catch a pointer returned by new[] with T(*)[size].

like image 191
David Haim Avatar answered Oct 12 '22 12:10

David Haim


The problem is that left and right sights have different types.

The type of:

new int[5]

is

int*.

The type of:

int (*p)[5]

is

int (*)[5].

And compiler cannot assign one to another.

Generally speaking it is impossible to assign T* to T (*)[N]. That is why you need to use the syntax mentioned in the beginning of your question.

like image 42
Edgar Rokjān Avatar answered Oct 12 '22 14:10

Edgar Rokjān