Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are class arguments stored after function execution?

I am brand new to C++ and trying to better how understand memory is allocated and stored in class methods.

Lets say I have the following code:

#include <iostream>
using namespace std;

class ExampleClass {

    public:

        void SetStoredAttribute(int Argument);
        int GetStoredAttribute(void);

    private:

        int StoredAttribute;
};


void ExampleClass::SetStoredAttribute(int Argument) {
    StoredAttribute = Argument;
};


int ExampleClass::GetStoredAttribute(void) {
    return StoredAttribute;
};


int main() {

    ExampleClass Object;
    Object.SetStoredAttribute(1);
    cout << Object.GetStoredAttribute();

    return 0;
};

What does C++ do with the argument once my SetStoredAttribute method is finished? Does it free that memory or do I need to do this manually?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Function arguments, including those in member functions, always have automatic storage duration. They start to exist at the beginning of the function scope, and cease to exist at the end of the function scope, and the implementation deals with ensuring they are "allocated" and "deallocated".

On most platforms, this is implemented in two ways.

  • Firstly by a stack, often called The Stack, because it is unique to the thread of execution, with a special purpose register in the CPU pointing to the top.
  • Secondly, the arguments are directly assigned to registers in the CPU.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading