Declaring arrays statically, on the stack and heap

  • Comp Sci
  • Thread starter r0bHadz
  • Start date
  • Tags
    Arrays
In summary, the conversation discusses the difference between declaring an array statically and on the stack in C++. The use of the static keyword is explained, along with the differences between static, heap, and stack memory allocation. The conversation concludes with the mention of a slide that further explains the topic.
  • #1
r0bHadz
194
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
  • #2
Use the static keyword - either at file scope or within a function.
static int a[10000];
 
  • #3
.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.
 
  • #4
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.
 

1. What is the difference between declaring arrays statically, on the stack, and on the heap?

Declaring an array statically means that the memory for the array is allocated at compile time and is fixed for the entire execution of the program. On the other hand, declaring an array on the stack means that the memory for the array is allocated at runtime and is deallocated once the function in which it was declared returns. Declaring an array on the heap means that the memory is allocated dynamically at runtime, and must be manually deallocated using the delete keyword.

2. What are the advantages and disadvantages of declaring arrays statically?

The main advantage of declaring arrays statically is that it is fast and efficient since the memory is allocated at compile time. However, the size of the array must be known at compile time, which can limit its flexibility. Additionally, if the array is large, it can lead to stack overflow errors.

3. How does declaring arrays on the stack affect memory usage?

Declaring arrays on the stack can lead to memory fragmentation, as the memory is allocated and deallocated repeatedly as functions are called and returned. It can also cause stack overflow errors if the array is large and the stack size is limited. However, it can be more memory efficient than declaring arrays on the heap, as there is no need for manual memory management.

4. What is the main benefit of declaring arrays on the heap?

The main benefit of declaring arrays on the heap is that it allows for dynamic memory allocation, meaning the size of the array does not need to be known at compile time. This allows for more flexibility in the size and usage of the array. Additionally, the memory is not tied to a specific function and can be accessed from other parts of the program.

5. How is memory managed when declaring arrays statically, on the stack, and on the heap?

When declaring arrays statically, the memory is managed by the compiler and is fixed for the entire execution of the program. When declaring arrays on the stack, the memory is managed automatically by the program, with the array being deallocated once the function returns. When declaring arrays on the heap, the memory must be manually managed by the programmer using the new and delete keywords.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
755
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
776
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top