Delete operator overloading with multiple arguments.


 
Thread Tools Search this Thread
Top Forums Programming Delete operator overloading with multiple arguments.
# 1  
Old 01-15-2010
Error Delete operator overloading with multiple arguments.

Hi,

I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and deallocator like a pool, which makes me to overload the delete operator.
Small example of the problem is

Code:
#include <new>
#include <iostream>
using namespace std;
void operator delete(void* p)
{
cout << "Hello" << endl;
free(p);
} // (1)
void operator delete(void* p, size_t s)
{
cout << "World" << endl;
free(p);
} // (2)
int main()
{
int* p = new int;
delete p; // This cannot render to show 'Hello' or 'World'
}

Output
Hello

I want to call the delete with size operator. So please help me how can this program will give the output as World.

Thanks in advance for my help.

Last edited by pludi; 01-15-2010 at 04:10 AM.. Reason: code tags, please...
# 2  
Old 01-15-2010
I think this is a compiler specific question. Can you also post what OS and compiler you are using? I tried this code as is and it failed to compile, because it was missing stdlib (required for free). I added that, and then I got "Hello". My compiler and OS is
$> c++ --version
c++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C++ operator overloading error

hi, I defined my own String class and overloaded the output operator with friend ostream& operator<<(ostream& os, const myString& str); //overloads the << operator so it can be used to output values of type myString which works fine. Until I try to execute the following statement: // +... (6 Replies)
Discussion started by: milhan
6 Replies

2. Shell Programming and Scripting

Passing multiple arguments

Hi, I know with getopts you can pass arguments from the command line ./script -ab -c apple But it doesn't support 2 or more arguments for ONE option. Is there any other way to do this? Thanks (2 Replies)
Discussion started by: testa500
2 Replies

3. Shell Programming and Scripting

Multiple runtime arguments

I am passing 3 runtime arguments to a shell script $path crtl1 crtl2 the crtl files contains data(filename|date|count) filename.txt|02/05/2010|10 The path contains the original data file,the code should fetch (filename|date|count) from original data file and it should match... (7 Replies)
Discussion started by: Prashanth B
7 Replies

4. Shell Programming and Scripting

Multiple arguments to read

I am developing a script where 3 other scripts are included. This is a graph related script. COMPLETE IDEA: -There are 3 different graph scripts. I would like to create a master graph with all 3 in one. -User chooses the type of graph -User is asked to enter the required auguments (... (7 Replies)
Discussion started by: newkid.7955
7 Replies

5. Shell Programming and Scripting

using switch on multiple arguments

I have a switch statement, and I want to have two options performing the same thing. For example, if $opt matches "-fb" or "--fbase", I want to perform the same operation. How can I include various matches in "case" ? switch ($opt) case "-T": set Atpath = $par set opt_tpath =... (8 Replies)
Discussion started by: kristinu
8 Replies

6. Programming

c++ assignment operator overloading

Hello everyone! Suppose that I have something like this A a; a.mem=new int; A b = a; where class A { public: int * mem; A() : mem(NULL) { } ~A() { if (mem!=NULL) delete mem; (1 Reply)
Discussion started by: bashuser2
1 Replies

7. Shell Programming and Scripting

How to use logic NOT operator for multiple AND conditions

Hi, my requirement is that my builds should not be built if the current hour is greater 3 but not (between 12 and 15), I'm trying to write a shell script for this but there is always an error hour=$1 echo "hour:$hour" if && ! && ]; then echo "exit" else echo "enter" fi ... (9 Replies)
Discussion started by: kgsrinivas
9 Replies

8. Shell Programming and Scripting

using multiple arguments in my script

hi all i am creating a script to ping hosts and then do a nslookup. So what needs to happen is that i type the script name with an argument eg: zong (script name) 172.x.x.x (IP) at the moment i have got it to take on argument, but idealy i would like it to take more than 1 argument. can you... (1 Reply)
Discussion started by: brian112
1 Replies

9. AIX

xlC compilation error when dealing with operator overloading

Hi, I have a piece of C++ code that can be compiled using g++, but reports an error when compiled with xlC: xlC -DHAVE_CONFIG_H -I../SRC -I../include -DNoChange -DSPRNG_MPI -q64 -DLONG64=long -I/usr/lpp/ppe.poe/include -DLONG64=long -c -o libsprng_a-bignum.o bignum.cpp "bignum.cpp",... (1 Reply)
Discussion started by: luop0812
1 Replies

10. Shell Programming and Scripting

Multiple Logical Operator Issues ...

Hi folks I have a snippet of code Which is like this If ( ( A || B || C ) && D ) then Do some thing.... elif exit fi How to rephrase this and use it so I dont get any Errors ... Looking out for helpful replies .. Thanks & Regards Srikanth GR (1 Reply)
Discussion started by: srikanthgr1
1 Replies
Login or Register to Ask a Question