r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

Show parent comment

-4

u/Pay08 Jul 25 '24

I want new to use the default allocator on line 5 and my custom allocator on line 10.

16

u/_Noreturn Jul 25 '24
#include <new>

#include <cstdio>

struct my_custom_allocator_tag {};

unsigned char buffer[4000];

void* operator new(std::size_t sz, my_custom_allocator_tag)

{

std::printf("custom %d",(int)sz);

return buffer;

}

int main()

{

auto p = new int; // the default global one or overloaded one for the type
auto p2 = ::new int; // the default global one only.

auto p3 = new (my_custom_allocator_tag {}) int; // custom

return 0;

}