Click here to Skip to main content
15,915,848 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: A simple question about memory reallocation Pin
David Crow24-Oct-06 8:29
David Crow24-Oct-06 8:29 
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 
Zac Howland wrote:
The problem is then that you have to keep track of the memory you allocated, how much is left, if you need to reallocate this time around, etc. With vector, that is taken care of for you


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. Allocating a big block of memory and doing a bulk read is the fastest he'll get, in which case realloc is an acceptable solution. My remarks were based on the context of his question.

Again, vector and other container types are fine for managing objects where you add infrequently, but are horrible for bulk-load ... although as a hack, he could use a vector, call reserve(), validate amount reserved, then call front() to get a reference (& for pointer), and pass that pointer to the file read function. But really, for what he needs to do, what does he gain.

Zac Howland wrote:
I have no problem with the C memory management routines, but when using C++, you really should stick with using the C++ versions instead


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.

Zac Howland wrote:
the Heap API's don't really gain you much anymore


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. An alternative would be custom code using the Virtual mem api to do something like http://www.hoard.org/[^]


...cmk

Save the whales - collect the whole set

GeneralRe: A simple question about memory reallocation Pin
Zac Howland25-Oct-06 8:35
Zac Howland25-Oct-06 8:35 
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 

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.