Click here to Skip to main content
15,902,299 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: A simple question about memory reallocation Pin
Mark Salsbery24-Oct-06 8:44
Mark Salsbery24-Oct-06 8:44 
AnswerRe: A simple question about memory reallocation Pin
Zac Howland24-Oct-06 10:57
Zac Howland24-Oct-06 10:57 
AnswerRe: A simple question about memory reallocation Pin
cmk24-Oct-06 11:25
cmk24-Oct-06 11:25 
GeneralRe: A simple question about memory reallocation Pin
Zac Howland24-Oct-06 11:36
Zac Howland24-Oct-06 11:36 
GeneralRe: A simple question about memory reallocation Pin
cmk25-Oct-06 6:11
cmk25-Oct-06 6:11 
GeneralRe: A simple question about memory reallocation Pin
Zac Howland25-Oct-06 6:28
Zac Howland25-Oct-06 6:28 
GeneralRe: A simple question about memory reallocation Pin
cmk25-Oct-06 8:10
cmk25-Oct-06 8:10 
GeneralRe: A simple question about memory reallocation Pin
Zac Howland25-Oct-06 8:35
Zac Howland25-Oct-06 8:35 
cmk wrote:
I agree that it would be nice to use some kind of container, but for his stated use (reading a file of a bunch of ints) using vector to: read 4 bytes from file, vector.push_back, repeat, is probably the least efficient way of doing things.


Agreed. Which is why you don't see that in the code sample I provided him. When using STL, the algorithms are your best bet 90% of the time. Chances are if you are thinking of writing a loop, you could do it better and faster using an algorithm.

As a side note, realloc suffers from the same problem unless he knows how much memory is needed at the start (in which case, you avoid the need for extra memory allocations with vector as well by using reserve). The deque container is also a good choice for this kind of operation.

cmk wrote:
Totally disagree, C vs C++ is irrelevant. Using new/delete vs alloc/free is a matter of which tool is best for the job. In C++ both have a place.


To an extent, that can be coughed up to a matter of opinion. However, when using malloc/free and new/delete mixed in the same application, you run into the potential for memory corruption (say, by calling free on something you new'd). It is just better avoided most of the time.

cmk wrote:
I use them to create custom allocators for certain object types. Using the default new/delete for all cases will lead to memory frag issues, paging issues, thread contention issues, ... Custom allocators can create much more efficient code. The Heap*() api provides a convenient way of creating/managing these allocators.


The default new/delete are designed to be a general purpose operator. They will work for most things, but yes, there are a few cases where you may want to override them. Those cases are few and far between though. I've seen far too many programmers try to "optimize" their code by overriding new/delete only to realize that memory was never their main bottleneck anyway.

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

QuestionText does not show on toolbar Pin
peterchen24-Oct-06 7:35
peterchen24-Oct-06 7:35 
AnswerRe: Text does not show on toolbar Pin
Mark Salsbery24-Oct-06 7:59
Mark Salsbery24-Oct-06 7:59 
GeneralRe: Text does not show on toolbar Pin
peterchen24-Oct-06 13:02
peterchen24-Oct-06 13:02 
GeneralRe: Text does not show on toolbar Pin
Mark Salsbery24-Oct-06 13:14
Mark Salsbery24-Oct-06 13:14 
Questionnavigation for vectorRichEditBox Pin
prithaa24-Oct-06 7:11
prithaa24-Oct-06 7:11 
AnswerRe: navigation for vectorRichEditBox Pin
prithaa24-Oct-06 7:18
prithaa24-Oct-06 7:18 
GeneralRe: navigation for vectorRichEditBox Pin
prithaa24-Oct-06 7:25
prithaa24-Oct-06 7:25 
QuestionRe: navigation for vectorRichEditBox Pin
Maximilien24-Oct-06 7:58
Maximilien24-Oct-06 7:58 
AnswerRe: navigation for vectorRichEditBox Pin
prithaa24-Oct-06 8:11
prithaa24-Oct-06 8:11 
GeneralRe: navigation for vectorRichEditBox Pin
Mark Salsbery24-Oct-06 8:28
Mark Salsbery24-Oct-06 8:28 
AnswerRe: navigation for vectorRichEditBox Pin
David Crow24-Oct-06 8:32
David Crow24-Oct-06 8:32 
GeneralRe: navigation for vectorRichEditBox Pin
prithaa24-Oct-06 9:50
prithaa24-Oct-06 9:50 
GeneralRe: navigation for vectorRichEditBox Pin
prithaa24-Oct-06 10:07
prithaa24-Oct-06 10:07 
GeneralRe: navigation for vectorRichEditBox Pin
David Crow24-Oct-06 10:08
David Crow24-Oct-06 10:08 
QuestionPrinting a document in VC++/MFC Pin
yaints24-Oct-06 6:41
yaints24-Oct-06 6:41 
QuestionDisplay a message in a box and close it from the program Pin
Giulio200024-Oct-06 5:43
Giulio200024-Oct-06 5:43 
AnswerRe: Display a message in a box and close it from the program Pin
David Crow24-Oct-06 5:47
David Crow24-Oct-06 5:47 

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.