Declaring arrays statically, on the stack and heap

  • Context: Comp Sci 
  • Thread starter Thread starter r0bHadz
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary
SUMMARY

In C++, declaring an array statically using the static keyword differs from declaring it on the stack. A static array, such as static int a[10000];, allocates memory before the program runs and initializes it to zero, while stack memory is allocated for local variables within a function. Heap memory, allocated at runtime via malloc() or new, serves a different purpose. Understanding these distinctions is crucial for effective memory management in C++.

PREREQUISITES
  • Understanding of C++ memory management
  • Familiarity with stack and heap memory allocation
  • Knowledge of the static keyword in C++
  • Basic understanding of local variables and function parameters
NEXT STEPS
  • Research C++ memory allocation techniques using malloc() and new
  • Learn about the differences between stack and heap memory in C++
  • Explore the implications of using the static keyword in various scopes
  • Investigate memory management best practices in C++ programming
USEFUL FOR

C++ developers, computer science students, and anyone interested in understanding memory allocation and management in C++ programming.

r0bHadz
Messages
194
Reaction score
17
Homework Statement
Write three functions in C++ : one that declares a large array statistically, one
that declares the same large array on the stack, and one that creates the same
large array from the heap . Call each of the subprograms a large number of times
(at least 100000) and output the time required by each
Relevant Equations
no equations
I'm confused here. I guess my professor mean "statically" in the problem, but anyways, I thought, in C++, that declaring an array statically is the as declaring the array on the stack?

For example, if I have a function:

void staticArr(){
int a[10000];
}

here I am declaring an array statically and it is being declared on the stack.

Am I wrong here?

Thanks
 
Physics news on Phys.org
Use the static keyword - either at file scope or within a function.
static int a[10000];
 
.Scott said:
Use the static keyword - either at file scope or within a function.
static int a[10000];
that's the only difference between declaring static and declaring on a stack?

edit: finally found the slide that is suppose to teach me this stuff. I guess I over looked it. You can ignore the above question, I'll be back if I have anymore though.
 
r0bHadz said:
hat's the only difference between declaring static and declaring on a stack?
All three signify different sections of memory, and are used in different ways.
Static memory is allocated before the program starts to run, and is initialized to zero or whatever the equivalent of zero is for the type of the variable.
Heap memory (AKA free store) is allocated at run time by a call to malloc() or similar in C, or by the new or new[] operators in C++. Stack memory is allocated for local variables and function or method parameters.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
11
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K