Custom vector AND memory allocator

OK, I THINK I have this functional now. Finally, now that I had a good few hours at once to sit down and work on it.

Latest commits can be found at: Vector
https://sourceforge.net/p/datastructs/code/ci/81a2cca24da0962af1230b09f5da47b5237c5be0/tree/

Memory Allocator
https://sourceforge.net/p/myalloc/code/ci/9bf3ce8e74fa001533cbd7f2c8017663eb8aaf65/tree/

Tell me what you think, especially tell me areas that are screwed up if you see them. And if you don't mind testing it that would be awesome. All around criticism again!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

#include "MyVector.h"

struct Dummy 
{
    static unsigned count ;
    static std::ostream& log ;

    void logIt(char const* s, unsigned c)
    { log << s << " - " << c << '\n' ; }

    explicit Dummy(int) { logIt("Dummy(int)", ++count); }

    Dummy() { logIt("Dummy()", ++count); }
    Dummy(const Dummy&) { logIt("Dummy(const Dummy&)\n", ++count); }
    Dummy( Dummy && dummy ) { logIt("Dummy(Dummy&&) ", ++count); }
    ~Dummy() { logIt("~Dummy()", --count) ; }

    Dummy& operator=(const Dummy&) { logIt("operator=(const Dummy&)\n", count); }
    Dummy& operator=(const Dummy&&) { logIt("operator=(const Dummy&&)\n", count); }
};

unsigned Dummy::count = 0 ;
std::ostream& Dummy::log = std::cout ;

int main()
{
    MyVector<Dummy> v ;
    //v[0] = Dummy() ;
}


With this code, I get the following unexpected output:

Dummy() - 1
Dummy() - 2
Dummy() - 3
Dummy() - 4
Dummy() - 5
Dummy() - 6
Dummy() - 7
Dummy() - 8
Dummy() - 9
Dummy() - 10
10 bytes allocated.
~Dummy() - 9
~Dummy() - 8
~Dummy() - 7
~Dummy() - 6
~Dummy() - 5
~Dummy() - 4
~Dummy() - 3
~Dummy() - 2
~Dummy() - 1
~Dummy() - 0
Buffer deallocated.


If I uncomment the other line in main I get compile errors:

1>myvector.h(151): error C3867: 'MyVector<T>::size': function call missing argument list; 
    use '&MyVector<T>::size' to create a pointer to member


where my guess was you meant to use either size() or size_.

You're also attempting to return NULL when the user attempts to access outside of bounds through operator[] and that obviously is not correct (and your comments there show you're aware of this.)
If I uncomment the other line in main I get compile errors:

Ah yup I meant size_, or size() really. Doesn't matter. I just missed that when I changed the naming I guess.

I have comments spread out about things I need to fix still, I was just working on a functional version first. I also still can't use this for types with no default constructor due to my allocate method, I'm working on that now. Wrong usage of new.

EDIT:
Should work with objects of no default ctor now. Check latest commit.

EDIT2:
It breaks when it needs to reallocate. Hmm
Last edited on
This code fails to compile
1
2
3
4
int main()
{
	std::vector<int, MemoryManager<int> > v = {1,2,3,4,5};
}
with this error
c:\mingw32\bin\../lib/gcc/i686-pc-mingw32/4.7.2/../../../../include/c++/4.7.2/bits/stl_vector.h:175:4: error: no 
matching function for call to 'std::_Vector_base<int, MemoryManager<int> >::_Vector_impl::deallocate(int*&, std::size_t&)'

You need the second argument in deallocate.
Last edited on
Noticed a couple things, line 48 in MemoryManager.h T* buffer = static_cast<T*>(::operator new(sizeof(T) * 5)); the 5 should be n.

And in deallocate you need to use operator delete
1
2
3
4
5
6
template <typename T>
void MemoryManager<T>::deallocate(T* const buffer, size_t&) const
{
    ::operator delete(buffer); 
    std::cout << "Buffer deallocated.\n";
}
I actually haven't been writing this as a replacement to std::vector, so I'm surprised it's even that close to being able to.

I'd assume that 5 was a typo. Changing that actually just made it functional. I have a test class being pushed into it 100000 time and it works pretty well. Takes a little bit of time though. It's actually pretty quick, nevermind. Had a bunch of debugging statements thrown in there slowing it down.

Thanks on the ::operator delete(). I actually wasn't sure what to do there, as my comment says.

Why do I need the second argument in deallocate()? What is it used for?
Last edited on
Topic archived. No new replies allowed.