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.
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).
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.
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.
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.
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 )
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With