Memory allocation

I have a few basic question about memory allocation...

1) I did create few multidimensional arrays (like double a[200][200][200] for example), but during the execution it says "segmentation fault".
I surfed the internet and found the page on stackoverflow, which gives a bunch of solutions to this: http://stackoverflow.com/questions/851122/large-2d-array-gives-segmentation-fault

One of the solution was to do this variable static.

So my question here is: what is the difference between declaration int a[200][200][200] and static int a[200][200][200].
Why it makes the difference and I have segmentation fault in one case, but everything is fine when this array is static?

2) Second, even more basic question: when I declare variables inside the function (for example function int main(){}), does these variables stay in the memory after the execution of the program?
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main ()
{
  // declaring variables:
  int a, b;
  int result;

  // process:
  a = 5;
  b = 2;
  result = a - b;

  // print out the result:
  cout << result;

  // terminate the program:
  return 0;
}


In this example, does a, b and result stays in memory after the execution? Or this memory just become free to use/overwrite right after the execution(which I suppose is the case, but want to be sure)?

3) If I declare global variables, does those variables stays in memory also only during the execution, or does they stay in memory?

4) Moving back to the http://stackoverflow.com/questions/851122/large-2d-array-gives-segmentation-fault it says that we could solve the problem of segmentation fault by either declaring those variables as static or declaring those variable as global (first and second fastest ways to resolve this problem, according to the most popular answer)... so my question here: What is the difference between static and global variables?

P.S. I'm sorry if I asked this question not very correctly, I'm a bit confused about it.
1) Double: 8 bytes. Array: 200^3 elements, which is 8,000,000 elements. Theoretical total size occupied (in practice this is significantly higher due to how multidimensional arrays are implemented): 64MB. Generally, stacks aren't that large, and you're causing a stack overflow. Static variables aren't allocated on the stack, rather I believe they're allocated on the heap. I could be wrong was wrong because I had a really bad night and forgot about the existence of storage spaces other than the stack and heap, derp.

2) Those variables are on the stack, so the latter happens (the one you supposed was the case).

3) Those will stay in memory until the program ends.

4) Scope. Static variables can have a limited scope. Global variables are by definition global, accessible from any function (generally). Both will last for the duration of your program's execution.

Hopefully this helped!

-Albatross
Last edited on
Albatross wrote:
rather I believe they're allocated on the heap. I could be wrong.

iirc how static variables are allocated (and where they're allocated to) is implementation specific. It's hard to tell because the stack and heap are implementation specific too, and aren't even mentioned in the C++ standard. All I could find about it in the standard is

Standard ยง3.6.2-1
Non-local variables with static storage duration are initialized
as a consequence of program initiation.

So it doesn't look like there are any guarantees in the C++ standard that a large static array won't cause any segmentation faults.
Actually I don't think the C++ standard talks at all about the effects of large amounts of data with automatic storage duration.

A little bit of Google research suggests that static variables that are zero-initialized are stored in the ".bss" (Block Started by Symbol) portion of the programs data segment, and the size that this segment will require at runtime is stored in the executable.
Last edited on
Question 1:
Here's a good SO post about stack and heap, which will help me answer your first question:
http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

I assume that since you're confused, you probably don't know what stack and heap are, and that's fine. You probably know already that when your program runs, it is given a certain amount of memory space by the program, which is divided up into areas to store static variables, the stack, and the heap.

The stack has a limited size, and is used to store temporary variables allocated for the duration of a function call. when you declare an array in any function, it is allocated on the stack. So if your array is too large, you'll exhaust all the space there and your program will crash because it has no more room to store any variables for your function.

The heap is much larger than the stack, and it is where all dynamically memory allocations happen. So in the stack overflow article you referenced, it was suggested to dynamically allocate the array on the heap, because those variables don't take up precious room on the stack.

One final area of memory is reserved for global variables, since they are outside the scope of everything. In C++, static variables live here as well.

Question 2:
Here's a good reference on variable scoping. This should answer part of your question: http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm

The short answer is no. It's important to remember that main() is still like any other function. The only difference is that it is called first. Any variables declared inside a function and not dynamically allocated are deallocated when execution gets to the end of the function.

Question 3:
All variables in your program only exist in the scope of your program. When your program is finished, the globals are also deallocated.

Question 4:
static is a keyword, so it can be used on variables regardless of whether they are global. See http://www.cprogramming.com/tutorial/statickeyword.html. Some global variables can be static, but not all static variables are global!

@Thumper
Ah, thank you. Good to know!

-Albatross
Thanks a lot to all of you guys!

I didn't know that memory divides up into areas to store different kind of variables.

It helps a lot and I definitely spend my time tonight to read everything you suggested.
Topic archived. No new replies allowed.