Sign in to follow this  
Basiror

Your own malloc and industry c++ coding standards

Recommended Posts

Hi as the headline says I have 2 questions 1. On Unix systems you can use sbrk to get some memory on the heap and you can overwrite the malloc functions with your own malloc how can i do this on windows? Is there a function that gives me access to unused memory on the heap so i can write my own malloc for windows too? and how do they implement the new and delete operators in C++ I know you can easily overload them but how do they call the constructor of a class? 2. Are there some coding standards in the industry for C++? Like ANSI C ?

Share this post


Link to post
Share on other sites
HeapAlloc and friends is a good staring point.

And are you talking 'bout globan new and delete or class specific new and delete? neither calls the constructors directly though.

but if you really want to do that you can use placement new.

Share this post


Link to post
Share on other sites
1.) Look up HeapAlloc in MSDN (it takes a few parameters), but it will allocate you a raw block of memory (and you then use HeapFree to remove it).

I'm not 100% sure how the operators are implemented under the hood (IE, how new itself is implemented). But, implementations usually use malloc to do the actual memory allocation. And, new and delete always call the constructor/destructor of a class, you don't need to do anything specific, it happens automatically.

2.) Yes, there are 'theoretical' C++ coding standards, but I'm not sure how widely used they are. See the book C++ Coding Standards by Herb Sutter and Andrei Alexandrescu for a large set of very good coding standards.

Share this post


Link to post
Share on other sites
If you really want raw memory to do with what you will then you probably want VirtualAlloc rather than HeapAlloc. Either will work though.

Hell, you can also just malloc a really big block of memory and suballocate it with your private allocator. This has the advantage of being 100% portable.

Share this post


Link to post
Share on other sites
thx your answers helped my a lot

i think i ll create a little MM system for my engine just for testing purpose

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
I know you can easily overload them but how do they call the constructor of a class?


When you invoke new/delete normally the constructor/destructor is invoked automataically, when redefining or overloading operator new/delete you just deal with uninitialized memory you don't need to worry about construction/destruction.

If you explicitly invoke new/delete they will only allocate/de-allocate uninitialized memory, if you need to invoke the constructor later on you would use the placement new operator its sole purpose is to invoke the construtor. Afterwoods for user-defined types only you must explicitly invoke destructor yourself when using placement new :


#include <cstddef> // size_t
#include <algorithm> // copy, for_each
#include <iterator> // ostream_iterator
#include <new> // global operators new/delete, placement new
#include <memory> // uninitialized_fill
#include <iostream> // cout, basic_ostream

struct foo {

int j;

foo(int i): j(i) {}

};

template < typename CharT, typename Traits >
inline std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& out, const foo& f) {
return out << f.j;
}

int main() {

const std::size_t num_elems = 30;

foo* foos = static_cast<foo*>(::operator new(sizeof(foo) * num_elems)); // allocate only

std::uninitialized_fill(foos, foos + num_elems, foo(40)); // construct elements only, uses placement new

std::copy(foos, foos + num_elems, std::ostream_iterator<foo>(std::cout, "\n")); // print out

struct foo_destructor {
void operator()(const foo& f) const {
f.~foo();
}
};

std::for_each(foos, foos + num_elems, foo_destructor()); // destruct all elements only

::operator delete(foos); // de-allocates memory only

}


The c++ standard library comes with some generic algorithms that work with uninitialized memory, they are in the header memory.

I think its not worth taking complete control over memory management on modern C++ compilers as they should already be optimal for large allocations, its only small allocations you want to optimize.

The loki library has a small object allocator you can use. It is also worth using pooled allocators in some places aswell you do not need to take over memory management for that and libraries such boost already come with good pooled allocators even ones that can be used with the standard library containers.

Share this post


Link to post
Share on other sites
i need to read some resources about this topic before i understand everything you posted

but thx for the elaboration

P.S.: I am not really planing to write my own memory management

I just try to advance my knowledge base a little bit maybe you need it somewhen, its better to know about it before you get into the situation where you need the knowledge

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this