Click here to Skip to main content
15,891,136 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 4:37
Hamed Musavi28-Feb-08 4:37 
GeneralRe: How does delete know it's size? Pin
CPallini28-Feb-08 4:41
mveCPallini28-Feb-08 4:41 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 4:44
Hamed Musavi28-Feb-08 4:44 
AnswerRe: How does delete know it's size? Pin
Rajkumar R28-Feb-08 18:25
Rajkumar R28-Feb-08 18:25 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 19:07
Hamed Musavi28-Feb-08 19:07 
QuestionRe: How does delete know it's size? Pin
Rajkumar R28-Feb-08 19:29
Rajkumar R28-Feb-08 19:29 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 20:19
Hamed Musavi28-Feb-08 20:19 
AnswerRe: How does delete know it's size? [modified] Pin
Rajkumar R28-Feb-08 23:26
Rajkumar R28-Feb-08 23:26 
Hamed Mosavi wrote:
My question was that, is it possible to get rid of casting and the type specified in new and explicitly define needed size of memory to allocate(and based on the type myStruct) the operator cast the result automatically?


why not, C++ is powerful man,


#include<new>
#include<iostream>

using namespace std;

struct myStruct
{
   myStruct( )
   {
      cout << "Construction myStruct." << this << endl;
   };

   ~myStruct( )
   {
      cout << "Destructing myStruct." << this << endl;
   };

   void *__CRTDECL operator new(size_t size, size_t realSize)
   {	// allocate the buffer for your required size
	return malloc(realSize);
   }

   void __CRTDECL operator delete(void * ptr, size_t) 
   {	// free the buffer
	free (ptr);
   }
};


int main( ) 
{
   // 1) using our custom new / delete operator
   const int nSize = sizeof(myStruct) + 100;
   myStruct* pStruct1  = new (nSize) myStruct;
   delete pStruct1 ;

   //

   // 2) using the placement new / delete operator
   char preAllocatedBuffer[nSize];  
   myStruct* pStruct2  = ::new( &preAllocatedBuffer[0] ) myStruct;
   <code>pStruct2->~myStruct();</code>
}


some of the techniques,
you can pass the custom size to the overloaded new operator see the 1) in the code.

And there is also a placement new operator that is defined if you #include <new>, in this you can pass pre allocated buffer and get the class created see 2) in the code, in the placement case donot use delete as the buffer need not to be freed by the class.

and also if size is multiple of myStruct,
straitforward
myStruct* pStruct2  = ::new myStruct[2];


modified on Friday, February 29, 2008 6:58 AM

GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 23:58
Hamed Musavi28-Feb-08 23:58 
GeneralRe: How does delete know it's size? Pin
Rajkumar R29-Feb-08 0:24
Rajkumar R29-Feb-08 0:24 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi29-Feb-08 1:02
Hamed Musavi29-Feb-08 1:02 
GeneralRe: How does delete know it's size? Pin
Hamed Musavi28-Feb-08 4:49
Hamed Musavi28-Feb-08 4:49 
Questiondynamic array without new Pin
tobse28-Feb-08 3:02
tobse28-Feb-08 3:02 
QuestionRe: dynamic array without new Pin
David Crow28-Feb-08 3:11
David Crow28-Feb-08 3:11 
GeneralRe: dynamic array without new Pin
Cedric Moonen28-Feb-08 3:17
Cedric Moonen28-Feb-08 3:17 
GeneralRe: dynamic array without new Pin
tobse28-Feb-08 3:43
tobse28-Feb-08 3:43 
GeneralRe: dynamic array without new Pin
Cedric Moonen28-Feb-08 3:49
Cedric Moonen28-Feb-08 3:49 
QuestionRe: dynamic array without new Pin
CPallini28-Feb-08 3:21
mveCPallini28-Feb-08 3:21 
GeneralRe: dynamic array without new Pin
tobse28-Feb-08 3:47
tobse28-Feb-08 3:47 
GeneralRe: dynamic array without new Pin
tobse28-Feb-08 3:23
tobse28-Feb-08 3:23 
GeneralRe: dynamic array without new Pin
Stephen Hewitt28-Feb-08 13:46
Stephen Hewitt28-Feb-08 13:46 
GeneralRe: dynamic array without new Pin
tobse28-Feb-08 22:42
tobse28-Feb-08 22:42 
GeneralRe: dynamic array without new Pin
Stephen Hewitt29-Feb-08 16:52
Stephen Hewitt29-Feb-08 16:52 
GeneralRe: dynamic array without new [modified] Pin
tobse3-Mar-08 23:38
tobse3-Mar-08 23:38 
GeneralRe: dynamic array without new Pin
Stephen Hewitt4-Mar-08 11:33
Stephen Hewitt4-Mar-08 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.