473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

overriding global new

I am trying to override global new.

Yes, I know this is not a good idea, but I want to do it anyway. Notice
that I am not trying to override new in a class.

Any suggestions? I thought the below would work but it just goes into a
recursive call (infinite loop)

Is this not possible? ummm

Many thanks to any suggestions.

#include <iostream>

class car {

public:

car()

{

std::cout << "car " << std::endl;

}

int type;

};

int main()

{

car my_car;

car* p_my_car = new car();

return 0;

}

void* operator new( size_t my_size)

{

std::cout << "now grabbing some memory" << std::endl;

return ::operator new( my_size );

}
Jul 23 '05 #1
1 1442
john smith wrote:
I am trying to override global new.

Yes, I know this is not a good idea, but I want to do it anyway. Notice
that I am not trying to override new in a class.

Any suggestions? I thought the below would work but it just goes into a
recursive call (infinite loop)

Is this not possible? ummm

[...]
void* operator new( size_t my_size)

{

std::cout << "now grabbing some memory" << std::endl;

return ::operator new( my_size );

}


Your global operator new is calling itself. I guess you meant to call the
'default' operator new instead? I don't think you can do that, but instead
you can use malloc. Also, you need to be *very* careful what you call
inside operator new. Any kind of C++-style IO or string handling is likely
to do some internal memory management, which may in turn .... call operator
new! Indeed, that is quite likely with your std::cout << "now grabbing
some memory" << std::endl; line. You can do C-style IO though. Try:

#include <cstdio>

void* operator new(size_t size)
{
std::printf("now grabbing some memory\n");
return malloc(size);
}

HTH,
Ian McCulloch

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Corey Lubin | last post by:
someGlobal=1 class Original: def foo(self): # Make use of someGlobal from original import * someGlobal=2
1
by: Jesper Madsen | last post by:
Can I confine overrides of new and delete to certain namesspaces, f.ex. by just introducing the modfied operator new in namespace GUI?? Or should I ask, if I declare operator new in a namespace, is...
6
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
1
by: Anders Borum | last post by:
Hello! I have a framework where all objects are uniquely identified by a GUID (Global Unique Identifier). The objects are used in conjunction with lots of hashtables and I was thinking, that...
6
by: rlvladbob | last post by:
Hi, I've try to access a static method using an instance instead of a class. public class test{ public static void ShowAText(string ThisText) { System.Console.WriteLine("->{0}",ThisText); }
7
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std;...
4
by: Raja Raman Sundararajan | last post by:
Hello guys, I have data stored in the database which has special characters like <, etc. Case 1: Whenever I wanted to present the output to a browser I need to escape these special characters...
4
by: =?Utf-8?B?S2V2aW4=?= | last post by:
I have an HttpHandler installed on a server in the GAC that I want to grant public access to across all sites on the server. The problem I have is that some sites use forms authentication. For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.