Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>You can definitely create Class "a" on the stack.</li> <li><p>There are 4 (actually more but will stick with the basics) new &amp; delete method signatures you should know. </p> <pre><code>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 (); </code></pre> <p>You are allocating an array within the "operator new" method which should be done in the "operator new[]" method. This will get rid of your nasty check. Write both, "operator new" and "operator new[]"</p></li> <li><p>Don't forget you want to give the caller an object of type "a" ( <code>a myA = new a</code>) so make sure you return "a" not char*, therefore you need to also be casting.</p></li> <li><p>You need to write the corresponding delete[] and delete methods.</p></li> <li><p>To answer your question, I believe it does leak memory. The new signature you've provided is called the "placement new". This allows you to allocate a new pointer without allocating memory but to give it a location to point to. Example: If you needed a pointer to a specific point in memory. </p> <pre><code>long z = 0x0F9877F80078; a myA = new (z) a[5]; // 5 pointers that point to 0x0F9877F80078 </code></pre></li> </ol> <p>By definition the placement-new operator is not supposed to allocate memory and since you have you make be leaking. Get rid of your second argument, which you can do now since you have 2 versions of operator new and you're good to go. Don't forget to return an object "a".</p> <p>Check out IBM's Info Center: <a href="http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318.htm" rel="nofollow">http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318.htm</a></p> <p>And the reference or references, cpluplus.com: <a href="http://www.cplusplus.com/reference/std/new" rel="nofollow">http://www.cplusplus.com/reference/std/new</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload