I tried to run my Bufferlist test, but I got a segmentation fault. I used valgrind and I see where the leaks are, but I don't understand what I'm doing wrong? It's saying it comes from my buffer full(), buffer enqueue, and bufferlist enqueue. I tried changing where pointers were pointing to, but it didn't really work. Any help would be appreciated.
BufferList::BufferList(int minBufCapacity){
if (minBufCapacity < 1)
{
Buffer aBuffer(DEFAULT_MIN_CAPACITY);
}
else
{
Buffer aBuffer(minBufCapacity);
}
// THESE ARE ALL JUST LOCAL VARIABLES TO THIS FUNCTION!!!
// YOU NEED TO REMOVE THE TYPES TO SET YOUR CLASS MEMBER VARIABLES.
Buffer * m_cursor = nullptr;
int m_listSize = 0;
int m_minBufCapacity = 0;
}
You should always compile with full warnings and pay attention to those warnings.
On g++ and clang++ you need to add the flags -Wall -Wextra -Wpedantic -Wshadow
I thought the loop itself was initializing it, since it's an array? So does that mean I have to initialize it by doing m_buffer = rhs.m_buffer ?
Not quite. First, if you have something like:
1 2
int* ptr;
ptr[0] = 42;
this is simply bad, because ptr is not pointing to any valid memory, which is what is happening here.
Have you learned what it means for something to be a "shallow copy" vs "deep copy"? https://www.learncpp.com/cpp-tutorial/shallow-vs-deep-copying/
m_buffer = rhs.m_buffer would be a "shallow" copy because you're just copying the pointer, but now you have two objects that are sharing one buffer of memory.
What you should be doing is something like: m_buffer = newint[rhs.m_capacity]; to initialize your array.