Which operator is used for dynamic memory allocation in C++?
new
In C++, memory can be allocated in two ways: automatically on the stack (ordinary local variables, freed when they go out of scope) or manually on the heap / free store at run time. Heap allocation, where the amount or lifetime of memory is decided while the program runs, is called dynamic memory allocation, and the dedicated operator for it is new. This is the correct answer.
Examples of its use:
int *p = new int; — allocates storage for a single int on the heap and returns its address.int *arr = new int[10]; — allocates a contiguous block for 10 integers.A key advantage of new over the C library function is that it is type-aware: it returns a pointer of the correct type (no cast needed) and, for class objects, it invokes the constructor so the object is properly initialised.
The delete operator is the counterpart of new — it releases previously allocated memory (using delete for a single object and delete[] for an array), so it is a deallocation operator, not an allocation one. Both allocate and alloc are not C++ operators at all; the closest C-style function is malloc(), but that returns a raw void*, requires an explicit cast, needs sizeof to compute the byte count, and does not call any constructor. Hence among the given choices only new is the C++ dynamic-allocation operator.
Which statements is correct regarding main () function in C++?
I. It is a user defined function.
II. It is a pre-defined function.
Consider the following segment of C++ code.
int j, n;
for(j=1;j<=n; )
j=j*2;
What is the number of comparisons made in the execution of the loop for any n > 0?
Given below are two statements:
Statement (I): The friend function and the member functions of a friend class directly access the private and protected data.
Statement (II): The friend function can access the private data through the member functions of the base class
In the light of the above statements, choose the most appropriate answer from the options given below:
If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the following is correct?
Which of the following statements is TRUE ?