动态创建 std::vector // 创建指向向量的指针
我是新人,所以我很可能错过了一些关键的东西。
我正在使用 std::vector 来存储 ReadFile 操作中的数据。
目前我有一个名为 READBUFF 的结构,其中包含一个字节向量。然后,READBUFF 通过名为 Reader 的类中的私有类型进行实例化。
class Reader{
public:
void Read();
typedef struct {
std::vector<byte> buffer;
} READBUFF;
private:
READBUFF readBuffer;
}
在 Read() 中,我目前将数组大小调整为我想要的大小,因为默认分配器创建了一个非常大的向量 [4292060576]
void Reader::Read()
{
readBuffer.buffer.resize(8192);
}
这一切都工作正常,但后来我开始思考我宁愿动态地内联新向量,这样我就可以控制分配指针的管理。我将缓冲区更改为:std::vector* 缓冲区。当我尝试执行以下操作时,缓冲区未设置为新缓冲区。从调试器可以清楚地看出它没有初始化。
void Reader::Read()
{
key.buffer = new std::vector<byte>(bufferSize);
}
然后我尝试了,但这与上面的行为相同。
void Reader::Read()
{
std::vector<byte> *pvector = new std::vector<byte>(8192);
key.buffer = pvector;
}
主要的第一个问题是为什么这不起作用?为什么我不能将缓冲区指针分配给有效指针?另外,如何定义内联分配的大小与必须调整大小?
我的最终目标是“新建”缓冲区,然后将它们存储在双端队列中。现在我这样做是为了重用上面的缓冲区,但实际上我是将缓冲区复制到另一个新缓冲区中,而我想要的只是存储指向创建的原始缓冲区的指针。
std::vector<byte> newBuffer(pKey->buffer);
pKey->ptrFileReader->getBuffer()->Enqueue(newBuffer);
提前致谢。当我发布这篇文章时,我意识到我错过了一些基本的东西,但我不知所措。
I am new so I more than likely missing something key.
I am using std::vector to store data from a ReadFile operation.
Currently I have a structure called READBUFF that contains a vector of byte. READBUFF is then instantiated via a private type in a class called Reader.
class Reader{
public:
void Read();
typedef struct {
std::vector<byte> buffer;
} READBUFF;
private:
READBUFF readBuffer;
}
Within Read() I currently resize the array to my desired size as the default allocator creates a really large vector [4292060576]
void Reader::Read()
{
readBuffer.buffer.resize(8192);
}
This all works fine, but then I got to thinking I'd rather dynamically NEW the vector inline so I control the allocation management of the pointer. I changed buffer to be: std::vector* buffer. When I try to do the following buffer is not set to a new buffer. It's clear from the debugger that it is not initialized.
void Reader::Read()
{
key.buffer = new std::vector<byte>(bufferSize);
}
So then I tried, but this behaves the same as above.
void Reader::Read()
{
std::vector<byte> *pvector = new std::vector<byte>(8192);
key.buffer = pvector;
}
Main first question is why doesn't this work? Why can't I assign the buffer pointer to valid pointer? Also how do I define the size of the inline allocation vs. having to resize?
My ultimate goal is to "new up" buffers and then store them in a deque. Right now I am doing this to reuse the above buffer, but I am in essence copying the buffer into another new buffer when all I want is to store a pointer to the original buffer that was created.
std::vector<byte> newBuffer(pKey->buffer);
pKey->ptrFileReader->getBuffer()->Enqueue(newBuffer);
Thanks in advance. I realize as I post this that I missing something fundamental but I am at a loss.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,您不应该使用
new
。它导致您必须手动管理内存,但出于多种原因永远您都不应该这样做1。您说您想通过使用 new 来管理向量的生命周期;实际上,向量的生命周期已经被管理,因为它与保存它的对象相同。因此该向量的生命周期就是 Reader 类实例的生命周期。要在构造向量之前设置向量的大小,您必须为 READBUFF 创建一个构造函数:
并在 Reader 的构造函数中使用初始化列表:
这将将
readBuffer.buffer
的大小设置为 8092。如果您确实想使用
new
只是为了学习:这会很好用,但是您不应该在
Read
函数,您应该在对象的构造函数中执行它。这样任何成员函数都可以使用它,而不必检查它是否为 NULL。不,它不会(如果确实如此,您的整个计算机上可能只有一个向量,并且您的计算机可能会崩溃)。当您添加内容并超出容量时,它会逐渐调整存储大小。像您所做的那样使用
resize
仍然很好,因为不是分配一个较小的,填充它,分配一个较大的并复制所有内容,填充它,分配一个较大的并复制所有内容,等等,您只需分配一次所需的大小,这要快得多。1 一些原因是:
删除
它。You shouldn't be using
new
in this case. It causes you to have to manage the memory manually, which is never something you should want to do for many reasons1. You said you want to manage the lifetime of the vector by usingnew
; in reality, the lifetime of the vector is already managed because it's the same as the object that holds it. So the lifetime of that vector is the lifetime of the instance of your Reader class.To set the size of the vector before it gets constructed, you'll have to make a constructor for
READBUFF
:and use an initialization list in
Reader
's constructor:Which will set the
readBuffer.buffer
's size to 8092.If you really want to use
new
just for learning:This will work fine, but you shouldn't be doing it in the
Read
function, you should be doing it in the object's constructor. That way any member function can use it without having to check if it'sNULL
.No, it doesn't (if it did, you could have one vector on your entire computer and probably your computer would crash). It incrementally resizes the storage up when you add things and exceed the capacity. Using
resize
like you are doing is still good though, because instead of allocating a small one, filling it, allocating a bigger one and copying everything over, filling it, allocating a bigger one and copying everything over, etc. you are just allocating the size you need once, which is much faster.1 Some reasons are:
delete
it in the destructor.我认为您可能会误解在向量上调用
max_size()
的结果:该程序打印向量的最大可能大小,而不是当前大小。另一方面,
size()
确实打印当前大小(忽略任何已保留的内容)。I think you may be misinterpreting the result of calling
max_size()
on a vector:This program prints the maximum possible size of the vector, not the current size.
size()
on the other hand does print the current size (ignoring anything that's been reserved).