user893373
user893373

Reputation:

Will this program cause memory leak

I asked my friend if he can print from 1 to 1000 without using loops or coditionals after being intrigued by this thread:

Printing 1 to 1000 without loop or conditionals

He replied with this program.

#include <iostream>
using namespace std;

static int n = 1;

class f {
public:

    f() {
        cout << n++ << endl;
    }
};

int main(int argc, char *argv[]) {
    f n [1000];
}

Running the program outputs ok. But when I close the program on netbeans it seems to be still running and consuming memory. Is the program causing memory leak? And can someone explain how this small program works?

Upvotes: 0

Views: 278

Answers (5)

harper
harper

Reputation: 13690

There is no obvious memory leak in the program. You don't allocate any object dynamically and you can't forget to release anything.

What does happen is that you call the array constructor for the array n. This itself calls fear each array element the constructor f::f(). Therefore you get the output. Well there you have a loop, but not at language level.

Upvotes: 0

Christian Stieber
Christian Stieber

Reputation: 12496

You shouldn't even have to close the program, it terminates automatically. Must be something that your IDE does, or whatever you work with.

How it works? Class "f" has a constructor that increases a global variable and outputs it. He creates an array with 1000 instances of "f", which causes the above to happen 1000 times, once for every "f" in the array.

Upvotes: 1

gibraltar
gibraltar

Reputation: 1708

Whenever this is done

f n[i]

Constructor is f() is called. Since with every call global variable is being incremented. So for every object of f from 1 to 1000 , the value of n is printed.

Regarding memory leak, there is none. The array is a local variable and is destroyed when the program ends. In order to enable dynamic allocation, use new keyword.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258548

But when I close the program on netbeans it seems to be still running and consuming memory

When you close a program or it terminates, regardless of whether or not it has memory leaks, the memory will be released. I'm pretty sure you're not terminating it correctly.

Is the program causing memory leak?

No, you can't have a memory leak if you don't use new or malloc (either directly or indirectly)

And can someone explain how this small program works?

f n [1000]; attempts to create a vector of 1000 f objects. When they get initialized, the constructor is called, printing n and incrementing it.

Upvotes: 9

Pubby
Pubby

Reputation: 53017

No, there is no memory leak.

Arrays use automatic storage which gets automatically freed when they go out of scope.

Using dynamic storage via new will cause a memory leak however:

int main(int argc, char *argv[]) {
    new f[1000]; // this leaks
}

And can someone explain how this small program works?

Constructing an array calls the default constructor for each element of the array. So f() is just being called 1000 times.

Upvotes: 3

Related Questions