C++ Dynamic Memory Allocation | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Dynamic Memory Allocation (set 1).
Submitted by Nidhi, on October 10, 2020

Program 1:

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	int **PTR;
	
	PTR = (int**)malloc(2*2*sizeof(int));
	
	PTR[0][0]=1;
	PTR[0][1]=2;
	PTR[1][0]=3;
	PTR[1][1]=4;
	
	cout<<PTR[0][0]<<" "<<PTR[0][1]<<endl<<PTR[1][0]<<" "<<PTR[1][1]<<" ";

	return 0;
}

Output:

timeout: the monitored command dumped core
sh: line 1: 20160 Segmentation fault      timeout 10s main

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.

Program 2:

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	int *PTR;
	int i=0;
	
	PTR = (int*)malloc(2*2*sizeof(int));
	
	for(i=0;i<4;i++)
	{
		*(PTR+i) = i;
	}
	
	for(i=0;i<4;i++)
	{
		cout<<*(PTR+i)<<" ";
	}
	
	return 0;
}

Output:

0 1 2 3

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.

Program 3:

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	int *PTR;
	int i=0;
	
	PTR = (int*)calloc(4,sizeof(int));
	
	for(i=0;i<4;i++)
	{
		PTR[i] = i*sizeof('C')+sizeof(3.14);
	}
	
	for(i=0;i<4;i++)
	{
		cout<<*(PTR+i)<<" ";
	}	
	return 0;
}

Output:

8 9 10 11

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.

Program 4:

#include<iostream>
#include<stdlib.h>
using namespace std;

int main()
{
	int *PTR;
	int *PTR_NEW;
	int i=0;
	
	PTR = (int*)calloc(4,sizeof(int));
	
	for(i=0;i<4;i++)
	{
		PTR[i] = i;
	}
	
	PTR_NEW = (int *)realloc(PTR, sizeof(int)*4); 
   	for(i=4;i<8;i++)
	{
		PTR_NEW[i] = i;
	}
	
	for(i=0;i<8;i++)
	{
		cout<<*(PTR_NEW+i)<<" ";
	}
	
	return 0;
}

Output:

0 1 2 3 4 5 6 7

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.

Program 5:

#include<iostream>
#include<stdlib.h>
using namespace std;

class Sample
{
	int A;
	
	public:
		void Sample(int x)
		{
			A=x;
		}
		void print()
		{
			cout<<A<<endl;
		}
};

int main()
{
	Sample *S;
	
	S = new Sample(10);
	
	S->print();
	
	return 0;
}

Output:

main.cpp:10:20: error: return type specification for constructor invalid
   void Sample(int x)
                    ^

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.