Is the next new memory leak on overload?

I came across the following code:

class a { public: void * operator new(size_t l, int nb); double values; }; void *a::operator new (size_t l,int n) { return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)]; } 

From what I get, then an array is used that looks like a structure starting with "values":

 double* Val = &(p->a->values) + fColumnNumber; 

My question is: is there a memory leak? I am very new to overloading a new operator, but I am sure that the allocated memory will not be freed properly. Also does this mean that I can never create a class β€œa” on the stack?

thanks

+4
source share
5 answers

I believe this technically creates UB, although it is a form of UB that will probably never cause a visible side effect (it uses new [] , but I believe it will be consistent with delete - but for char it usually will not cause visible Problems).

IMO, it's almost worse than using a new expression to highlight what should really be raw bytes instead of objects. If I did this, I would write it like this:

 void *a::operator new (size_t l,int n) { return ::operator new(l+ (n>1 ? n - 1 : 0)*sizeof(double)); } 

You will agree with this:

 void a::operator delete(void *block) { ::operator delete(block); } 
+5
source

I do not understand why, by default, operator delete , called in a * , will not be able to correctly free the memory allocated by this user-defined operator new . The best way to check would be to write the code and find out, although instead of the rob05c method, I could run it in a profiler like valgrind. I assume that the questionnaire sees a memory leak and suspects this as a reason, so writing a test case around this statement seems appropriate.

Obviously, it will flow if no one encounters the actual removal of it afterwards ...

I would question the need to redefine new for this kind of functionality, but I also assume that it was someone else's code.

+1
source

This is pretty easy to find out. Write a loop that builds and deconstructs a lot of files, and monitors your memory usage. It will be very fast if it leaks.

0
source

This is fine, but you need to use delete[] , not delete , from the code that uses this class, since it allocates an array. Note that the user would not receive any hints that they need to do this - so overloading the delete operator would be a good idea for them.

0
source
  • You can definitely create a class "a" on the stack.
  • There are 4 (actually more, but will stick to the basics) new and deleted method signatures that you should know.

     void* operator new (std::size_t size) throw (std::bad_alloc); void* operator new[] (std::size_t size) throw (std::bad_alloc); void operator delete (void* ptr) throw (); void operator delete[] (void* ptr) throw (); 

    You allocate an array in the operator new method, which must be executed in the operator new [] method. This will save you an unpleasant check. Write both: "operator new" and "operator new []"

  • Remember that you want to give the caller an object of type "a" ( a myA = new a ), so make sure you return "a" not char *, so you also need to cast.

  • You need to write the appropriate removal methods [] and removal.

  • To answer your question, I believe this is a memory leak. The new signature you provided is called the "placement of the new." This allows you to assign a new pointer without allocating memory, but give it a place to point. Example: if you need a pointer to a specific point in memory.

     long z = 0x0F9877F80078; a myA = new (z) a[5]; // 5 pointers that point to 0x0F9877F80078 

By definition, a placement-new operator should not allocate memory, and since you have a leak. Get rid of your second argument, which you can do now, since you have 2 versions of the new operator, and you are good to go. Remember to return the object "a".

Check out the IBM Information Center: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318.htm

And link or links, cpluplus.com: http://www.cplusplus.com/reference/std/new

0
source

Source: https://habr.com/ru/post/1411993/


All Articles