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?
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.
No, you can’t have a memory leak if you don’t use
newormalloc(either directly or indirectly)f n [1000];attempts to create a vector of 1000fobjects. When they get initialized, the constructor is called, printingnand incrementing it.