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
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