Realloc a struct **



 DEVELOP > c-Plus-Plus > Realloc a struct **

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Henrik J"
Date: 10 Nov 2003 02:01:46 AM
Object: Realloc a struct **
Hello group...
I have this little problem:
I'm using a struct **foo. I have allocated x foo's using malloc:
foo=(FOO**)malloc(Amount*sizeof(FOO*));
No problem....!
but my question is now: How do I realloc a **foo...!
I'm thinking of doing:
struct **tempfoo;
tempfoo=(FOO**)realloc(Amount+extraAmount,sizeof(FOO*));
foo=tempfoo;
Somehow this will not work...
Hope some of you can help...
Regards Henrik Tomra
.

User: "David White"

Title: Re: Realloc a struct ** 10 Nov 2003 02:54:53 AM
"Henrik J" <doktorhj@sol.dk> wrote in message
news:d124446c.0311100001.70be9845@posting.google.com...

Hello group...
I have this little problem:

I'm using a struct **foo. I have allocated x foo's using malloc:

You mean x FOOs, right?


foo=(FOO**)malloc(Amount*sizeof(FOO*));

No problem....!


but my question is now: How do I realloc a **foo...!
I'm thinking of doing:

struct **tempfoo;
tempfoo=(FOO**)realloc(Amount+extraAmount,sizeof(FOO*));
foo=tempfoo;

Somehow this will not work...

Well, the documentation I have declares 'realloc' as:
void *realloc( void *memblock, size_t size );
So the correct code would be:
foo = static_cast<FOO**>(realloc(foo, new_size_in_bytes));
However, it almost offends the senses to see static_cast and realloc in the
same statement, so maybe it's better to make it:
foo = (FOO**)realloc(foo, new_size_in_bytes);
At least then you can move the code to a C source file where it belongs.

Hope some of you can help...

The best help I can offer is to repeat the following line 1000 times:
"When writing C++ code I will never use 'malloc' again."
Unless you have special reasons for writing C code in C++, use a std::vector
to store your pointers. It does all reallocations for you.
DW
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER