473,323 Members | 1,570 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,323 software developers and data experts.

Effective C++ - item 7 (memory mgt).

Hi,

i have a little question concerning Scott Meyer's Effective C++ (2nd
edition), Item 7 "Be prepared for out-of-memory conditions". It's
about his example for a class specific new_handler() management. Please
note that i don't want to start a discussion about the
purpose/usability of the approach. But i just want to understand some
technical issue of the example.
Here's the question:
Why does he define the member function
static void *operator new(size_t size);
as a *static* function for the class?
I can understand, that the
static new_handler set_new_handler(new_handler p);
Is defined as static. Because the user needs to invoke it for the class
itself , before creating an instance of the class (
X::set_new_handler(noMoreMemory) ).
But why using static for operator new()? It is not invoked on the user
level. Is it because the compiler needs to use it before there actually
is an instance of the object? (so the same reason, but this time the
compiler instead of the programmer?).
Can anyone confirm of tell me the right answer for this?

Thanks in advance!

Frank Bergemann

Aug 7 '05 #1
1 1604

<FB********@web.de> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
Hi,

i have a little question concerning Scott Meyer's Effective C++ (2nd
edition), Item 7 "Be prepared for out-of-memory conditions". It's
about his example for a class specific new_handler() management. Please
note that i don't want to start a discussion about the
purpose/usability of the approach. But i just want to understand some
technical issue of the example.
Here's the question:
Why does he define the member function
static void *operator new(size_t size);
as a *static* function for the class?
I can understand, that the
static new_handler set_new_handler(new_handler p);
Is defined as static. Because the user needs to invoke it for the class
itself , before creating an instance of the class (
X::set_new_handler(noMoreMemory) ).
But why using static for operator new()? It is not invoked on the user
level. Is it because the compiler needs to use it before there actually
is an instance of the object? (so the same reason, but this time the
compiler instead of the programmer?).
Can anyone confirm of tell me the right answer for this?

Thanks in advance!

Frank Bergemann


I guess even if you don't write the 'static' the compiler will still treat
it as static so why not making it more explicit? The only difference between
static and non-static member functions is that a non-static one take an
addtional, hidden parameter the 'this pointer'. Using 'this pointer' simply
doesn't make sense in operator new()

But, I am just guessing, I might be wrong. In that case please correct me.

Ben
Aug 7 '05 #2

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

Similar topics

4
by: John | last post by:
I am looking for a good Lock class implementation that has barely been implemented in Effective STL of Myers (Item 12). Any pointers? Is there an implementation that is good for both OpenMP and...
13
by: tuvok | last post by:
I have an Item object which allocates memory in its ctor and frees it in its dtor. I want to add such items to a vector. But the program crashes. Why? How do I fix it? #include <vector> ...
3
by: smileydip | last post by:
I have a performance and memory management issues with SQL SERVER. I feel SQL server does not releases the unused memory back to the OS. I have been monitoring that at the end of the day the SQL...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
2
by: Piotr | last post by:
I am reading Effective STL item 7 My understanding is, instead of doing this: tempate <typename T> struct DeleteObject: public unary_function<const T*, void> { void operator() (const T* ptr)...
3
by: creator_bob | last post by:
I decided to post here after a little incident where I got an effective memory leak that was immune from garbage collection. I created a simple binary tree, no balancing or deleting. Only two...
1
by: ankushmn | last post by:
i am working on web application in Asp.Net, C# in which we are using Outproc session mgt. As we manage session using Outproc there are two ways 1) Sql Server 2) State Server in above we are...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.