Q:
C++ Dynamic Memory Allocation | Find output programs | Set 1
belongs to collection: C++ find output programs
C++ find output programs
- C++ Operators | Find output programs | Set 1
- C++ Operators | Find output programs | Set 2
- C++ const Keyword | Find output programs | Set 1
- C++ const Keyword | Find output programs | Set 2
- C++ Manipulators | Find output programs | Set 1
- C++ Manipulators | Find output programs | Set 2
- C++ Reference Variable | Find output programs | Set 1
- C++ Reference Variable | Find output programs | Set 2
- C++ Conditional Statements | Find output programs | Set 1
- C++ Conditional Statements | Find output programs | Set 2
- C++ Switch Statements | Find output programs | Set 1
- C++ Switch Statements | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 1
- C++ goto Statement | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 2
- C++ Looping | Find output programs | Set 1
- C++ Looping | Find output programs | Set 2
- C++ Looping | Find output programs | Set 3
- C++ Looping | Find output programs | Set 4
- C++ Looping | Find output programs | Set 5
- C++ Default Argument | Find output programs | Set 1
- C++ Default Argument | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 1
- C++ Arrays | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 3
- C++ Arrays | Find output programs | Set 4
- C++ Arrays | Find output programs | Set 5
- C++ Strings | Find output programs | Set 1
- C++ Strings | Find output programs | Set 2
- C++ Strings | Find output programs | Set 3
- C++ Strings | Find output programs | Set 4
- C++ Strings | Find output programs | Set 5
- C++ Class and Objects | Find output programs | Set 1
- C++ Class and Objects | Find output programs | Set 2
- C++ Class and Objects | Find output programs | Set 3
- C++ Class and Objects | Find output programs | Set 4
- C++ Class and Objects | Find output programs | Set 5
- C++ Constructor and Destructor | Find output programs | Set 1
- C++ Constructor and Destructor | Find output programs | Set 2
- C++ Constructor and Destructor | Find output programs | Set 3
- C++ Constructor and Destructor | Find output programs | Set 4
- C++ Constructor and Destructor | Find output programs | Set 5
- C++ this Pointer | Find output programs | Set 1
- C++ this Pointer | Find output programs | Set 2
- C++ this Pointer | Find output programs | Set 3
- C++ Structures | Find output programs | Set 1
- C++ Structures | Find output programs | Set 2
- C++ Structures | Find output programs | Set 3
- C++ Structures | Find output programs | Set 4
- C++ Structures | Find output programs | Set 5
- C++ Friend Function | Find output programs | Set 1
- C++ Friend Function | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 1
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 1
- C++ Static Variables and Functions | Find output programs | Set 2
- C++ Static Variables and Functions | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 4
- C++ Static Variables and Functions | Find output programs | Set 5
- C++ Operator Overloading | Find output programs | Set 1
- C++ Operator Overloading | Find output programs | Set 2
- C++ Operator Overloading | Find output programs | Set 3
- C++ Operator Overloading | Find output programs | Set 4
- C++ Operator Overloading | Find output programs | Set 5
- C++ Enumeration | Find output programs
- C++ Inheritance | Find output programs | Set 1
- C++ Inheritance | Find output programs | Set 2
- C++ Inheritance | Find output programs | Set 3
- C++ Inheritance | Find output programs | Set 4
- C++ Inheritance | Find output programs | Set 5
- C++ Exceptional Handling | Find output programs | Set 1
- C++ Exceptional Handling | Find output programs | Set 2
- C++ Exceptional Handling | Find output programs | Set 3
- C++ Virtual Functions | Find output programs | Set 1
- C++ Virtual Functions | Find output programs | Set 2
- C++ Virtual Functions | Find output programs | Set 3
- C++ Templates | Find output programs | Set 1
- C++ Templates | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 1
- C++ Preprocessor | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 3
- C++ Signal Handling | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 1
- C++ Dynamic Memory Allocation | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 3
Answer Program 1:
Output:
Explanation:
This program will generate runtime error, Let's understand the program.
Here, we declared a double-pointer and allocate memory space using malloc() but malloc() function returns a (void *), and here we are trying to typecast it into the double-pointer, then the above program will generate runtime error segmentation fault.
Answer Program 2:
Output:
Explanation:
It will print "0 1 2 3" on the console screen. Let's understand the program.
In the above program, we declared a pointer PTR, and allocate space for 4 integers dynamically using malloc() function. The prototype of malloc() function is available in stdlib.h.
The malloc() function returns (void *), and then here we typecast it into (int *).
After that, we assign values to allocated memory by accessing using a pointer.
*(ptr+0) *(ptr+1) *(ptr+2) *(ptr+3)
Because memory allocated by malloc() function in a contiguous manner. So finally we assign values and print them using for loop.
Answer Program 3:
Output:
Explanation:
It will print "8 9 10 11" on the console screen. Let's understand the program.
Here, we declared a pointer PTR, and allocate space for 4 integers dynamically using calloc() function. The prototype of calloc() function is available in stdlib.h.
The calloc() function allocate memory space block-wise, and returns (void *), and then here we typecast it into (int *).
After that, we assign values to allocated memory by accessing using a pointer.
*(ptr+0) *(ptr+1) *(ptr+2) *(ptr+3)
Because the calloc() function in allocate blocks in a contiguous manner. Now, look at the expression used to assign values in dynamically allocated space.
Here i=0; PTR[i] = i*sizeof('C')+sizeof(3.14); // sizeof('C') will be 1, and sizeof(3.14) will be 8, // because 3.14 is double instead of float. PTR[0] = 0*1+8; PTR[0] = 8; i=1; PTR[1] = 1*1+8; PTR[1] = 9; i=2; PTR[2] = 2*1+8; PTR[2] = 10; i=3; PTR[3] = 3*1+8; PTR[3] = 11;
Then finally "8 9 10 11" will be printed on the console screen.
Answer Program 4:
Output:
Explanation:
It will print "0 1 2 3 4 5 6 7" on the console screen. Let's understand the program.
Here, we declared a pointer PTR, and allocate space for 4 integers dynamically using calloc() function. The prototype of calloc() function is available in stdlib.h.
The calloc() function allocate memory space block-wise, and returns (void *), and then here we typecast it into (int *).
After that we assign values to allocated memory by accessing using pointer.
*(ptr+0) *(ptr+1) *(ptr+2) *(ptr+3)
Because the calloc() function in allocate blocs in contiguous manner.
Here, we also used realloc() function to reallocate dynamic memory using an existing pointer of allocated memory, here we allocate space for 4 more integers. And then assign values and print them.
Answer Program 5:
Output:
Explanation:
It will generate a syntax error because we used the return type void in the constructor, which not valid because the constructor does not have any return type.
need an explanation for this answer? contact us directly to get an explanation for this answer