jeudi 2 mai 2019

What happens when Memory is allocated using a function?

When you return a newly allocated variable through a function , is a copy made and passed and the original deleted automatically?

Im assuming theres no memory leak

#include <iostream>

using namespace std;

int* allocater()
{

    int* x = new int(1);
    return x; 

    // what happens to the memory allocated to x ?
}


int main()
{


int* a = allocater();
int* b = allocater();


cout<<*a<<"  "<<*b;

delete a;
delete b;

// all memory allocated has been deleted?

}

the output is as expected.

Aucun commentaire:

Enregistrer un commentaire