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

|
Related Articles |
|
|