0

Consider the following code. Hence we have some main.cpp file, and we declare a static pointer.

int main()
{
  static int* array = new int[1000];
}

So after the main ends and before the program finishes running, the static pointer will be deleted from static storage. And what about the memory, which it allocates. When the array will be deleted? Will it leak?

Niklas
  • 13,005
  • 23
  • 79
  • 119
Eduard Rostomyan
  • 7,050
  • 2
  • 37
  • 76

5 Answers5

4

The actual memory will be released to the OS when the program ends because the program ended, but not because you free'd it.

So yes, you could say that it leaks, although a majority of programmers refer to leaks as something that happens continuously, not a one-off allocation you're aware of and gets reclaimed anyway.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

Once your program exits, it's virtual memory also gets revoked, so in this case no memory leak.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
1

No it won't leak. Any modern operating system is capable of handling such a situation and will get back the memory no sooner the process exits, but there are some exceptions like Palm OS where in you have to free the memory allocated.

A detailed answer at below link will clear all your myths.

https://stackoverflow.com/a/654766/2959769

Community
  • 1
  • 1
Vinay Shukla
  • 1,818
  • 13
  • 41
1

After application exits, all memory allocated on the heap is returned to the operating system (*) (in most cases, although I didn't hear about OS, that doesn't do it (**)). So, is it a memory leak? It depends.

In such simple case, there is no harm, but consider following situation:

class C
{
protected:
    //file handles, network connections, database handles

public:
    C()
    {
        //initialize resources
    }

    ~C()
    {
        //release resources
    }
};

static C* globals = new C[10];

In terms of C++, calling new/new[] is directly connected to initialization using constructor, while delete/delete[] also means calling the destructor of a particular class. So in this case resources owned by C will never be released - yes, memory occupied by globals will be freed, but their destructors will never be called. It may or may not be harmful, but remember, that:

  • if you have written data to file and didn't close it, data may not get flushed
  • if you didn't close temporary file, it may not get removed
  • network connections should be always closed gracefully, when possible
  • databases connections should also be explicitly closed

The point is, that memory is not always the most important part. Sometimes, not releasing more complex resources can have serious consequences, that can lead to undefined behaviour of your program.


(*) How is this possible? Most modern operating system have a different variations of "Memory Manager". Userland processes only see so-called virtual memory, which is not directly related to physical memory in a way, that you probably could expect - it essentially maps virtual addresses, into physical addresses in computer memory. After program terminates, its whole virtual space is released and all addresses, that were mapped by it, become available to other processes.

(**) As far as I know, DOS, Windows 3.x and probably MacOS 9 and earlier may not properly reclaim all memory allocated by processes after they terminate. But these are definitely not used anymore.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
0

The answer to your question depends on your definition of a "leak", but I think it's better to answer the question it seems like you intended to ask: will the array of 1000 ints be automatically destroyed, as though we had called delete[] array;.

The answer is no. Everything allocated with new or new[] should be deallocated with delete or delete[]. In your example, only the pointer itself is statically allocated, not the array that it points at.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324