Explain Static and Dynamic memory allocation How do you decl

Explain Static and Dynamic memory allocation. How do you declare a class Rectangle statically and dynamically? How would you access member functions of Rectangle in each case? What does the following statement mean Rectangle *rectPtr = nullptr;

Solution

Q.1. What is Static and Dynamic Memory Allocation ?

Static Memory Allocation : Static Memory allocation is memory which is allocated by the compiler. When the program starts compiler automatically allocated the memory to the variables. Static variables have fixed size. There are multiple examples of static variables like global variables, variables with keyword static or file scope variables.

Dynamic memory allocation is memory which is allocated at the run time , which means at the execution time. They have variable size. Calloc() and malloc() are used to allocate dynamic memory. Sometimes it is risky to use dynamic memory allocation as it creates space problem.

Q.2. How do you declare Rectange Class Statically and dynamically ?

dynamic allocation :

#include <stdio.h>
#include <stdlib.h>
void main(){
int l,b,*ptr,area=0;
printf(\"Enter length of rectangle :\");
scanf(\"%d\",&l);
   printf(\"Enter breadth of rectangle :\");
scanf(\"%d\",&b);
ptr=(int*)calloc(l,sizeof(int));

Static Allocation :

#include <stdio.h>
#include <stdlib.h>
void main(){
static l,b,area=0;
printf(\"Enter length of rectangle :\");
scanf(\"%d\",&l);
   printf(\"Enter breadth of rectangle :\");
scanf(\"%d\",&b);
area=l*b ;
printf(\"area=%d\",area);
  
}
   ptr=(int*)calloc(b,sizeof(int));
if(ptr==NULL)   
{
printf(\"unable to allocate memory\");
exit(0);
}
area=l*b*(ptr+i);
printf(\"area=%d\",area);
free(ptr);
}

Access Member functions :

Let\'s take an c++ example of accessing member functions.

1. Simple Functions

int length;
int breadth;
int getArea(length,breadth){
   return length * breadth;

}

2. Static functions

class Rectangle
{
public:
static void getArea(){};
};
// calling member function directly with class name
int main()
{
Rectangle::getArea(); }

3. Friend function :

class Rectangle
{
public:
int length;
int breadth;
int getArea();
}
// defined outside class definition
int Area :: getArea()   
{
return length*breadth;
}


int main()
{
Area a1;
a1.length=4;
a1.breadth 6
cout<< \"Area of Rectangle A =\"<< a1.getArea();
}

Q.3 Rectangle *recPtr = nullptr;

The above question means , creating an object of Rectangle class recPtr and default value is null for that object. After that we can assign other pointer\'s value to recPtr. This is for creating an object of a class.

 Explain Static and Dynamic memory allocation. How do you declare a class Rectangle statically and dynamically? How would you access member functions of Rectang
 Explain Static and Dynamic memory allocation. How do you declare a class Rectangle statically and dynamically? How would you access member functions of Rectang

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site