Professional Documents
Culture Documents
WINSEM2023-24_BCSE102L_TH_VL2023240501147_2024-02-23_Reference-Material-III
WINSEM2023-24_BCSE102L_TH_VL2023240501147_2024-02-23_Reference-Material-III
WINSEM2023-24_BCSE102L_TH_VL2023240501147_2024-02-23_Reference-Material-III
int x;
int main(void)
{
int y;
char *str;
y = 4;
printf("stack memory: %d\n", y);
str = malloc(100*sizeof(char));
str[0] = 'm';
printf("heap memory: %c\n", str[0]);
free(str);
return 0;
}
The variable x is static storage, because of its global nature.
Both y and str are dynamic stack storage which is deallocated when
the program ends. The function malloc() is used to allocate 100
pieces of of dynamic heap storage, each the size of char, to str.
Conversely, the function free(), deallocates the memory associated
with str.
REPORT THIS AD
Static Variables inside Functions
Static variables when used inside function are initialized only once,
and then they hold there value even through function calls.
These static variables are stored on static storage area , not in stack.
void counter()
int main()
for(int i=0;i<5;i++)
counter();
0 1 2 3 4
Let's se the same program's output without using static variable.
void counter()
int count=0;
int main(0
for(int i=0;i<5;i++)
counter();
0 0 0 0 0
If we do not use static keyword, the variable count, is reinitialized
everytime when counter() function is called, and gets destroyed each
time when counter() functions ends. But, if we make it static, once
initialized count will have a scope till the end of main() function and
it will carry its value through function calls too.
If you don't initialize a static variable, they are by default initialized
to zero.
int X::i=1;
int main()
{
X obj;
cout << obj.i; // prints value of i
}
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 5.0, double b = 5.0, double h = 5.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
Static member functions have a class scope and they do not have
access to the this pointer of the class. You could use a static
member function to determine whether some objects of the class
have been created or not.
class X
{
public:
// statement
};
int main()
Live Demo
#include <iostream>
class Box {
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h =
2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
// Print total number of objects before creating
object.
cout << "Inital Stage Count: " << Box::getCount() <<
endl;
return 0;
}
Static keyword works in the same way for class objects too. Objects
declared static are allocated storage in static storage area, and have
scope till the end of program.
#include<iostream>
using namespace std;
class Sam
{
int m = 0;
public:
Sam()
{
m = 0;
cout << "javatpoint\n";
}
~Sam()
{
cout << "Data Science\n";
}
};
int main()
{
int o = 0;
if (o==0)
{
Sam nex;
}
cout << "Machine Learning\n";
}
Output:
javatpoint
Data Science
Machine Learning
Static object
#include<iostream>
using namespace std;
class Sam
{
int m = 0;
public:
Sam()
{
m = 0;
cout << "javatpoint\n";
}
~Sam()
{
cout << "Data Science\n";
}
};
int main()
{
int o = 0;
if (o==0)
{
static Sam nex;
}
cout << "Machine Learning\n";
}
Output:
javatpoint
Machine Learning
Data Science
If the object had been created as a non-static object, then the scope of the variable
would have been only inside the if block, and as soon as the control of the if the
block had gotten over, the destructor would have been invoked. This problem must
be avoided, so the object has to be created static as it has been done in the program.
This has made the destructor gets invoked once the main has ended. It is only
possible because of the static object and its scope throughout the program's lifetime.
class Abc
int i;
public:
Abc()
i=0;
~Abc()
};
void f()
int main()
int x=0;
if(x==0)
f();
Output:
constructor
END
destructor
You must be thinking, why was the destructor not called upon the
end of the scope of if condition, where the reference of
object obj should get destroyed. This is because object was static,
which has scope till the program's lifetime, hence destructor for this
object was called when main() function exits.