According to the GC documentation this code snippet

char* p = new char[10];
char* q = p + 6; // ok
q = p + 11;      // error: undefined behavior
q = p - 1;       // error: undefined behavior

suggests that char *p is really a "fat pointer" with size information.

However, if get some memory allocated by some C library that is allocated with malloc we have no size information. We would get a char * without any size information and according to the documentation we can do anything including access out of bounds.

How does D internally know that a pointer was previously allocated by the GC or malloc?

If we would replace the GC with reference counting. How would D be able to distinguish a reference counted pointer from a raw pointer at compile time in order to insert the code associated with the reference counting?

This brings me back to MS managed C++ where they actually had two types of "pointers" a managed pointer and the normal C++ pointers. Like this:

MyType^ instance = gcnew MyType();

In this case it was obvious what is done with GC and what wasn't (past tense since managed C++ is deprecated). In this case it would be trivial to replace the GC algorithm with whatever you want since the compiler know the type at compile time.

Reply via email to