Could this cause a memory leak, returning array from function?

I have the following code which works fine except I'm not sure if I need to delete the returned_array pointer in the int main() or if its automatically deleted. I will guess its not automatically deleted and assume I should use delete returned_array; after the cout is finished. Any suggestions?

#include <iostream>

double* pass_return_array(double passed[])
{
        double* returned_array = new double[3];

        for(int index = 0; index < 3; index++)
            returned_array[index] = passed[index];

        return returned_array;
}
int main()
{
        double passed[3];
        double* returned_array;

        for(int index = 0; index < 3; index++)
            passed[index] = index + 100;

        returned_array = pass_return_array(passed);

        for(int index = 0; index < 3; index++)
            std::cout<<returned_array[index]<<std::endl;

        return 0;
}

Answers


You are right: you created the array in the function, you also are responsible for its deallocation. You should call delete[] returned_array; as soon as you no longer need it.

Note, that OS always cleans up all resources allocated by the program when it terminates, so there won't be any system-wide memory leaks. But it's a very bad practice to leave the deallocation to the OS, you should always deallocate all resources you allocated (and that includes other kinds of things - for example handles to files or brushes).

Consider using std::vector or std::array instead - if used as a simple local variable, they will take care of memory they allocated and you won't have to remember about it. That's the power of C++ in work :)


It's good practice to clean up all resources that you allocate regardless of whether or not you think it will be done automagically for you.

It's also good practice for the responsibility for a resource to follow the resource itself. That means, if you return some dynamically allocated memory from a function, you also "return" the responsibility for it.

That's not to say it has to be cleaned up in the function caller since that may break encapsulation (you don't necessarily even know if it's dynamically allocated). But, even if you pass the resource back to the code/library that created it, that is again passing the responsibility too, as per the following pseudo-code:

def makeSomething():
    something = unknownMethodForCreatingSomething()

    #makeSomething() has responsibility here but passes it off to caller

    return something

def killSomething(something):
    # Responsibility with killSomething() at this point

    equallyUnknownMethodForDestroyingSomething(something)

def main():
    xyzzy = makeSomething()

    # main() has responsibility for xyzzy here but hands it off in next line

    killSomething (xyzzy)

Need Your Help

I need a backend system that is integrated with web services, is there an open source solution?

web-services open-source mobile-application

I'm basically familiar with what I need in order to setup web services to talk with a centralized db, but if I don't have to go through and do all the work, I'd rather not.

android how to use startActivityForResult for doing action after coming back from mail composer

android email android-activity

I'm sending an email, but I need to go to a different activity when the user comes back from the email composer, how to accomplish this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.