I'm receiving a run-time error when I try to memset my AudioBuffers on line 16. I know it's something small, and I know it's because I can't think straight. I'm also aware that there are other ways of accomplishing the same thing (using classes for AudioBuffers or using std::fill() instead of memset()), I guess that makes this a purely theoretical question.
resize will actually change the size of the vector.
You also are memsetting the vector and not the elements of the vector. You probably meant to do this:
memset(&buffers[i][0], ... // <-note the [0]
But you shouldn't use memset anyway because it's not typesafe (case in point: this problem you're having). std::fill is a better alternative. (as you already mentioned)
But even that is unnecessary because vector will 0 newly added elements.
I am a bit wary of nested vectors, they are horribly inefficient when they need to be resized, consider some of the other containers, like a std::list of std::array say - given the size of the buffer is constant.
It's a design decision that depends on whether you need to sort or use find or insert for example. If things need to be sorted, std::set might be better, as it stores items sorted by default. std::map is also good like std::set when there are lots of items, because they use AVL trees internally.
Thanks for the quick response. I didn't know that a vector will initialize its elements to zero upon resizing. However, let's pretend I'm a curmudgeonly old man and I insist on using memset().
I did what you suggested and added the [0]. memset(&buffers[i][0], ...
Now, I'm getting a vector subscript out of range run-time error.
Any ideas?
*EDIT* after reading my own posts it does sound a lot like I'm asking questions and then not regarding the advice I receive, which I assure you is not the impression I want to give off. I'm very thankful to anyone who responds.
Actually, since initializing the buffers really isn't an issue, perhaps this is a better example of the problem using the same typedefs as before:
1 2 3 4 5 6 7 8 9 10
void WaveFile::write(constchar* filename) {
std::ofstream stream(filename, std::ios::binary);
//assuming that a wave file has two buffers (weird, I know), and my buffers have data in them, but I only want to write the first buffer
//write .wav RIFF header
stream.write((constchar*)(&buffers[0]/* [0] */), bufferSize);//run-time error!
stream.close();
}
¿have you changed the `reserve()' to `resize()'?
`buffers[i][0]' is out of bounds because `buffers[i]' is empty.
Edit:
The vector, and the data stored by the vector are not contiguous in memory.
You've got a pointer to where the elements are allocated (in the heap)
So stream.write((constchar*)(&buffers[0]), bufferSize); is trying to write a vector (say 3 pointers), not its elements.