Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the () at the end of dynamic array allocate mean?

I have see some example like below in a different question which I have not seen before.

new int[m_size]();
               ^^

I have seen and used the version new int[m_size] all the time but not one with the () at the end.

like image 364
dubnde Avatar asked Apr 05 '11 12:04

dubnde


People also ask

What does it mean to dynamically allocate an array?

Dynamically allocated arrays are allocated on the heap at run time. The heap space can be assigned to global or local pointer variables that store the address of the allocated heap space (point to the first bucket).

What does it mean by dynamically allocated?

Dynamic memory allocation is when an executing program requests that the operating system give it a block of main memory. The program then uses this memory for some purpose. Usually the purpose is to add a node to a data structure.

What does malloc () return when dynamically allocating an array?

The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

What does dynamically allocated mean in C?

Dynamic allocation of memory is a very important subject in C. It allows building complex data structures such as linked lists. Allocating memory dynamically helps us to store data without initially knowing the size of the data in the time we wrote the program.


2 Answers

Two words : Value Initialization

new int[m_size](); array elements would be zero-initialized by writing () because () implies value initialization.1 (zero initialization for a primitive type)

1: An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. ( $8.5/7 )

like image 192
Prasoon Saurav Avatar answered Nov 05 '22 04:11

Prasoon Saurav


it means all the elements will be zero initialized,similar to calloc(o,sizeof(int)) where with this calloc ,ur initializing a single integer on heap with 0

like image 24
Vijay Avatar answered Nov 05 '22 03:11

Vijay