Is it possible to place a new surviving optimization, unlike memset?

So, I'm really intrigued by whether he can survive the aggressive optimization tactics used by GCC and clang.

Given the following example:

void* clean(void* pointer, std::size_t size) noexcept { return new(pointer) char[size]{}; } void doStuff() { //... clean(pointer, size); //... } 

Can I trust him with the task of cleaning confidential data?

+7
c ++ optimization memory
source share
2 answers

I do not think that optimization can play any tricks in you. The standard mandates to initialize the value in this case are: new(pointer) char[size]{} , therefore, after this memory of the call pointed to by pointer , 0 will be filled.

Maybe the compiler can optimize it if you never access the new pointer or redefine it before access (based on observability). If you want to avoid this small possibility, you need to define a pointer as a pointer to volatile .

+2
source share

I'm not sure if this is the answer to your question or just a note, but you can turn off optimizations for this particular function using the optimize() directive

 void* __attribute__((optimize("O0"))) clean(void* pointer, std::size_t size) { // unmodifiable compiler code } 

This ensures that your clean () function is not optimized.

+2
source share

All Articles