stack vs. heap memory limits

Jun 23, 2009 at 2:09am
I am running VC++ 2008 Express in Vista. I was experimenting with stack vs. heap memory allocation and immediately ran into stack overflow line 24 trying to allocate a 2M array. That seems like a pretty low limit? With the heap allocation if I comment out line 16 and increase loop limit I could go up to 2G almost I think. Also, when I run the windows task manager and look at memory useage for my program, running either in debugger or from a console window, I see that each loop through memoryFunc() bumps the memory up and down by 10 Meg, but for memoryFunc2() the memory goes up my 1 Meg upon first entry to the function, then stays at that level no matter how many times the function is entered or exited. Almost like the runtime is thinking the program might use that stack frame again so don't release it yet?

So my questions are:

1. Why is stack memory not released upon exit from memoryFunc2()?
2. Is there a way to find out what the stack memory limit is for 1 or many objects? I read this post but am still a little confused.
http://www.cplusplus.com/forum/beginner/3123/
3. I want to verify there is no way to catch a stack overflow and do graceful recovery from line 24 as mentioned in this article?
http://www.cplusplus.com/forum/general/9768/

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>

using namespace std;

char myChar;

class myClass
{
public:
	string id;
	char *myCharray; 
	myClass(const char* name) : id(name) { myCharray = new char[10000000];}
	~myClass() {
		printf("myClass dtor for: %s\n", id.c_str());
		delete myCharray;
	}
};

class myClass2
{
public:
	string id;
	//char myCharray[2000000];	//stack overflow, on first entry
	char myCharray[1000000]; 
	myClass2(const char* name) : id(name) {}
	~myClass2() {
		printf("myClass dtor for: %s\n", id.c_str());
		//myCharray automatically deleted when out of scope
	}
};

void memoryFunc() {
		myClass mc("heap");
		printf("exiting memoryFunc(): Enter a character to continue \n");
		cin >> myChar;
}

void memoryFunc2() {
		myClass2 mc2("stack");
		printf("exiting memoryFunc(): Enter a character to continue \n");
		cin >> myChar;
}
int main()
{
	char myChar;

  while (1) {
	printf("enter h for heap, s for stack \n");
	cin >> myChar;

	if (myChar=='h') {
		for (int i=0; i<3; i++) {
			printf("calling heap function(): Enter a character to continue \n");
			cin >> myChar;
			memoryFunc();
		}
	}
	else if (myChar=='s') {
		for (int i=0; i<3; i++) {
			printf("calling stack function(): Enter a character to continue \n");
			cin >> myChar;
			memoryFunc2();
		}
	}
  }//while (1)
		
	printf("leaving main(): Enter a character to continue \n");
	cin >> myChar;

	return 0;
}
Jun 23, 2009 at 2:47am
That seems like a pretty low limit?
The stack is not designed to hold huge arrays. That's why you have the heap. The stack is there as a quick allocator and to serve as the call stack.

1. Why is stack memory not released upon exit from memoryFunc2()?
This is just not true. Everything that's constructed on the stack has automatic storage duration, and are always destructed as soon as they go out of scope.
AFAIK, the system is not able to measure stack utilization. The system will tell you how many private bytes a program has allocated, but the stack is always fully allocated, so the system isn't able to distinguish between a full stack and an empty stack. Of course, this depends completely on the system and the implementation. I'm describing Windows and Linux.
You must be looking at the memory footprint of something else.

2. Is there a way to find out what the stack memory limit is for 1 or many objects? I read this post but am still a little confused.
http://www.cplusplus.com/forum/beginner/3123/
"If you have to ask, you can't afford it."
Always assume the stack to be very limited. Finding out the stack limit is not very useful, and you shouldn't be allocating terribly large things in it, anyway.
That thread deals mostly with the heap, not the stack.
3. I want to verify there is no way to catch a stack overflow and do graceful recovery from line 24 as mentioned in this article?
You can pretty much forget about recovery. Once you've corrupted memory, the best you can do is show a friendly message and then quit. It's consensus that you shouldn't let the program crash, but there isn't really much of a difference.


Your myClass has a memory leak of 10^7-1 bytes at line 16.

EDIT: Okay, after testing it, it seems Windows can tell how tall has the stack grown, but doesn't report when the stack shrinks again. You can rest assured that the stack is being freed, though.
Last edited on Jun 23, 2009 at 3:08am
Jun 23, 2009 at 3:22am

Your myClass has a memory leak of 10^7-1 bytes at line 16

I totally missed that one, I think I should have used?
 
delete [] myCharray;


Strangely, when I was monitoring memory usage in Windows Task Manager the memory increased and decreased by 10M each time through the loop, maybe Windows compiler assumes "[]" for arrays of primitives. But obviously I should never rely on that.

Thanks helios.
Topic archived. No new replies allowed.