#include <iostream>
#include <cstdlib>
#include <new>

using namespace std;

void memwarning();
void* operator new(size_t, int);
void operator delete(void*);

int main()
     char*p = new('$')char[100]; //Query 4b
     cout<<endl<<"First allocation: p = " <<hex<<long(p)<<endl; // Query 1
     for(int i=0;i<100;i++)


     delete p;// Query 2

     p=new('*')char[64000u]; // Query 3
     delete p;
     return 0;

void memwarning()
{    cout<<endl<<"Free store has now gone empty";

void* operator new(size_t sz, int setvalue)
     void *p;

     if (p==NULL)

     memset(p,setvalue, sz);
     return (p); //Query 4a

void operator delete(void *pp)
    free (pp);

Query 1) In this code the hex manipulator is printed before the address in p is converted to long....how come? since long(p) comes after hex in the code..

Query 2) please go through this sample program..

          /* int *p1;
          struct employee
                  char name[20];
                  int age;
                  float sal;
               }; *p2;

           p1=new int; // allocates 4 bytes
           p2=new employee;  // allocates 28 bytes

           int *p3;
           p3=new int[30]; // allocates memory for storing 30 integers

           // some code

           delete p1;
           delete p2;
           delete [ ] p3;

     Note the last usage of the delete operator:

            delete[ ]p3;
      THE AUTHOR SAYS "It indicates that we are not deleting a thing but an array of things pointed to by pointer p3 would a simple
             delete p3;
       not work in this case? The compiler may not flag an error, but whether it would work successfully or not would vary from compiler to compiler. In some compilers the heap may get corrupted, in some others only the first object in the array would get deleted. In short, you would be better off if you use delete[ ] whenever you allocate memory using new [ ]. Otherwise be prepared for a disaster.*/

       Ok, now why in the main program char array p is not deleted including the [ ] syntax????

Query 3) how come the compiler distinguished in the code p=new('*')char[64000u]; that it has to use global new and not custom new operator. What is the deciding factor?

Query 4) please observe the return type, we are returning a void pointer return (p); //Query 4a to char*p = new('$')char[100]; //Query 4b, here a void pointer cannot be assigned to a char pointer, but the inverse is possible i.e char pointer can be assigned to void pointer.

Recommended Answers

All 2 Replies

Are you using C++98 or C++11? The function definition for "new" is slightly different between the two.


If you are making a custom "new" operator, you should look at the links above and make your override function match exactlyn (and again, be aware that the C++98 and C++11 versions are slightly different, so point out which one you are using. Also be aware that "new" and "new[]" are different. Your custom function does not contain brackets. Your new CALLS do contain brackets, so the compiler, assuming it can actually compile the code (it didn't for me), would not pick your custom function because of that.

I'm not sure what you are trying to do with the '$' in the "new" calls, so I can't speculate how to fix the code to make it compile (I assume it did not compile for you either?).

As far as the "hex" question goes, "hex" doesn't display anything. It merely specifies that it should display the long(p) in hexadecimal rather than decimal. Why are you converting a char* pointer to a long?

You also have code and non-code mixed together, so it's sort of confusing. If the non-code part refers to questions about the code, it's easier to read if you comment it out using // or /* */ and make sure that the actual code part is uncommented. You have the actual code commented out and you have questions to Daniweb that shouldn't be in code at all.

If the code you post has errors in it and you know the code has errors in it and are asking why, please point that out and point out what you are trying to do. It's not clear, at least to me. It's particularly unclear what you are trying to do with the '$' code in the "new" statements. I thought the problem was that you were trying to have your custom "new" function called, but instead the global "new" was called and you didn't know why, so I assumed the code compiled and just executed in a way you did not understand. The code certainly is not compiling for me.

So hopefully I at least answered the "hex" query.

Thanks for your valuable adive about "hex" query.

The second Code is just a sample code to let all to know how I understand about deleting not a thing but an array of things, about the version it definitely is not c++11

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.