内存管理基础

Memory management basics?

本文关键字:管理 内存      更新时间:2023-10-16

使用下面的代码:

#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
    int a;
    const char* t;
};
int main()
{
    std::vector<x> instances;
    
    while(true)
    {
        printf("wait for key1n");
        getchar();
        getchar();
        
        {
            for(int i = 0; i < 100000; i++)
            {
                x n;
                n.a = i;
                n.t = "x instance";
                instances.push_back(n);
            }
            //x instance deleted right?
        }
        
        {
            x x1, x2, x3;
            x1 = instances[0];
            x2 = instances[1];
            x3 = instances[2];
            
            std::cout << x1.t << std::endl;
            std::cout << x2.t << std::endl;
            std::cout << x3.t << std::endl;
            
            instances.clear();
        }
        
        printf("wait for key2n");
        getchar();
        getchar();
    }
    
    return 0;
}

我得到这样的输出:

wait for key2
wait for key1
x instance
x instance
x instance

这很可爱,但我认为我应该得到这样的输出:

wait for key2
wait for key1
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½

因为必须删除x个struct实例。我错了吗?真正的实现应该是这样的:

#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
    int a;
    const char* t;
};
int main()
{
    std::vector<x*> instances;
    
    while(true)
    {
        printf("wait for key1n");
        getchar();
        getchar();
        
        {
            for(int i = 0; i < 100000; i++)
            {
                x* n = new x();
                n->a = i;
                n->t = "x instance";
                instances.push_back(n);
            }
        }
        
        {
            x* x1 = 0;
            x* x2 = 0;
            x* x3 = 0;
            x1 = instances[0];
            x2 = instances[1];
            x3 = instances[2];
            
            std::cout << x1->t << std::endl;
            std::cout << x2->t << std::endl;
            std::cout << x3->t << std::endl;
            
            instances.clear(); /* delete x instances one-by-one like 'delete instances[i];' */
         }
        
        printf("wait for key2n");
        getchar();
        getchar();
    }
    
    return 0;
}

我不清楚内存管理。为什么我还能得到(非新)清理后的"x个实例"?例子吗?

我已经在link>>并且我认为必须删除for循环中的x个实例?

<标题> 更新

这是我为其他人(像我这样的初学者)提供的示例实现。我将使用套接字io数据包的同步队列我不关心thread.join(),因为我的线程只是工人,不是管理者!(多么真实的模拟!)

#include <iostream>
#include <thread>
#include <chrono>
bool b1 = true;
bool b2 = true;
//Of course you can create only 1 boolean for all threads (isAlive should be a good name for it)
//but this way provides more detailed thread aliveness control.
void process(bool* ref, int id)
{
    bool my = *ref;
    while (my)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        std::cout << "thread#" << id << std::endl;
        my = *ref;
    }
    std::cout << "thread#" << id << " end." << std::endl;
}
int main()
{
    std::thread(process, &b1, 0).detach();
    std::thread(process, &b2, 1).detach();
    std::cin.get();
    b1 = false;
    
    std::cin.get();
    b2 = false;
    
    //MS-DOS :(
    std::cin.get();
    return 0;
}

Posibble输出:

thread#thread#10
thread#0
thread#1
thread#1
thread#0
//Hit the enter!
thread#1
thread#0
thread#0 end.

线程# 1线程1号//按回车键!线程1号线程1号结束。//按回车键!

当您使用push_backvector添加元素时,该元素将复制到vector中。这意味着vector有自己的元素副本,即使元素被删除(由您删除或在作用域结束时删除),vector仍然包含该元素。当vector对象被删除时,vector对象会自己一个一个地删除它的元素,所以你不必这样做。

您可以在这里找到更多关于push_back的信息:

在vector对象的最后一个元素之后,在vector对象的末尾添加一个新元素。val的内容被复制(或移动)到新元素中。

你的"改进"版本,动态分配变量,是对指针的误用。如果您所做的只是将元素插入到vector中,那么就不需要动态分配它们,因为向量甚至不包含动态分配的值-仅包含它们的副本。

您正在将副本放入向量中。

for(int i = 0; i < 100000; i++)
{
    x n;
    n.a = i;
    n.t = "x instance";
    instances.push_back(n); // <--- Copy of n is created and added to instances
}

在执行push_back操作时,将实例的副本插入到vector中。因此,删除原始实例并不重要。这些字符串可能会丢失,因为它们是在本地分配的,原始字符串被销毁了。

关于未被删除的字符串,可能是由于您为指针分配了静态字符串。

不,不应该删除。

在vector对象上调用push_back()时,等于在vector对象内部创建了一个x的副本。

std::vector::clear()不删除项,而是调用任何适当的析构函数。实例的元素是指针,没有全局适用的析构函数。虽然您可能认为删除是一种适当的行为,但std::vector::clear()并不知道谁可能拥有这些指针。一般来说,如果分配了内存,就应该释放它。试着

for( std::vector<x*>::iterator it=instances.begin(); it!=instances.end(); ++it)
{
    delete *it;
}
instances.clear();