c++


overload operators new/new[] delete/delete[] globally C++


I have this sample code for overloading operator new and delete
#include <iostream>
#include <cstddef>
#include <new>
#ifdef USE_ZMALLOC
extern "C" {
#include "zmalloc.h"
}
#define m_malloc zmalloc
#define m_free zfree
#else
#ifdef USE_JEMALLOC
#include <jemalloc/jemalloc.h>
#define m_malloc je_malloc
#define m_free je_free
#else
#include "malloc.h"
#define m_malloc std::malloc
#define m_free std::free
#endif
#endif
// C++ requires that operator new return a legitimate pointer
// even when zero bytes are requested. That's why if (size == 0) size = 1
void* operator new (std::size_t size) throw (std::bad_alloc) {
using namespace std;
if (size == 0) { // Handle 0-byte requests by treating them as 1-byte requests
size = 1;
}
while (true) {
void* mem = m_malloc(size);
if (mem != nullptr) {
return mem;
}
new_handler global_handler = set_new_handler(nullptr);
set_new_handler(global_handler);
if (global_handler) {
(*global_handler)();
} else {
throw bad_alloc();
}
}
}
void* operator new[] (std::size_t size) throw (std::bad_alloc) {
return operator new(size);
}
void operator delete (void* ptr) throw() {
if (ptr == nullptr) {
return;
}
m_free(ptr);
}
void operator delete[] (void* ptr) throw() {
operator delete(ptr);
}
This code is actually working and everything seems to be correct.
My question is: when using valgrind, if I do something like this:
int main() {
Foo** foo = new Foo*[10];
std::cout << "# " << zmalloc_used_memory() << "." << std::endl;
delete foo; // Using wrong delete operator
return 0;
}
valgrind doesn't complain about using the wrong operator delete.
If I use the default operator new/delete pair, valgrind does warn me about the operator delete error.
1) Is there something wrong with my new/delete operators?
2) Is it normal that valgrind is not warning this kind of errors anymore?
Thx!

Related Links

C++ - Pretty way to make frequencies add up to 1
integerset obtaining the given array
Arduino Invalid conversion from 'char' to 'char*'
virtual ClistCtrl with checkboxes on displayed report list style
Error when modifying vector member of another class
Interrupt Service Routine can't be called directly
Lambda capture by default reference vs named
convert bitset<64> to string every 8 bit
argument of type “int” incompatible with parameter of type “int”
When is the correct time to call restoreState for QHeaderView?
How does it pass lvalue reference to std::forward with CRTP?
Using lower_bound function in loop is giving runtime error?
Problems with rand(), always taking the same random generated number [duplicate]
Reading Credentials file fails with error 32
The mechanism of shared_ptr
C++ specialized function template

Categories

HOME
.htaccess
google-maps
momentjs
highcharts
rx-java
veins
autodesk-forge
attributes
amazon-redshift
google-places-api
mapkit
acr122
http-headers
spring-transactions
shoutem
sourceforge
fontforge
ninja
restcomm
shogun
jlabel
conv-neural-network
filenet-p8
spring-data-elasticsearch
systemml
licode
beatbox
bind
firebase-cloud-messaging
react-apollo
touch
google-cloud-vision
python-datetime
google-surveys
chinese-locale
polymer-starter-kit
jquery-ui-autocomplete
hashcode
direct3d9
outlook-web-app
regedit
pivottable.js
notnull
google-truth
evosuite
rt
run-configuration
behavior
fluentbootstrap
shared-ptr
wso2dss
dispy
c#-to-vb.net
cscore
strstream
markov-models
arithmetic-expressions
spawn-fcgi
ntfs-3g
couchbase-sync-gateway
angular-component-router
pdl
android-context
msbuildcommunitytasks
ti-nspire
xlet
setlocale
sapb1
centura
httplib
ember-qunit
tracker
string-conversion
topcoat
harfbuzz
wsimport
happstack
nexus-7
system-design
expandoobject
pasteboard
equivalent
zend-navigation
loose-coupling
asp.net-mvc-2-validation
application-design
isaserver
maintaining-code
pia

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile