2

If I declare an array inside a function, is the memory deallocated upon leaving the function?

I wouldn't have thought so, however, when I declare an array inside a function, write the pointer to the array to a global variable and then attempt (outside the function) to dereference the pointer to an element in the array, I get a memory access violation. I don't get a memory access violation if I use the same code inside the function.

Clarification would be much appreciated.

Thanks in advance.

JonaGik
  • 1,533
  • 5
  • 24
  • 35

3 Answers3

4

All automatic variables are removed after the function exits, including arrays. This would generally be all non-static local variables.

Joel
  • 5,618
  • 1
  • 20
  • 19
  • 3
    All *automatic* variables cease to exist. Local variables can be `static`, in which case their lifetime extends beyond a single function invocation (until the end of the process, in fact). – Ben Voigt Feb 02 '12 at 23:50
2

An array declared in a function is allocated on the program stack. When your program exits the function, the local variables on the stack are popped and the memory containing the array is no longer accessible. The alternative is to new an array pointer which is allocated on the program heap, will survive the function exit and must subsequently be delete'd or a memory leak will occur.

A fairly general explanation of the program stack is a block of memory set aside to hold the local variables for your functions. When a function is called, the amount of memory required to hold the local variables for the function is pushed on the top of the stack, i.e the stack pointer is moved up by that amount. When the function exits, that exact amount of memory is popped off the top of the stack and the stack pointer moves back down to its previous location prior to the function call. The program heap on the other hand is memory that doesn't have stack semantics and is used when a function requests a new block of memory. The program is then responsible for managing the deallocation of that memory.

Smart pointers are often used in C++ to automatically handle the allocation and deallocation of memory to avoid bugs/memory leaks associated with memory management.

A quick google threw up this explanation of stack versus heap in C++.

David Clarke
  • 12,888
  • 9
  • 86
  • 116
  • Thanks very much! We were introduced (briefly) to the heap vs stack in an Engineering Programming course I did, however, I don't understand the distinction. Could you please either explain or point me in the direction of any good resources? Thanks again! – JonaGik Feb 02 '12 at 23:35
  • 1
    Think of each variable in the system as plates. When you enter a function, the parameters and local variables are added to the stack of plates where they get dirty and are used. When you leave the function your plates need to be cleaned up, so they get thrown in the sink. The heap is more like a table, with name cards (addresses). You can put any plate on the table with a new and anyone can use it if they know the name (i.e. address), but you eventually have to clean up after yourself with a delete. No one likes a messy table :-) – Joel Feb 02 '12 at 23:45
  • @Joel so the stack is effectively garbage collected - except all memory is deallocated upon exiting the function? – JonaGik Feb 03 '12 at 00:16
  • Sort of. Garbage collection is generally not deterministic, but stack deallocation _always_ happens right after you leave the function. – Joel Feb 03 '12 at 00:30
  • @JonaGik: The stack implements scope-based resource management (SBRM), also known as RAII, where each plate in the stack of plates corresponds to a lexical scope (e.g., a set of curly braces). This is a memory management scheme, and therefore does a similar job as garbage collection. But GC guarantees that an object exists as long as you have a reference to it (e.g. a pointer), which is quite different and can lead to memory leaks. – Potatoswatter Feb 03 '12 at 01:20
  • @Potatoswatter, so how do I stop deallocation (i.e. if I understand it correctly, get memory from the heap)? Using the 'new' keyword? – JonaGik Feb 03 '12 at 09:11
  • Yes, you would use the new keyword, and assign the value to a pointer. You would later have to delete that pointer to remove the memory, or you might end up with a memory leak. And that would be bad. – Joel Feb 03 '12 at 18:32
  • 1
    @JonaGik: In modern C++, we usually use a smart pointer class, such as `unique_ptr` or `shared_ptr`. These objects wrap pointers and perform the `delete` operation from their destructors. Because `new` and `delete` can be tricky to use directly, it's better to use a higher-level construct if you can. – Potatoswatter Feb 05 '12 at 08:31
0

When a program exits a function all the local variables (including the arrays you declared inside) on the stack are popped and are no longer accessible outside the function. Like Follows:

(Does not work)

void func1(){
    int x[3]={1,2,3};
}

int main(){
    func1()
    cout<<x[1];
    return 0;
}

Now your second bet would be to somehow return the array, which can be done in following ways:

1. Since C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.

(Works)

int * func( ) {
    static int  x[3]={1,2,3};
    return r;           
    }       
int main () {    
     // a pointer to an int.
    int *x;
    x = func();
    cout<<x[1];           
    return 0;        
}

2. Another way is to pass an pointer as an argument to a function and pass the array to the variable.

(Does Not Work)

void func(int *ptr){
    int x[3]={1,2,3};
    ptr=x;
}
int main{
    int *ptr;
    cout<<ptr[1];
    retrun 0;
}

This does not work since array declared inside func is local variable and gets destroyed after program exits function, that's why we have to declare the array dynamically

(Does Not Works)

void func(int *ptr){
    int *x{ new int[3]{1,2,3 } };
    ptr=x;
}
int main(){
    int *ptr;
    func(ptr);
    cout<<ptr[1];
    return 0;
}

Even this didn't work that's because if a pointer is passed to a function as a parameter and tried to be modified then the changes made to the pointer does not reflects back outside that function. This is because only a copy of the pointer is passed to the function. The above problem can be resolved by passing the address of the pointer to the function instead of a copy of the actual function. For this, the function parameter should accept a “pointer to pointer” as shown in the below program:

(Works, Pointer to pointer)

#include<bits/stdc++.h>
using namespace std;
void func(int **ptr){
    int *x{ new int[3]{1,2,3 } };
    //cout<<*ptr<<endl;
    //cout<<ptr<<endl;
    *ptr=x;
    //cout<<ptr<<endl;
    //cout<<x<<endl;
}
int main(){
    int *ptr;
    func(&ptr);
    cout<<ptr[1]<<endl;
    return 0;
}

Or by passing reference allows called function to modify a local variable of the caller function. For example, consider the following example program where fun() is able to modify local variable x of main().

(Works, Reference to pointer)

#include<bits/stdc++.h>
using namespace std;
void func(int *&ptr){
    int *x{ new int[3]{1,2,3 } };
    //cout<<ptr<<endl;
    ptr=x;
    //cout<<ptr<<endl;
    //cout<<x<<endl;
}
int main(){
    int *ptr;
    func(ptr);
    cout<<ptr[1]<<endl;
    return 0;
}
Suryansh Singh
  • 1,123
  • 7
  • 15