Igal wrote:
> hay, i have this werid problem with my book adding function, this how
> it looks
>
> book* AddBook(book *bp, unsigned *size) {
> ...
> //then i use realloc to allocate space for the new item in the bp
> pointer
> bp = (book*)realloc(bp, sizeof(book));
realloc accepts the pointer to the prevoiusly allocated block, and the new
size. Since you are passing sizeof( book ) you are only allocating enough
space for 1 item.
You probably meant something like:
bp = realloc( bp, sizeof( book ) * *size );
or
bp = realloc( bp, sizeof( book ) * (*size + 1) )
or something. I'm not sure how many additional items you want to allocate
for nor what size represents (new size or old size).
> ...
> }
>
> when i do this, the next them is added in this right space. but let's
> say *bp hold 2 book type items, first item will be ok, second one will
> get garbage content, then the third item (the new reallocated one)
> will get the new data.
> i just have no idea will the last item gets corruped, this also
> happens with more items in array.
>
> i know that to use linked lists is a lot better solution, but i still
> dunno how to use them right just yet.
--
Jim Langston
tazmaster@[EMAIL PROTECTED]


|
16 Posts in Topic:
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:15:19 |
|
Richard Heathfield <rj |
2008-05-08 14:27:23 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:33:01 |
|
"Jim Langston" |
2008-05-08 07:52:48 |
|
"Jim Langston" |
2008-05-08 07:36:48 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:37:40 |
|
Igal <igal.alkon@[EMAI |
2008-05-08 07:43:38 |
|
"Jim Langston" |
2008-05-08 07:54:42 |
|
Kenneth Brody <kenbrod |
2008-05-08 10:56:41 |
|
"K. Jennings" & |
2008-05-08 18:57:08 |
|
Eric Sosman <Eric.Sosm |
2008-05-08 13:55:17 |
|
Kenneth Brody <kenbrod |
2008-05-08 13:46:46 |
|
CBFalconer <cbfalconer |
2008-05-08 18:17:43 |
|
Richard Heathfield <rj |
2008-05-09 05:32:33 |
|
Antoninus Twink <nospa |
2008-05-09 11:52:02 |
|
Eligiusz Narutowicz<el |
2008-05-09 11:54:42 |
|