0

I'm trying to have a better understanding of memory allocation in c++. There are many concepts and theories on webs but I can't find the source code of what's really in c++ global operator new.
I understand to simply overload operator new and delete I can just do this:

void* operator new(size_t size){
        void* allocated = malloc(size); //allocate necessary space;
        return allocated;
}

void operator delete(void* ptr){
        SA<T>* p = static_cast< SA<T>* >(ptr);
        return free(p);
}

But when I just saw some code overloaded operator new without using the new and delete keyword, I didn't know what was going on. The code I read is this:

#include <stddef.h>
struct Head { 

long length; 

struct Head *next; 

) ;
static Head pool = { 0, 0 };
static Head *h = (Head *)HEAP_BASE ADDRESS;

/* fast dumb first-fit allocator */
typedef char *Char_p;
// NEXT LINE IS MACHINE AND COMPILER DEPENDENT:
const long WRDSIZE = sizeof (void*) ;

void* operator new(size_t nbytes)
{
    /* First, look in free list */
    if (pool.next){
        Head *prev &pool;
        for (Head *cur = &pool; cur; cur = cur->next) {
            if (cur->length >= nbytes) {
                prev->next - cur->next;
                return (void*) (Char_p(cur) + sizeof(Head));
            }else prev = cur;
        }
     }
/* Nothing on free list, get new block from heap */
    h = (Head*) (((int) ((Char_p(h)+WRDSIZE-1))WRDSIZE)*WRDSIZE);
    h->next = 0;
    h->length = nbytes;
    h = Char_p(h) + nbytes + sizeof(Head);
    return (void*) (Char_p(h) - nbytes); 
}

Not a single new or malloc was called. How can this allocate memory??

L. Dai
  • 229
  • 2
  • 13
  • 2
    The source code will either call other libraries or be extremely complicated. It's very unlikely to help you. – SLaks Jun 26 '17 at 18:01
  • It could very well be `malloc`. – NathanOliver Jun 26 '17 at 18:04
  • 4
    It's any compiler's choice how to implement that. There won't be any common source code. – πάντα ῥεῖ Jun 26 '17 at 18:04
  • so is it possible to overload operator new without using keyword new or malloc? – L. Dai Jun 26 '17 at 18:06
  • ***so is it possible to overload operator new without using keyword new or malloc?*** Yes but not in a standard way. I mean you can directly call OS routines to allocate memory for your process and replace the functionality of new or malloc. – drescherjm Jun 26 '17 at 18:09
  • 1
    If what you are looking to do is replace the global operator `new` and `delete`, see https://stackoverflow.com/q/8186018/1896169 – Justin Jun 26 '17 at 18:11
  • Thanks. I know how to overload new and delete. But I when I read some overloaded operator new and I didn't see any new and malloc in it, it confused me which made me want to read what's in the actual new. – L. Dai Jun 26 '17 at 18:15
  • Maybe you should start a new question about the implementation that you have. Perhaps someone can explain that code. – drescherjm Jun 26 '17 at 18:18
  • I think there are a few typos in the current code. Not sure what `Char-F(h)` is? – drescherjm Jun 26 '17 at 18:49
  • thanks. just manually changed everything. It was originally from a scanned file. – L. Dai Jun 26 '17 at 19:04
  • The code appears to assume the heap can just grow from an address and that there is no virtual memory in use above that address. – drescherjm Jun 26 '17 at 20:26
  • Yes, it starts "allocating" memory in the beginning of heap. Does it imply that we can in fact just start writing on memories as long as we have a pointer to a specific space? Doesn't matter whether it's allocated or not? – L. Dai Jun 26 '17 at 21:39
  • ***Does it imply that we can in fact just start writing on memories as long as we have a pointer to a specific space?*** This would be very platform specific. I mean I would expect this version of new to not work on windows since the address space tends to be fragmented with executable code / dlls... Also I expect the compiler has added some type of handler for page faults so that the heap can be allowed to grow. In this code there is however no attempt to handle the case where the heap is full. To me this seems like code that can work on an embedded platform. – drescherjm Jun 27 '17 at 16:08

0 Answers0