mlimber wrote:
Don't roll your own. Use std::vector for dynamic arrays. See this FAQ:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1
If you need to allocate memory dynamically, prefer new/delete to
malloc/realloc/free. See this FAQ:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.4
Cheers! --M
I know, I know, I must learn to trust the C++ way.
Can you specify a "realloc block size" to use with std::vector, so it
doesn't
have to allocate a new element each time it grows? My main interest is
in having
a dynamic "stack" of objects, but I will sometimes need to access
neighboring elements and/or march through the whole stack sequentially.
BTW I did get the pseudocode in my earlier post to work (or at least
not crash on the inputs I was using).
My increment function was
void InsertElement(ElementArray &A,Element El);
I found stepping through the debugger that for some reason El was being
treated as
a pointer! (i.e. El[0] gave me the object contents I expected, while El
was an address).
Changing my increment function to
void InsertElement(ElementArray &A,Element &El)
appears to have fixed the difficulty!
I have no idea why this is the case. I guess I have a lot to learn
about C++.
I would suggest you pick up _Accelerated C++_ by Koenig and Moo. It
teaches C++ from the ground up the right way.