×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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

Program 1:

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

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

int main()
{
	Sample **S;
	
	S = (Sample*)malloc(sizeof(Sample)*2);
	
	S[0].set(10);
	S[0].print();
	
	S[1].set(20);
	S[1].print();	
	
	return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:25:38: error: cannot convert ‘Sample*’ to ‘Sample**’ in assignment
  S = (Sample*)malloc(sizeof(Sample)*2);
                                      ^
main.cpp:27:7: error: request for member ‘set’ in ‘* S’, which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
  S[0].set(10);
       ^~~
main.cpp:28:7: error: request for member ‘print’ in ‘* S’, which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
  S[0].print();
       ^~~~~
main.cpp:30:7: error: request for member ‘set’ in ‘*(S + 8u)’, which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
  S[1].set(20);
       ^~~
main.cpp:31:7: error: request for member ‘print’ in ‘*(S + 8u)’, which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
  S[1].print(); 
       ^~~~~

Explanation:

It will generate errors, because in the main() function, we trying to allocate a dynamic array of objects using malloc(), but we used a double-pointer, it can be done using a single pointer.

Program 2:

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

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

int main()
{
	Sample *S;
	
	S = new Sample[2];
	
	S[0]->set(10);
	S[0]->print();
	
	S[1]->set(20);
	S[1]->print();
	
	return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:27:6: error: base operand of ‘->’ has non-pointer type ‘Sample’
  S[0]->set(10);
      ^~
main.cpp:28:6: error: base operand of ‘->’ has non-pointer type ‘Sample’
  S[0]->print();
      ^~
main.cpp:30:6: error: base operand of ‘->’ has non-pointer type ‘Sample’
  S[1]->set(20);
      ^~
main.cpp:31:6: error: base operand of ‘->’ has non-pointer type ‘Sample’
  S[1]->print();
      ^~

Explanation:

It will generate errors. In the main() function we allocated dynamic array using the new operator, but we are not accessing member functions properly.

We need to used referential operator -> instead of dot (.) operator. We need to use the below statements to access member functions.

S[0].set(10);
S[0].print();
	
S[1].set(20);
S[1].print();

Program 3:

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

int main()
{
	long *ptr;
	
	long large =0;
	
	ptr = new long(5);
	
	ptr[0]=5;ptr[1]=8;ptr[2]=7;ptr[3]=o;ptr[4]=6;
	
	
	large = ptr[0];
	for(int i=1;i<4;i++)
	{
		if(large>ptr[i])
			large = ptr[i];
	}
	
	cout<<large<<endl;
	
	
	return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:36: error: ‘o’ was not declared in this scope
  ptr[0]=5;ptr[1]=8;ptr[2]=7;ptr[3]=o;ptr[4]=6;
                                    ^

Explanation:

It will generate a syntax error. Because of the below statement.

ptr[3]=o;

In the above statement we assign 'o' instead of zero, and 'o' is not declared in the program.

Program 4:

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

int main()
{
	long *ptr;
		
	ptr = new long(5);
	
	ptr[0]=5;ptr[1]=8;ptr[2]=7;ptr[3]=6;ptr[4]=6;
	
	
	for(int i=0;i<4;i++)
	{
		if(ptr[i]%2==0)
			cout<< ptr[i]<<" ";
	
	}
	
	return 0;
}

Output:

8 6

Explanation:

It will print "8 6" on the console screen. Let's understand the program.

In the above program, we allocated memory space for 5 long values using the new operator. Then assign values to them. And using the loop we found even numbers and print them on the console screen.

Program 5:

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

typedef struct st
{
	int A;
}STR;

int main()
{
	STR *S;
	
	S = new STR();
	
	S[0].A = 10;
	
	cout<<S->A<<endl;
	
	free(S);
	
	return 0;
}

Output:

10

Explanation:

It will print "10" on the console screen. Let's understand the program.

In the above program, created a structure with typedef that contains a member A.

Now, look to the main() function, here we created a pointer S, and initialized pointer using the new operator. Then assign value to the A using the below statement.

S[0].A = 10;

The above statement is similar to S->A=10. And, then we print the value of A using cout. And free pointer S using free() function.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.