如果我忘记在 C++ 中释放内存会发生什么 [英] What happens if I forget to deallocate memory in C++

查看:106
本文介绍了如果我忘记在 C++ 中释放内存会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 C++ 内存管理的问题.据我所知,Java 中不需要释放内存,因为未使用的对象会在某个时候被 JVM 垃圾收集器删除.我的意思是,如果我在C++中忘记释放内存,使用的内存地址会被占用,直到我重新启动机器并且内存中的数据丢失?例如,在下面的代码中,我有一个简单的链表,您可以观察到我没有释放内存(在析构函数中注释):

I have a question regarding the memory management in C++. As far as I know, there is no need to deallocate memory in Java, since unused objects will be removed by the JVM garbage collector at some time. My point is, if I forgot to free memory in C++, the used memory addresses will be occupied until I restart the machine and the data in the memory get lost? For example, on the code below I have a simple linked list and you can observe that I do not free memory(which is commented in the destructor):

#include <iostream>
using namespace std;

typedef struct Node{
Node* next;
int id;
} *ListPtr;
class LinkedList{
public:`ListPtr header;

LinkedList(){
    header = NULL;
}
~LinkedList(){
    /*
     ListPtr a = header,b;
    while(a != NULL)
    {
        b = a;
        a = a -> next;
        delete b;
    }
    delete a,b;
    cout << "Memory freed!"<< endl;
    */
}

public: void Insert(){
    ListPtr new_element = new Node;
    new_element -> next = NULL;

    if(header == NULL){
        header = new_element;
        header -> id = 0;
    }
    else{
        new_element -> next = header;
        new_element -> id = header -> id + 1;
        header = new_element;
    }
}

public: void Print(){
    ListPtr curr = header;
    while(curr != NULL){
             cout << "[" << &curr -> id << "]" << "-->";
        curr = curr -> next;
    }
    cout << "[NULL]"<<endl;
}};

int main(){
LinkedList list;
list.Insert();
list.Insert();
list.Insert();
list.Insert();
list.Insert();
list.Print();
return 0;
}

是不是说那些内存地址会一直被占用,直到我关掉机器?程序执行完成后,诸如整数之类的变量会发生什么?我可以释放他们吗?

Does it mean that those memory addresses will be occupied until I turn the machine off? What happens with variables such as integers after the execution of a program is done? Can I free them?

程序的输出是:[0x8622ac]-->[0x86229c]-->[0x86228c]-->[0x86227c]-->[0x8615bc]-->[NULL]

推荐答案

这取决于操作系统.大多数现代操作系统都会跟踪每个进程分配的内存,因此当进程退出时,应该释放该进程分配的所有内存.

It depends on the operating system. Most modern operating systems keeps track of allocated memory per process, so when the process exits all memory allocated by the process should be released.

但是,在处理完资源后不释放它们会导致泄漏,在内存的情况下,您将有内存泄漏.对于长时间运行的进程,这些泄漏会累积,直到所有资源都用完.

However, not releasing resources after you're done with them will lead to leaks, in the case of memory you will have memory leaks. And for long-running processes, those leaks can accumulate, until all resources are used up.

即使在进程终止时自动释放内存和其他资源,在退出进程之前显式释放分配的资源通常仍然被认为是一种好的方式.

Even when the memory and other resources are released automatically on process termination, it's often still considered good style to explicitly release allocated resources before exiting the process.

这篇关于如果我忘记在 C++ 中释放内存会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆