473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Realloc array of pointers to objects?

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

May 18 '06 #1
5 2971
ma***********@gmail.com wrote:
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-lit....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-lit....html#faq-16.4

Cheers! --M

May 18 '06 #2

mlimber wrote:

Don't roll your own. Use std::vector for dynamic arrays. See this FAQ:

http://www.parashift.com/c++-faq-lit....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-lit....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++.

May 18 '06 #3
On Thu, 18 May 2006 12:29:52 -0700, "matt.wolinsky wrote:
mlimber wrote:

Don't roll your own. Use std::vector for dynamic arrays. See this FAQ:

http://www.parashift.com/c++-faq-lit....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-lit....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.


std::vector already does that.
May 18 '06 #4

ma***********@gmail.com wrote:
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.

May 18 '06 #5
ma***********@gmail.com wrote:
mlimber wrote:

Don't roll your own. Use std::vector for dynamic arrays. See this FAQ:

http://www.parashift.com/c++-faq-lit....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-lit....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

May 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andreas Bauer | last post by:
I've got an array in the vein of MyType* array; arry = new MyType; This of course works fine, but I have the need to dynamically grow the boundaries of that array at runtime. I can't seem to...
9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
9
by: Goh, Yong Kwang | last post by:
I'm currently doing a project that has a array that supposed to be determined only at runtime. Originally, the prototype I did as a proof of theory (or a quick hack) So one method is to create a...
4
by: John | last post by:
I'm trying (struggling) to use realloc to grow a list of strings. The number of strings is not known (this is a subset of an assignment to write a recursive ls program... my BS was in EE, so I'm...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
8
by: Jean-Claude Arbaut | last post by:
When you realloc for more size, it may be necessary to allocate a new block, copy memory and deallocate the old, for example if there is not enough free space after the original block (maybe the...
64
by: Robert Seacord | last post by:
The C standard doesn't say anything about what happens when you call realloc with a size argument of 0. Both glibc and openbsd appear to return a valid pointer to a zero-sized object.. e.g. the...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.