0

i ve overloaded new operator for the class which contains an integer variable and a character pointer. when overloaded the new operator allocates memory only for the integer as it first creates memory and then calls the constructor. could someone please tell me how to rectify this?

//program for overloading new operator


//header files
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

class Student
{
   char *name;
   int age;
   public:
   Student(char *name,int age)
   {
     strcpy(name,this->name);
     this->age=age;
   }
  void disp()
  {
     cout<<"\n name is "<<name<<" age is "<<age<<endl;
  }
  void * operator new(size_t s)
  {
     cout<<" \n in new block and size is "<< s;
     void *ptr=malloc(s);
     if(!ptr) cout<<"\n memory full ";
     else return ptr;
  }
  void operator delete(void *ptr)
  {
     cout<<" \n in delete block ";
     free(ptr);
  }
};

main()
{
   clrscr();
   Student *ob=new Student("abc",15);
   ob->disp();
   delete ob;
   getch();
}

produces an output

in new block and size is 4 name is age is 15

in delete block

1 Answers1

1

From operator new, operator new[]

Class-specific overloads

Both single-object and array allocation functions may be defined as public static member functions of a class (versions (15-18)). If defined, these allocation functions are called by new-expressions to allocate memory for single objects and arrays of this class,

  • So the new operator allocates memory for new instances of the class, not for the attributes of the class.
  • If you need to allocate memory for attributes of the class do that e.g. in the constructor.
  • Avoid the use of char* for strings. Rather use C++ Strings.
  • Avoid pointers as much as possible. Have a look at smart pointers.

For the sake of completeness, if you must use char*, you can use strlen, new, strncpy and delete to do this. Do not use strcpy as this can cause an overflow.. (To get the below to compile in VS2015 you will need to add the _CRT_SECURE_NO_WARNINGS to the preprocessor definitions.)

int length = strlen(source) + 1;
char *destination = new char[length];
if (destination) {
    strncpy(destination, source,length);
}
...
delete destination //do not forget to free the allocate memory

Here is the Student class.

  • The 1st constructor takes a char* (for demonstration purposes only!)
  • But better, the 2nd constructor uses
    • no pointers,
    • it uses C++ strings, and
    • uses the constructors initializer list to setup the attributes.
  • new operator (just delegates to the global new operator).
  • delete operator (for completeness, delegates to global delete operator).

.

#include <iostream>
#include <string>

// class-specific allocation functions
class Student {

    char *nameCharArray; //this only for demonstration
    std::string name; //prefer string
    int age;
public:
    Student(char *name, int age) : age(age)
    {
        int length = strlen(name) + 1;
        nameCharArray = new char[length];
        if (this->nameCharArray) {
            strncpy(this->nameCharArray, name, length);
        }
        this->name = std::string(nameCharArray);
    }

    Student(std::string &name, int age) : name(name), age(age) {}

    ~Student() {
        std::cout << "Freeing name... " << std::endl;
        delete nameCharArray;
    }

    std::string getName() const {
        return name;
    }

    static void* operator new(std::size_t sz)
    {
        std::cout << "custom new for size " << sz << '\n';
        return ::operator new(sz);
    }

    static void operator delete(void* ptr, std::size_t sz)
    {
        std::cout << "custom delete for size " << sz << '\n';
        ::operator delete(ptr);
    }

};

This is how the code is used

int main() {
    char name[] = "studentname";
    Student* p1 = new Student(name, 12);

    std::cout << p1->getName() << std::endl;

    delete p1;
}
Community
  • 1
  • 1
robor
  • 2,969
  • 2
  • 31
  • 48