gpt4 book ai didi

c++ - 指针复制到范围外c++

转载 作者:行者123 更新时间:2023-11-30 05:34:44 26 4
gpt4 key购买 nike

今天我回去调查了我在旧项目中遇到的一个错误。这不完全是一个错误,而是我不知道该怎么做。真的不想深入了解项目的细节,因为它陈旧、漏洞百出、效率低下,更重要的是,它无关紧要。所以我编写了一个新的示例代码:

#include <iostream>
#include <vector>
#include <time.h>
#include <random>
#include <string>

class myDoc;

class myElement
{
int myInt;
std::string myString;
myElement * nextElement;
//a pointer to the element that comes immediately after this one
public:
myElement(int x, std::string y) : myInt(x), myString(y){};
friend myDoc;
};//an element type

class myDoc
{
std::vector<myElement> elements;
public:
void load();
~myDoc()
{
//I believe i should delete the dynamic objects here.
}
};// a document class that has bunch of myElement class type objects as members

void myDoc::load()
{
srand(time(0));
myElement * curElement;
for (int i = 0; i < 20; i++)
{
int randInt = rand() % 100;
std::string textInt = std::to_string(randInt);
curElement = new myElement(randInt,textInt);
//create a new element with a random int and its string form

if (i!=0)
{
elements[i-1].nextElement = curElement;
//assign the pointer to the new element to nextElement for the previous element
//!!!!!!!!!!!! this is the part that where i try to create a copy of the pointer
//that goes out of scope, but they get destroyed as soon as the stack goes out of scope
}

elements.push_back(*curElement);// this works completely fine
}
}

int main()
{
myDoc newDoc;
newDoc.load();
// here in newDoc, non of the elements will have a valid pointer as their nextElement
return 0;
}

基本纲要:我们有一个文档类型,它由我们定义的元素类型 vector 组成。在本例中,我们将 20 个随机动态分配的新元素加载到文档中。我的疑问/问题:

  1. void myElement::load() 函数结束时,指针和/或其拷贝超出范围并被删除。我如何保留一个拷贝(不是完全静态的,是吗?)至少直到它指向的对象被删除?
  2. elements vector 中的对象,它们是原始动态分配的对象还是只是一个拷贝?
  3. 我用new分配内存,我应该如何/何时删除它们?

这是我画的一张图来解释第一个问题(具体例子不是很准确,但问题是一样的),谢谢你的时间。 Here is a picture i made to visualize my main problem.

最佳答案

注意:我假设您想要一个 myElement 对象的 vector ,其中每个对象都指向它旁边的元素。不清楚您是否希望 elements 中的对象指向它们的拷贝,无论如何修改代码以实现后者应该很容易

这是您的代码中发生的情况:

void myDoc::load()
{
..
curElement = new myElement(n,m); // Create a new element on the heap

...
// If this is not the first element we inserted, have the pointer for the
// previous element point to the heap element
elements[i-1].nextElement = curElement;

// Insert a COPY of the heap element (not the one you stored the pointer to)
// into the vector (those are new heap elements copied from curElement)
elements.push_back(*curElement);// this works completely fine
}

所以当 myDoc::load() 超出范围时没有任何内容被删除,但是你有内存泄漏错误因为指针不是指向 elements vector 中的元素,而是指向您分配的第一个堆元素中的元素。

这也回答了您的第二个问题:它们是拷贝。

为了自动释放你的内存,没有泄漏并指向正确的元素,你可以做类似的事情

class myElement
{
int a;
std::string b;
myElement *nextElement = nullptr;
//a pointer to the element that comes immediately after this one
public:
myElement(int x, std::string y) : a(x), b(y){};
friend myDoc;
};//an element type

class myDoc
{
std::vector<std::unique_ptr<myElement>> elements;
public:
void load();
~myDoc()
{}
};// a document class that has bunch of myElement class type objects as members

void myDoc::load()
{
srand((unsigned int)time(0));
for (int i = 0; i < 20; i++)
{
int n = rand() % 100;
std::string m = std::to_string(n);
//create a new element with a random int and its string form
elements.emplace_back(std::make_unique<myElement>(n, m));

if (i != 0)
{
//assign the pointer to the new element to nextElement for the previous element
elements[i - 1]->nextElement = elements[i].get();
}
}
}

Live Example

无需删除析构函数中的任何内容,因为当 myDoc 元素超出范围时,智能指针将自动销毁(并释放内存)。我相信这可能是您想要做的,因为这些元素无论如何都属于 myDoc 类。

关于c++ - 指针复制到范围外c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com