Dynamically creating std::vector // creating a pointer to a vector
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 anothe开发者_开发百科r 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.
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 using new
; 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
:
// inside the READBUFF struct (assuming you're using a normal variable, not a pointer)
READBUFF() { } // default constructor
READBUFF(int size) : buffer(size) { } // immediately sets buffer's size to the argument
and use an initialization list in Reader
's constructor:
// inside the Reader class
Reader() : readBuffer(8092) { }
Which will set the readBuffer.buffer
's size to 8092.
If you really want to use new
just for learning:
key.buffer = new std::vector<byte>(bufferSize);
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's NULL
.
as the default allocator creates a really large vector [4292060576]
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:
- You have to make sure to allocate it before anyone else uses it, where with a normal member variable it's done automatically before your object has a chance to use it.
- You have to remember to
delete
it in the destructor. - If you don't do the above 2 things, you have either a segfault or a memory leak.
I think you may be misinterpreting the result of calling max_size()
on a vector:
#include <vector>
#include <iostream>
int main() {
std::cout << std::vector<char>().max_size() << std::endl;
std::cout << std::vector<char>::size_type(~0) << std::endl;
}
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).
精彩评论