Click here to Skip to main content
15,917,928 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: windows 7 UAC [fixed] Pin
CPallini20-Nov-10 0:01
mveCPallini20-Nov-10 0:01 
AnswerRe: windows 7 UAC [modified] Pin
MsmVc20-Nov-10 0:49
MsmVc20-Nov-10 0:49 
AnswerRe: windows 7 UAC Pin
MsmVc20-Nov-10 1:08
MsmVc20-Nov-10 1:08 
GeneralRe: windows 7 UAC Pin
CPallini20-Nov-10 2:34
mveCPallini20-Nov-10 2:34 
QuestionThe difference between new and malloc function Pin
ycc89200919-Nov-10 21:31
ycc89200919-Nov-10 21:31 
AnswerRe: The difference between new and malloc function Pin
Code-o-mat19-Nov-10 21:51
Code-o-mat19-Nov-10 21:51 
AnswerRe: The difference between new and malloc function PinPopular
CPallini19-Nov-10 22:28
mveCPallini19-Nov-10 22:28 
AnswerRe: The difference between new and malloc function PinPopular
Aescleal19-Nov-10 23:36
Aescleal19-Nov-10 23:36 
new creates new objects, malloc just reserves you a lump of memory and gives you a pointer to it. So when you say:

foo *p = new foo;


you're asking the compiler to generate the code for creating an original instance of foo (whatever that is) and to tell you where it is in the program's address space. Generally this means:

- The compiler generates code for reserving a lump of memory to store the object in
- It calls the appropriate constructor for foo and runs it on that lump of memory.

On the other hand when you say (in C):

foo *p = malloc( sizeof( foo ) );


you're saying "give me a lump of bytes that happens to be the size of a foo and I'll treat it as a foo." There can be any old rubbish in there. If you want it initialised into any reasonable state you've got to do it manually. Incidentally this is one of the reasons you'll find C programmers immediately memsetting allocated memory blocks or using calloc instead - it gets the memory into a known state. You'll also find that many decent C programmers bundle allocation and initialisation into functions and call them things like "construct_foo" to keep the operation atomic.

Incidentally there are loads of places you can plug into the object creation sequence process in C++ but very few (i.e. none I know of) of modifying the way malloc works in C. In C++ you can modify how the memory is reserved (by overloading operator new or using placement new), what to do if the allocation fails (set_new_handler) and how the object is initialised (by writing constructors).

One final point. If new fails it throws an exception and winds back any initialisation or memory reservation that's already been completed. The exception is either bad_alloc if the memory reservation fails, or something from the constructor. If malloc fails it returns a NULL pointer, which is another good reason to use initialisation functions in C - you can handle all the errors in one place.

Cheers,

Ash
AnswerRe: The difference between new and malloc function Pin
«_Superman_»22-Nov-10 7:15
professional«_Superman_»22-Nov-10 7:15 
Questiongfh Pin
kongdl19-Nov-10 21:09
kongdl19-Nov-10 21:09 
QuestionHow to call DoDataExchange() of a property page at runtime Pin
lakshman rao19-Nov-10 21:00
lakshman rao19-Nov-10 21:00 
AnswerRe: How to call DoDataExchange() of a property page at runtime Pin
KingsGambit21-Nov-10 17:13
KingsGambit21-Nov-10 17:13 
AnswerRe: How to call DoDataExchange() of a property page at runtime Pin
«_Superman_»22-Nov-10 7:11
professional«_Superman_»22-Nov-10 7:11 
Questiontimeout problem Pin
ALLERSLIT19-Nov-10 10:35
ALLERSLIT19-Nov-10 10:35 
AnswerRe: timeout problem Pin
«_Superman_»19-Nov-10 11:44
professional«_Superman_»19-Nov-10 11:44 
AnswerRe: timeout problem Pin
Moak19-Nov-10 14:26
Moak19-Nov-10 14:26 
QuestionlineOpen() problem in TAPI Pin
AmbiguousName19-Nov-10 8:20
AmbiguousName19-Nov-10 8:20 
QuestionRe: lineOpen() problem in TAPI Pin
CPallini19-Nov-10 9:09
mveCPallini19-Nov-10 9:09 
AnswerRe: lineOpen() problem in TAPI Pin
AmbiguousName19-Nov-10 20:38
AmbiguousName19-Nov-10 20:38 
GeneralRe: lineOpen() problem in TAPI Pin
Richard MacCutchan19-Nov-10 22:36
mveRichard MacCutchan19-Nov-10 22:36 
QuestionRe: lineOpen() problem in TAPI Pin
CPallini19-Nov-10 22:40
mveCPallini19-Nov-10 22:40 
QuestionExecuting a usermode program from a driver? Pin
Member 469267019-Nov-10 3:36
Member 469267019-Nov-10 3:36 
AnswerRe: Executing a usermode program from a driver? Pin
Luc Pattyn19-Nov-10 6:58
sitebuilderLuc Pattyn19-Nov-10 6:58 
AnswerRe: Executing a usermode program from a driver? Pin
«_Superman_»19-Nov-10 7:19
professional«_Superman_»19-Nov-10 7:19 
QuestionRe: Executing a usermode program from a driver? Pin
CPallini19-Nov-10 9:06
mveCPallini19-Nov-10 9:06 

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.