Memory

Hi,
I some time ago, I was reading a book about c++ programming. And here was chapter about memory. It said if you create variable in stack memory it will be freed automatically when it will go out of scope. But I wander if it really do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void print();

int main()
{
	for (int i = 0; i < 10; i++)
	{
		print();
	}
	
	return 0;
}

void print()
{
	int var = 1;
	std::cout << &var << std::endl;
	return;
}


var variable is a local to function print(), so it must be created and destroyed 10 times in this function. As it go out of scope 10 times? But if I run this program it prints the same address all 10 times. So it is not destroyed until program is running. Is it right?
So, if program do not allocate dynamic memory with "new", does it memory usage change when program runs? Or not?
So it is not destroyed until program is running. Is it right?


THat makes no snese at all. I suspect you didn't mean that, probably because English is not your first langauge. That said, of course it isn't destroyed until program is running. It also isn't created until program is running. Before the program is running, nothing at all happens. When you run the program, then things start happening.

In your program, you call the same funciton ten times. So the exact same function has memory allocated on the exact same stack ten times, and thus the exact same memory is used, ten times.
Last edited on
I could not understand your answer :S

So lets change program a little.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

void print();

int main()
{
	int var = 2;
	
	for (int i = 0; i < 10; i++)
	{
		print();
	}
	
	int var2 = 3;
	
	std::cout << &var << std::endl;
	std::cout << &var2 << std::endl;
	
	return 0;
}

void print()
{
	int var = 1;
	std::cout << &var << std::endl;
	return;
}


Now is all 3 variables created at the same time? When program starts and destroyed when program ends? Or is they created when program gets to line of variable declaration and destroyed at the last line they are used?
For example: will variable var of function print() exist when program comes to line 14, or will it be destroyed before variable var2 will be created?

P.S. variable not it's value.
Last edited on
The lifetime of automatic objects begins at the point of declaration and ends at the end of scope. In your program,

main's var is created at line 7 and destroyed at line 20
main's i is created at line 9 and destroyed at line 12 (after the loop ends)
main's var2 is created at line 14 and destroyed at line 20
print's var is created at line 24 and destroyed at line 27.

The storage (memory locations) in which these objects are stored when they are created may be reused.
You could test this by making a program that doesn't define all the variables right away in the beginning and then use system("pause");.
Monitor your ram usage using something like task manager (ctrl+alt+delete). both before you reach system("pause"); and after.

should look something like this:(the code below hasn't been tested)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
int main(){
    system("pause");//check ram usage here through task manager
    int var[1000];//just make it big enough to be noticeable up on the task managers ram
    system("pause");//check ram usage here through task manager
	for (int i = 0; i < 10; i++)
	{
		print();
	}
	
	int var[1000];
	
	std::cout << &var << std::endl;
	std::cout << &var2 << std::endl;
	
	return 0;
}

void print()
{
        system("pause");//check ram usage here through task manager
	int var[1000];
	std::cout << &var << std::endl;
        system("pause");//check ram usage here through task manager
	return;
}
}
Thanks,
I got it.
It do not always change at a right time (maybe because of system monitor), but it shows how memory is allocated and freed. My version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

void print();
int next;

int main()
{
	std::cout << "Start: ";
	std::cin >> next;
	
	int var[100];
	std::cout << "var[100]: ";
	std::cin >> next;
	
	for (int i = 0; i < 10; i++)
	{
		print();
	}
	std::cout << "end for(): ";
	std::cin >> next;
	
	int var2[10000];
	std::cout << "var[10000]: ";
	std::cin >> next;
	
	std::cout << &var << std::endl;
	std::cout << &var2 << std::endl;
	std::cout << "end: ";
	std::cin >> next;
	
	return 0;
}

void print()
{
	int var[100000];
	std::cout << "var[100000]: ";
	std::cin >> next;
	std::cout << &var << std::endl;
	return;
}


I use Ubuntu so system() won't compile. So I used cin to stop.
Topic archived. No new replies allowed.