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

Discussion Overview

The discussion revolves around the concepts of declaring arrays in C++, specifically focusing on the differences between static, stack, and heap allocation. Participants explore the implications of these different memory allocation strategies in the context of function declarations and variable scope.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about whether declaring an array statically is the same as declaring it on the stack, using an example function to illustrate their point.
  • Another participant suggests using the static keyword to clarify the difference between static and stack allocation.
  • A later reply reiterates the use of the static keyword and questions if this is the only difference between static and stack declarations.
  • Another participant explains that static, stack, and heap memory allocations signify different sections of memory and are used in different contexts, highlighting the initialization and allocation differences among them.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the definitions and implications of static versus stack allocation, indicating that multiple competing views remain on the topic.

Contextual Notes

Some participants reference specific keywords and memory allocation methods, but there are unresolved assumptions regarding the definitions and contexts of static and stack memory, as well as the implications of using the static keyword.

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
3K
Replies
11
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K