By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Realloc a struct **

P: n/a
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(F OO*));
foo=tempfoo;

Somehow this will not work...
Hope some of you can help...

Regards Henrik Tomra
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Henrik J" <do******@sol.dk> wrote in message
news:d1**************************@posting.google.c om...
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(F OO*));
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

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.