C++: Difference between revisions

380 bytes removed ,  10 years ago
Remove wrong advise that g++ doesn't support -ffreestanding and fix size_t declaration.
[unchecked revision][unchecked revision]
m (There is no longer a special C++ Bare Bones, this has been merged into the standard Bare Bones article for maintainability. The constructor advise is not in that article, either.)
(Remove wrong advise that g++ doesn't support -ffreestanding and fix size_t declaration.)
Line 403:
== The Operators 'new' and 'delete' ==
Before you can properly use '''new''' and '''delete''', you have to implement some sort of memory management. You also have to implement both operators (including their array counterparts). '''new''' and '''delete''' respectively allocate and delete memory (much like '''malloc''' and '''free''' in C). Take a look at the [[Memory Management]] article if you would like to know more about this subject.
 
GCC provides several standard library functions as built-in, which you most likely do not want in your kernel binary either. Disable them by adding '''-nostdlib''' to '''g++'''. Note that the option '''-ffreestanding''' (usually recommended in kernel tutorials) cannot be used with '''g++'''.
 
Every time you call one of the operators '''new''', '''new[]''', '''delete''', or '''delete[]''', the compiler inserts a call to them. The most simple implementation would be to map them to your kernel's '''malloc''' and '''free'''. For example:
 
<source lang="cpp">
#include <stddef.h>
// size_t depends on your implementation, the easiest would probably be:
// typedef __SIZE_TYPE__ size_t;
 
void *operator new(size_t size)
Anonymous user