JonaGik
JonaGik

Reputation: 1573

C++ - Declaring Arrays Inside Functions

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.

Upvotes: 2

Views: 3572

Answers (3)

Suryansh Singh
Suryansh Singh

Reputation: 1173

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;
}

Upvotes: 0

David Clarke
David Clarke

Reputation: 13256

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++.

Upvotes: 2

Joel
Joel

Reputation: 5674

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

Upvotes: 4

Related Questions