Realloc array of pointers to objects?

M

matt.wolinsky

Hello C++ gurus,

I am trying to learn about how to use C++ memory management, and I know
that
there is no "renew" command in C++. What I am hoping to do is slightly
different
though.

I want to implement a dynamic "array" by using an array of pointers to
objects,
something like this:

class Element { double a,b,c; };

struct ElementArray { int Nel,Nall; Element **els; };

void InsertElement(ElementArray &A,Element El)
{
if( A.Nel == A.Nall )
{ A.Nall+=10; // some block size
A.els = (Element**)realloc(A.els,A.Nall*sizeof(Element*));
for(int el=A.Nel; el<A.Nall;lyr++) A.els[el] = new Element;
}
*A.els[A.Nel]=El;
A.Nel++;

}

Will this work? (I know I haven't checked for successful memory
allocation)

I am not moving El objects around, but I am moving around pointers
returned by new.
Will this create problems?

I have started on a program using this approach, and am getting some
errors, but I don't
know if this is because of my use of realloc.

I would appreciate any advice you may have.

Thank you for your assistance,

Matt Wolinsky
 
M

mlimber

Hello C++ gurus,

I am trying to learn about how to use C++ memory management, and I know
that
there is no "renew" command in C++. What I am hoping to do is slightly
different
though.

I want to implement a dynamic "array" by using an array of pointers to
objects,
something like this:

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
 
M

matt.wolinsky

mlimber said:
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++.
 
A

Andre Kostur

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.

std::vector already does that.
 
N

Noah Roberts

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?

I assume you have already written your implementation, profiled, and
found a bottleneck at vector insertion.

You can do two things under such extremely rare conditions:

1) Specify a size in element count for the initial vector and/or
"resize" to a particular size.
2) Write a specialized allocator for your element types and pass it as
template parameter to the vector template.

The first is easy. The second requires knowledge I have not needed to
learn thus far.
 
M

mlimber

mlimber said:
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.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top