int *array = new int[n]; what is this function actually doing?
new allocates an amount of memory needed to store the object/array that you request. In this case n numbers of int.
The pointer will then store the address to this block of memory.
But be careful, this allocated block of memory will not be freed until you tell it so by writing
delete [] array;
Why is new int[n] valid when int array[n] is not?
That's because the former is allocated on the stack and the latter on the heap.
When you allocate something on the stack, knowing the size of the object is essential for correctly building it. C99 allows the size to be specified at run time, and this introduces some complications in building and dismantling the aforementioned stack, since you cannot calculate its size at compile time. Machine code must be emitted in order to perform said calculation during the execution of the program. This is probably the main reason why this feature wasn't included in the C++ standard.²
On the contrary, the heap has no fixed structure, as the name implies. Blocks of any size can be allocated with no particular order, as long as they do not overlap and you have enough (virtual) memory¹. In this case, knowing the size at compile time is not that relevant.
Also, remember that the stack has a limited size, mostly to detect infinite recursions before they consume all the available memory. Usually the limit is fixed around 1MB, and you rarely reach that. Unless you allocate large objects, which should be placed in the heap.
As of what you should use, probably a std::vector<int>
. But it really depends on what you are trying to do.
Also note that C++11 has a std::array
class, whose size must be known at compile time. C++14 should have introduced std::dynarray
, but it was postponed because there is still much work to do concerning compile-time unknown size stack allocation.
¹ blocks are usually allocated sequentially for performance reasons, but that's not required.
² as pointed out, knowing the size at compile time is not a hard requirement, but it makes things simpler.
int** a = new int*[n](); What does this function do?
As you (should) know, int *a = new int[n];
allocates an array of int
s with size n
.
So, in general, T *a = new T[n];
allocates an array of T
s with size n
.
Now if you substitute T = int *
, you'll get int **a = new int*[n];
, which allocates an array of int *
s (that is, of pointers to int
).
Adding ()
on the right zeroes every pointer in the array (otherwise they would be uninitialized).
How is memory allocated in this line of code int **v = new int*[n]; ?
This allocates an array of n
pointers to int
. A pointer to the first element in this array of pointers is stored in v
. It is a double pointer, such that accessing an element via v[i]
returns a stored pointer from the array.
std::bad_alloc when declaring new int[n]
You need to initialize numNeg
and numPos
to 0
.
What does new int(100) do?
The first line allocates a single int
and initializes it to 100
. Think of the int(100)
as a constructor call.
Since this is a scalar allocation, trying to access arr[1]
or to free the memory using delete[]
would lead to undefined behaviour.
What is meant by the statement new employee *[num] in C++
This will allocate a memory required to hold num
number of pointers to employee
on the free store.
e.g:
employee** a = new employee* [2]; // 2 pointers on the heap
heap:
address a (e.g. 0x97a0008): pointer1 to employee
address a + 1 ( 0x97a000c): pointer2 to employee
Side note: you use delete[]
on arrays so you can delete above with delete[] a
BUT you will have to loop first through all entries if you have allocated memory for them so it have to be freed before you loose the pointer.
https://stackoverflow.com/a/13477214/1141471
Related Topics
Template Metaprogramming - Difference Between Using Enum Hack and Static Const
Determining the Alignment of C/C++ Structures in Relation to Its Members
Finding Square Root Without Using Sqrt Function
"Field Has Incomplete Type" Error
Namespaces and Operator Resolution
C++ Template Typename Iterator
When Virtual Inheritance Is a Good Design
Is There an Equivalent to Winapi's Max_Path Under Linux/Unix
How to Prevent an Object Being Created on the Heap
What Is the Size of an Enum Type Data in C++
Is There a Standard Way of Moving a Range into a Vector
Linux C++: How to Profile Time Wasted Due to Cache Misses
Sharing Precompiled Headers Between Projects in Visual Studio