Velocity Reviews > Rallocating a struct * *

# Rallocating a struct * *

Henrik J
Guest
Posts: n/a

 11-07-2003
Hello..!

I have this problem:
Following shows allocation of my struct:

///////////////////////////////////////////
FOO **foo;//this is my struct
FOO **reallocfoo;//this is an extra pointer

foo=(FOO **)malloc(StartNumber*sizeof(FOO*));
for(int c=0;c<StartNumber;c++)
{
ILChild[c]=new ImageLocaterData;
}
....
foo[Number]=...
....

///////////////////////////////////////////

This works well...!

Then I want to Realloc som more space for my foo.
Strangely I can only allocate space for my pointer 44 times...!!?

Following shows the code to realloc:

///////////////////////////////////////////

reallocfoo=(FOO**)realloc(foo,(StartNumber+(Reallo cInc*(Counter+1)))*sizeof(FOO*));
for(int c=StartNumber+(ReallocInc*Counter)-1;c<StartNumber+(ReallocInc*(Counter+1));c++)
{
reallocfoo[c]=new FOO;
}
foo=reallocfoo;
Counter+=1;

///////////////////////////////////////////

44 times that dosn't make sense...

Regards Henrik Tomra

Tristan Miller
Guest
Posts: n/a

 11-07-2003
Greetings.

In article <(E-Mail Removed) >, Henrik J
wrote:
> I have this problem:
> Following shows allocation of my struct:
>
> ///////////////////////////////////////////
> FOO **foo;//this is my struct
> FOO **reallocfoo;//this is an extra pointer
>
> foo=(FOO **)malloc(StartNumber*sizeof(FOO*));
> for(int c=0;c<StartNumber;c++)
> {
> ILChild[c]=new ImageLocaterData;
> }
> ...
> foo[Number]=...
> ...
>
> ///////////////////////////////////////////
>
> This works well...!

On the contrary; this does not work at all. There is no "new" keyword in C,
and for a number of good reasons the result of malloc oughtn't be cast.

I suspect you're trying to write C++ code, not C, in which case you should
post this query to comp.lang.c++.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you

Sheldon Simms
Guest
Posts: n/a

 11-07-2003
On Fri, 07 Nov 2003 06:50:35 -0800, Henrik J wrote:

> Hello..!
>
> I have this problem:
> Following shows allocation of my struct:

C++ code snipped

> 44 times that dosn't make sense...

After converting your code to C, I was able to realloc 45
times. If your intention is to write C (and not C++), then
try to post a minimal complete (compilable!) program that
demonstrates your problem. Then we'll be better able to help
you out.

-Sheldon