This section contains the C++ find output programs with their explanations on C++ Dynamic Memory Allocation (set 2).
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;
}
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;
}
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;
}
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;
}
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;
}
Answer Program1:
Output:
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.
Answer Program 2:
Output:
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();
Answer Program 3:
Output:
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.
Answer Program 4:
Output:
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.
Answer Program 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer