Click here to Skip to main content
15,908,776 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalnew&delete, malloc&free Pin
raner13-Jan-03 22:26
raner13-Jan-03 22:26 
GeneralRe: new&delete, malloc&free Pin
jhwurmbach13-Jan-03 22:46
jhwurmbach13-Jan-03 22:46 
GeneralRe: new&delete, malloc&free Pin
raner14-Jan-03 0:19
raner14-Jan-03 0:19 
GeneralRe: new&delete, malloc&free Pin
jhwurmbach14-Jan-03 1:08
jhwurmbach14-Jan-03 1:08 
GeneralThks jhwurmbach & AlexO Pin
raner14-Jan-03 20:56
raner14-Jan-03 20:56 
GeneralRe: Thks jhwurmbach & AlexO Pin
jhwurmbach14-Jan-03 22:59
jhwurmbach14-Jan-03 22:59 
GeneralRe: Thks jhwurmbach & AlexO Pin
raner15-Jan-03 3:54
raner15-Jan-03 3:54 
GeneralRe: new&delete, malloc&free Pin
AlexO14-Jan-03 4:20
AlexO14-Jan-03 4:20 
The best way to understand the difference is to analyze what new/delete is actually doing.

new(global new operator):
1. Call operator new of the data type to allocate n bytes of memory. Default implementation just calls ::malloc(n)
2. Call constructor of the given data type for the new object

delete (global delete operator):
1. Call destructor of the given data type for the object being deleted
2. Call operator delete of the data type to free the memory. The default implementation just calls ::free(p).

Example
main()
{
A* pA = new A();//1. call ::new
//2. call A::operator new(...)
//3. call A::A()

delete pA; ;//1. call ::delete
//2. call A::~A
//3. call A::operator delete(...)
}

The array and build in types are slightly different, but we can ignore the difference for now.


Now about loop allocation
int* pi = 0;
for(int i = 0; i < 100;++i)
{
pi = new int;//we allocate new int
//if we do not call delete before next iteration
//we are going to loose memory when pi is
//reassign to new value
}

I hope it helps
AlexO
P.S. I did not cover constructor/destructor invokation order, I assume you already know about that
GeneralWord Automation Pin
Exceter13-Jan-03 18:56
Exceter13-Jan-03 18:56 
GeneralRe: Word Automation Pin
Anonymous13-Jan-03 20:40
Anonymous13-Jan-03 20:40 
GeneralMAC based Firewall Pin
summo13-Jan-03 18:11
summo13-Jan-03 18:11 
GeneralRe: MAC based Firewall Pin
Dana Epp15-Jan-03 9:02
Dana Epp15-Jan-03 9:02 
Generalproblem in debugging VC 6 code in windows xp Pin
r i s h a b h s13-Jan-03 16:45
r i s h a b h s13-Jan-03 16:45 
General*.pck file Pin
:_Rocket_:13-Jan-03 16:04
:_Rocket_:13-Jan-03 16:04 
GeneralRe: *.pck file Pin
Taka Muraoka13-Jan-03 17:38
Taka Muraoka13-Jan-03 17:38 
GeneralRe: *.pck file Pin
Ted Ferenc13-Jan-03 22:54
Ted Ferenc13-Jan-03 22:54 
Generalmodeless dialog(rather long story), I appreciate Pin
Anonymous13-Jan-03 15:29
Anonymous13-Jan-03 15:29 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Joaquín M López Muñoz13-Jan-03 20:58
Joaquín M López Muñoz13-Jan-03 20:58 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Hans Ruck13-Jan-03 22:02
Hans Ruck13-Jan-03 22:02 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Anonymous14-Jan-03 3:53
Anonymous14-Jan-03 3:53 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Hans Ruck14-Jan-03 5:14
Hans Ruck14-Jan-03 5:14 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Anonymous14-Jan-03 5:25
Anonymous14-Jan-03 5:25 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Hans Ruck14-Jan-03 5:44
Hans Ruck14-Jan-03 5:44 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Anonymous14-Jan-03 10:11
Anonymous14-Jan-03 10:11 
GeneralRe: modeless dialog(rather long story), I appreciate Pin
Anonymous14-Jan-03 11:29
Anonymous14-Jan-03 11:29 

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.