Solved

29. Can we define new operation?

Posted on 2015-01-24
6
57 Views
Last Modified: 2015-03-17
Can we define new operation?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40568270
If you are referring to overload the 'new' operator - yes, that can be done, as well as 'delete'. Both have to be member overloads, see http://www.cprogramming.com/tutorial/operator_new.html ("Customized Allocators with Operator New and Operator Delete").

The scoop would be to

class MyClass
{
public:
        void* operator new(size_t);
        void operator delete(void*);
};

void* MyClass::operator new(size_t size)
{
    void *storage = malloc(size);
    if(NULL == storage) {
            throw "allocation fail : no free memory";
    }
}

Open in new window


There is also a so-called 'placement new' that allows you to create an object on a certain memory area that you can choose.
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40568442
Is this example more or less complete?

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
class MyClass
{
public:
        MyClass(){ cout << "MyClass::MyClass() is called" << endl;}
        void doSmth() { cout << "MyClass::doSmth() is called" << endl;}
        void* operator new(size_t);
        void operator delete(void*);
};

void* MyClass::operator new(size_t size)
{
    void *storage = malloc(size);


    cout << "Placement new operator is called" << endl;

    if(NULL == storage) {
            throw "allocation fail : no free memory";
    }
    return storage;
}

void MyClass::operator delete(void *obj)
{
    cout << "Delete operator is called" << endl;
    free(obj);

    obj = NULL;
}

int main()
{

    MyClass *mc = new MyClass;

    mc->doSmth();

    delete mc;

}

Open in new window



>>> There is also a so-called 'placement new' that allows you to create an object on a certain memory area that you can choose.

How we can create an object on a certain memory area?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 40568544
Hm, first of all, sorry again, I apparently missed toprovide an example for overloading 'delete()' in the above, that would be

void MyClass::operator delete(void* p)
{
    free(p)
}

Open in new window


>> Is this example more or less complete?

It is pretty much complete, yet the line
obj = NULL;

Open in new window


in your 'delete()'  implementation is superfluous - the operator is passed a pointer by value, setting that to NULL has no effect since it is not reflected in the calling code. But anyway, the idea is good.

OK, so now further on:

>> How we can create an object on a certain memory area?

Full explanation: http://en.wikipedia.org/wiki/Placement_syntax

The gist:

#include <iostream>

class MyClass {
   
   public:

    // Placement new operator
    void* operator new (size_t sz, void* v) {
        std::cout << "Placement new called" << std::endl;
        return v;
    }

    ~MyClass() {
        // Cleanup
    }
};

int main()
{
    // Create a buffer to store the object
    int buffer[256]; // providing 'sizeof(MyClass)' only would work with gcc/g++
    std::cout << "Starting address of my buffer = " << &buffer << std::endl;

    // Create the object. Use placement new
    MyClass* obj = new (buffer) MyClass();
    std::cout << "Location of my object = " << obj <<std:: endl;

    // Don't delete object created with placement delete
    // Call the destructor explicitly
    obj->~MyClass();
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nusrat Nuriyev
ID: 40606135
thank you, will  review that a bit later
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40670676
Yes, I have checked obj=NULL  does not affect the original pointer.
When I tried to change to void** , it started to blame:
new_del.cpp:13:30: error: ‘operator delete’ takes type ‘void*’ as first parameter
   void operator delete(void**);

Open in new window

Yes, we may overload the new and delete, but are those operator's prototypes are "carved in stone"?
0
 

Author Comment

by:Nusrat Nuriyev
ID: 40670802
int buffer[256]; // providing 'sizeof(MyClass)' only would work with gcc/g++

Open in new window


I don't get this comment. PLease, explain.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 152
Dialogbox API leak? 18 108
Embarcadero WebBroker REST server endpoint parameters 2 96
boost::uuid crashes 17 36
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question