I'm trying to replace the global new operator in Visual Studio and C++. This is my code (only one new operator shown for simplicity):
void* operator new(size_t _Size)
{
// Do something
}
It works fine, however Visual Studio is giving me a warning when running Code-Analysis for my project:
warning C28251: Inconsistent annotation for 'new': this instance has no annotations. The first user-provided annotation on this built-in function is at line vcruntime_new.h(48).
Using the annotations from vcruntime_new.h for my operator new
replacement, as suggested in the warning from IntelliSense, resolves the warning:
_NODISCARD _Ret_notnull_ _Post_writable_byte_size_(_Size) _VCRT_ALLOCATOR
void* __cdecl operator new(size_t _Size)
{
// Do something
}
- Is it safe to use the annotations inside vcruntime_new.h for my own replacement code like shown above?
- What are the consequences of this change?
- Are there special use cases were the "new operator" cannot be used anymore like before because of the annotations?
- And why is that change necessary?
EDIT:
- Am I correct, that the annotations won't change anything in the resulting binary, and are simply for static code analysis? (Except
__cdecl
, which changes the assembler, but it should be standard anyway I guess?)