Allocate memory for an integer by dynamic memory allocation valid ways in CPP

Q.  Which of the following is/are valid ways to allocate memory for an integer by dynamic memory allocation in CPP?
- Published on 17 Jul 15

a. int *p = new int(100);
b. int *p; p = new int; *p = 100;
c. int *p = NULL; p = new int; *p=100;
d. Only 1,2
e. All of these

ANSWER: All of these
 

    Discussion

  • Ramesh   -Posted on 23 Oct 15
    According to given question all are valid statement. It allocate memory at runtime.
    new operator is used to create memory at run time.
    All the give below statements are correct.
    • int *p = new int(100);
    • int *p; p = new int; *p = 100;
    • int *p = NULL; p = new int; *p=100;

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)