Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

29. Can we define new operation?

Posted on 2015-01-24
6
Medium Priority
?
62 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 2000 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

671 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