首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内存管理基础?

内存管理基础?
EN

Stack Overflow用户
提问于 2014-07-03 11:05:47
回答 5查看 175关注 0票数 1

使用此代码:

代码语言:javascript
复制
#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 key1\n");
        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 key2\n");
        getchar();
        getchar();
    }
    
    return 0;
}

我得到了这个输出:

代码语言:javascript
复制
wait for key2
wait for key1
x instance
x instance
x instance

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

代码语言:javascript
复制
wait for key2
wait for key1
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½

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

代码语言:javascript
复制
#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 key1\n");
        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 key2\n");
        getchar();
        getchar();
    }
    
    return 0;
}

我对内存管理不太清楚。为什么在清理之后我还能得到(非新的)“x实例”?举个例子?

我查看了link>>,我认为必须删除for循环中的x实例吗?

更新

下面是我为其他人(像我这样的初学者)实现的示例。我将对socket数据包使用同步队列,我不关心thread.join(),仅仅因为我的线程只是工作人员,而不是管理器!(多么真实的模拟!)

代码语言:javascript
复制
#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输出:

代码语言:javascript
复制
thread#thread#10
thread#0
thread#1
thread#1
thread#0
//Hit the enter!
thread#1
thread#0
thread#0 end.

thread#1 thread#1 //按回车!thread#1 thread#1 end.//按回车!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-07-03 11:11:21

当您使用vector添加push_back元素时,元素将被复制到向量中。这意味着向量有自己的元素副本,即使元素被删除(不管是由您还是在范围的末尾),向量仍然包含元素。当该向量被删除时,该向量负责逐个删除它的元素,所有这些都是由它自己完成的,所以您不必这样做。

here

在向量的末尾,在当前的最后一个元素之后添加一个新元素。val的内容被复制(或移动)到新元素。

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

票数 3
EN

Stack Overflow用户

发布于 2014-07-03 11:11:45

你在向量里放了一份。

代码语言:javascript
复制
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
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-03 11:12:18

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

对于未被删除的字符串,可能是因为您向指针分配了一个静态字符串。对动态创建的字符串也进行同样的尝试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24552048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档