All Exams Test series for 1 year @ ₹349 only
Question

Which operator is used for dynamic memory allocation in C++?

The correct answer is

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.

Was this answer helpful?

Important Questions from OOPs concept

  1. Let A be the base class in C++ and B be the derived class from A with protected inheritance. Which of the following statement is false for class B?

  2. Which of the following statements are true regarding C++?

    (a) Overloading a function gives the capability to an existing operator to operate on other data types.

    (b) Inheritance in object-oriented programming provides support to reusability.

    (c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.

    (d) Overloading is a type of polymorphism.

    Choose the correct option from those given below:
  3. Match List I with List II

    List IList II
    (Programming Paradigm)(Characteristic)
    A.ImperativeI.Declarative, clausal representation, theorem proving
    B.Object‐orientedII.Side‐effect free, declarative, expression evaluation
    C.LogicIII. Imperative, abstract data type
    D.FunctionalIV.Command‐based, procedural

    Choose the correct answer from the options given below :

  4. A member function can always access the data in _______, (in C++).

  5. Which of the following is not correct for virtual function in C++?

Need Expert Advice?
Test Series
RRB JE img
Railways
RRB JE Prev. Yr. Paper (CBT 1 + CBT 2) Test Series
290 Tests 5 Tests Free
4473 Attempts
4.3(88)
English

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App