简体   繁体   English

该程序会导致内存泄漏吗

[英]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: 我问我的朋友在被这个线程吸引后,是否可以在不使用循环或条件的情况下从1打印到1000:

Printing 1 to 1000 without loop or conditionals 无循环或有条件打印1至1000

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. 但是,当我在netbeans上关闭程序时,它似乎仍在运行并正在消耗内存。 Is the program causing memory leak? 程序是否引起内存泄漏? And can someone explain how this small program works? 有人可以解释这个小程序如何工作吗?

But when I close the program on netbeans it seems to be still running and consuming memory 但是,当我在netbeans上关闭程序时,它似乎仍在运行并消耗内存

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) 不,如果不使用newmalloc (直接或间接),则不会发生内存泄漏

And can someone explain how this small program works? 有人可以解释这个小程序如何工作吗?

fn [1000]; attempts to create a vector of 1000 f objects. 尝试创建一个包含1000个f对象的向量。 When they get initialized, the constructor is called, printing n and incrementing it. 当它们被初始化时,构造函数被调用,打印n并将其递增。

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: 通过new使用动态存储导致内存泄漏:

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. 因此, f()仅被调用1000次。

You shouldn't even have to close the program, it terminates automatically. 您甚至不必关闭程序,它会自动终止。 Must be something that your IDE does, or whatever you work with. 必须是您的IDE可以执行的操作,或者您可以使用的任何操作。

How it works? 这个怎么运作? Class "f" has a constructor that increases a global variable and outputs it. 类“ f”具有一个构造函数,该构造函数增加全局变量并将其输出。 He creates an array with 1000 instances of "f", which causes the above to happen 1000 times, once for every "f" in the array. 他创建了一个包含1000个“ f”实例的数组,这导致以上情况发生1000次,对于数组中的每个“ f”都发生一次。

Whenever this is done 每当这样做

f n[i]

Constructor is f() is called. 构造函数被f()调用。 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. 因此,对于从1到1000的f的每个对象,将打印n的值。

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. 为了启用动态分配,请使用new关键字。

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. 确实发生的是,您为数组n调用了数组构造函数。 This itself calls fear each array element the constructor f::f(). 这本身称为恐惧每个数组元素的构造函数f :: f()。 Therefore you get the output. 因此,您得到输出。 Well there you have a loop, but not at language level. 好了,您有一个循环,但没有语言水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM