-2

From my knowledge after a function is called in c++ its memory is deallocated for another variables. If it doesn't allocates to another variable then variable should have allocated memory dynamically. I'm confused how the function display() displays array values when it isn't allocated memory dynamically.

#include<iostream>
using namespace std;
void init_values(int arr[]){
    for(int i=0;i<100;i++){
        arr[i]=i;
    }
}
void display(int arr[]){
    for(int i=0;i<100;i++){
        cout<<arr[i] << " ";
    }
}
int main(){
    int arr[100];
    init_values(arr);
    display(arr);
}

I expected the function displays garbage or will show an error. But it displayed the values correctly.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • _"I expected the function displays garbage or will show an error."_ Why so exactly? There's actually nothing wrong with the code? – πάντα ῥεῖ Jun 18 '19 at 17:34
  • `arr` is not local to a subroutine. It won't be freed until the end of `main` – stark Jun 18 '19 at 17:35
  • Can anyone help me. How the memory model in the above example works. – pranav suresh Jun 18 '19 at 17:35
  • 1
    @pranavsuresh _"How the memory model in the above example works."_ `arr` is locally allocated and both functions are seeing this local allocation. – πάντα ῥεῖ Jun 18 '19 at 17:36
  • The scope of your allocation `int arr[100];` is the `main` function, not the other two functions. `arr` remains alive through the lifetime of your program. – Robert Harvey Jun 18 '19 at 17:38
  • main() allocates the array, calls the first function, passes a pointer to the first function that fills it, the first function returns, but arr is still kept alive by main, that passes arr to the second function. for example, the "i" variable in the 2 functions is destroyed when they return but arr gets destroy at the end of main() – CuriouslyRecurringThoughts Jun 18 '19 at 17:39
  • @Robert If you still want to hammer: I'd do so for _unclear what's asked about_. – πάντα ῥεῖ Jun 18 '19 at 17:41
  • In both `init_values` and `display`, each have a separate, independent local `i` primitive object. That object is destructed at the end of each of those routines (well, technically destructed at the termination of the for loop, since it is locally scoped to the for loop). – Eljay Jun 18 '19 at 17:42
  • @πάνταῥεῖ can you explain me how the stack memory works in this case? – pranav suresh Jun 18 '19 at 17:57
  • @pranavsuresh I have no clue what you're asking about? May be [this](https://stackoverflow.com/questions/1461432/what-is-array-decaying) helps. your functions see a pointer to the locally allocated memory in `main()`. If that should be your question, there's no copying involved. – πάντα ῥεῖ Jun 18 '19 at 18:00

1 Answers1

0
void init_values(int arr[]){
    for(int i=0;i<100;i++){ // i is created here
        arr[i]=i;
    } // i goes out of scope here
}

No problems here, there's no attempt to use i later.

void display(int arr[]){
    for(int i=0;i<100;i++){ // i is created here
        cout<<arr[i] << " ";
    } // i goes out of scope here

Again, no problem.

int main(){
    int arr[100]; // arr is created here
    init_values(arr);
    display(arr);
} // arr goes out of scope here

When we call init_values and display, arr is still in scope since its lifetime ends at the end of the scope it was created in. So it's still in scope during those function calls.

Now, this would be a problem:

int *init_values(void){
    int arr[100];
    for(int i=0;i<100;i++){
        arr[i]=i;
    }
    return arr;
}

Why? Think about it:

int *init_values(void){
    int arr[100]; // arr is created here
    for(int i=0;i<100;i++){
        arr[i]=i;
    }
    return arr;
} // arr goes out of scope here

So the caller of init_values would receive a pointer to arr's contents even though arr does not exist after we exit this function. This function's return value could not be safely used. Your code doesn't do this.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278