PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > problem with realloc
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
problem with realloc

Réponse
 
LinkBack Outils de la discussion
Vieux 03/05/2008, 17h21   #1
Igal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut problem with realloc

hay, i'm doing this program. having problem wiht realloc in the
function that reads data structures into array (pointer - bp2), this
happens after reading the second record. when call to realloc.
i can't figure out what's wrong, think it's soming got to do with
freeing bp2.
and something called "corruption of the heap".

book* LoadBookData(unsigned *size)
{
FILE* fp;
int n = 0;
book *bp2 = NULL;

//open book data file
fp=fopen("book.bin","rb");
if (fp == NULL)
{
bp2 = (book*)calloc(0, sizeof(book));
return bp2;
}
<<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

//read data from file
while(fread(bp2,sizeof(book),1,fp) == 1)
{
if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
exit(ERR_FREAD); }
bp2 = bp2 - n;
n++;

bp2 = realloc(bp2, sizeof(book));
if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
exit(ERR_REALOC); }

bp2 = bp2 + n;
}

//close book data file
if (fclose(fp)==EOF)
{ perror("ERROR [book.bin - close - LoadBookData()]]");
exit(ERR_FCLOSE); }

*size = n;

return bp2;
}
  Réponse avec citation
Vieux 03/05/2008, 17h33   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

On May 3, 7:21 pm, Igal <igal.al...@gmail.com> wrote:
> hay, i'm doing this program. having problem wiht realloc in the
> function that reads data structures into array (pointer - bp2), this
> happens after reading the second record. when call to realloc.
> i can't figure out what's wrong, think it's soming got to do with
> freeing bp2.
> and something called "corruption of the heap".
>
> book* LoadBookData(unsigned *size)
> {
> FILE* fp;
> int n = 0;
> book *bp2 = NULL;
>
> //open book data file
> fp=fopen("book.bin","rb");
> if (fp == NULL)
> {
> bp2 = (book*)calloc(0, sizeof(book));
> return bp2;

Don't cast calloc. I'm not sure what the results of calloc(0, N) are,
but if they are similar of malloc(), then that pointer is not really
reliable.
You most likely want something like this:
return calloc(0, sizeof book);
> }
> <<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>

Comment your code with /* */
> bp2 = realloc(bp2, sizeof(book));

That's actually a malloc(), since bp2 was initialized to NULL before,
and realloc(NULL, N) is equal to malloc(N)
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }

Are you sure you want to exit here? And what is the value of
ERR_REALOC?
>
> //read data from file
> while(fread(bp2,sizeof(book),1,fp) == 1)
> {
> if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");

fread() can't return the count (third parameter) if an error in the
stream occurs.
> exit(ERR_FREAD); }

Memory leak here, you don't free `bp2'.
> bp2 = bp2 - n;
> n++;

Why?
>
> bp2 = realloc(bp2, sizeof(book));

If `bp2' doesn't point to a pointer returned by realloc, malloc or
calloc, realloc() will produce undefined results (unless `bp2's value
is NULL)
But I *see* what you are trying to do, here's the actual logic:
size_t n = 0;
back:
if(fread(&bp2[n], sizeof bp2[0], 1, fp) != 1) { /* handle error or EOF
*/ }
n++;
bp2 = realloc(bp2, n+1 * sizeof bp2[0]);
if(bp2 == NULL) { /* ... */ }
goto back;
  Réponse avec citation
Vieux 03/05/2008, 18h13   #3
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc


"Igal" <igal.alkon@gmail.com> wrote in message
news:5392c5d0-dd97-484b-930a-4f34db38bc1a@e39g2000hsf.googlegroups.com...
> hay, i'm doing this program. having problem wiht realloc in the
> function that reads data structures into array (pointer - bp2), this
> happens after reading the second record. when call to realloc.
> i can't figure out what's wrong, think it's soming got to do with
> freeing bp2.
> and something called "corruption of the heap".
>
> book* LoadBookData(unsigned *size)
> {
> FILE* fp;
> int n = 0;
> book *bp2 = NULL;
>
> //open book data file
> fp=fopen("book.bin","rb");
> if (fp == NULL)
> {
> bp2 = (book*)calloc(0, sizeof(book));
> return bp2;
> }
> <<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
> bp2 = realloc(bp2, sizeof(book));
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }
>
> //read data from file
> while(fread(bp2,sizeof(book),1,fp) == 1)
> {
> if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
> exit(ERR_FREAD); }
> bp2 = bp2 - n;
> n++;
>
> bp2 = realloc(bp2, sizeof(book));
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }
>
> bp2 = bp2 + n;
> }
>
> //close book data file
> if (fclose(fp)==EOF)
> { perror("ERROR [book.bin - close - LoadBookData()]]");
> exit(ERR_FCLOSE); }
>
> *size = n;
>
> return bp2;
> }`




Say hey.

Jim


  Réponse avec citation
Vieux 03/05/2008, 18h29   #4
Joe Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

Igal wrote:
> hay, i'm doing this program. having problem wiht realloc in the
> function that reads data structures into array (pointer - bp2), this
> happens after reading the second record. when call to realloc.
> i can't figure out what's wrong, think it's soming got to do with
> freeing bp2.
> and something called "corruption of the heap".
>
> book* LoadBookData(unsigned *size)
> {
> FILE* fp;
> int n = 0;
> book *bp2 = NULL;
>
> //open book data file
> fp=fopen("book.bin","rb");
> if (fp == NULL)
> {
> bp2 = (book*)calloc(0, sizeof(book));
> return bp2;
> }
> <<<<<<<<<<<<<<<HERE>>>>>>>>>>>>>>
> bp2 = realloc(bp2, sizeof(book));
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }
>
> //read data from file
> while(fread(bp2,sizeof(book),1,fp) == 1)
> {
> if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
> exit(ERR_FREAD); }
> bp2 = bp2 - n;
> n++;
>
> bp2 = realloc(bp2, sizeof(book));
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }
>
> bp2 = bp2 + n;
> }
>
> //close book data file
> if (fclose(fp)==EOF)
> { perror("ERROR [book.bin - close - LoadBookData()]]");
> exit(ERR_FCLOSE); }
>
> *size = n;
>
> return bp2;
> }

The two arguments to calloc are multiplied to form the size of the
allocation. calloc(0 * sizeof (book)) yields size 0.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
  Réponse avec citation
Vieux 03/05/2008, 18h39   #5
Ulrich Eckhardt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

Igal wrote:

> hay, i'm doing this program. having problem wiht realloc in the
> function that reads data structures into array (pointer - bp2), this
> happens after reading the second record. when call to realloc.
> i can't figure out what's wrong, think it's soming got to do with
> freeing bp2.
> and something called "corruption of the heap".
>
> book* LoadBookData(unsigned *size)
> {
> FILE* fp;
> int n = 0;
> book *bp2 = NULL;
>
> //open book data file
> fp=fopen("book.bin","rb");
> if (fp == NULL)
> {
> bp2 = (book*)calloc(0, sizeof(book));
> return bp2;
> }


* Don't cast calloc/malloc/realloc etc.
* Why assign to bp2 at all?
* There is no way that your code can signal an error here, i.e. the calling
code can't distinguish between a non-existant and empty file.

> bp2 = realloc(bp2, sizeof(book));
> if (bp2 == NULL) { perror("ERROR [realloc - LoadBookData()]");
> exit(ERR_REALOC); }


Here, you exit on realloc() failure, above, you return NULL when calloc()
fails. I'd suggest using some kind of xalloc().

> //read data from file
> while(fread(bp2,sizeof(book),1,fp) == 1)


Note: the binary layout of structures depends on the compiler and system. It
is for sure not easily portable to store binary dumps of structures in a
file like that.

> {
> if(ferror(fp)) { perror("ERROR [book.bin - read - LoadBookData()]");
> exit(ERR_FREAD); }
> bp2 = bp2 - n;
> n++;
>
> bp2 = realloc(bp2, sizeof(book));


Problem here: the pointer you pass to realloc() _MUST_ have been acquired by
a former call to realloc()/malloc(). Suggestion:

book tmp;
while(read_book( &tmp, fp)) {
bp2 = xrealloc( bp2, (n+1)*(sizeof *bp2));
bp2[n++] = tmp;
}

cheers

Uli

  Réponse avec citation
Vieux 04/05/2008, 00h53   #6
Igal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc


> If `bp2' doesn't point to a pointer returned by realloc, malloc or
> calloc, realloc() will produce undefined results (unless `bp2's value
> is NULL)
> But I *see* what you are trying to do, here's the actual logic:
> size_t n = 0;
> back:
> if(fread(&bp2[n], sizeof bp2[0], 1, fp) != 1) { /* handle error or EOF
> */ }
> n++;
> bp2 = realloc(bp2, n+1 * sizeof bp2[0]);
> if(bp2 == NULL) { /* ... */ }
> goto back;


thanks, this ed a lot. i just didn't know i can use bp2 just as
normal array.
  Réponse avec citation
Vieux 04/05/2008, 01h00   #7
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

On May 4, 2:53 am, Igal <igal.al...@gmail.com> wrote:
> > If `bp2' doesn't point to a pointer returned by realloc, malloc or
> > calloc, realloc() will produce undefined results (unless `bp2's value
> > is NULL)
> > But I *see* what you are trying to do, here's the actual logic:
> > size_t n = 0;
> > back:
> > if(fread(&bp2[n], sizeof bp2[0], 1, fp) != 1) { /* handle error or EOF
> > */ }
> > n++;
> > bp2 = realloc(bp2, n+1 * sizeof bp2[0]);
> > if(bp2 == NULL) { /* ... */ }
> > goto back;

>
> thanks, this ed a lot. i just didn't know i can use bp2 just as
> normal array.


Depends, in this case you can.
Read section 6 of the FAQ <http://c-faq.com/> which explains arrays/
pointers and then read the rest of the FAQ.
  Réponse avec citation
Vieux 04/05/2008, 05h51   #8
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

On Sat, 3 May 2008 09:33:17 -0700 (PDT), vippstar@gmail.com wrote:

>On May 3, 7:21 pm, Igal <igal.al...@gmail.com> wrote:
>> hay, i'm doing this program. having problem wiht realloc in the
>> function that reads data structures into array (pointer - bp2), this
>> happens after reading the second record. when call to realloc.
>> i can't figure out what's wrong, think it's soming got to do with
>> freeing bp2.
>> and something called "corruption of the heap".
>>
>> book* LoadBookData(unsigned *size)
>> {
>> FILE* fp;
>> int n = 0;
>> book *bp2 = NULL;
>>
>> //open book data file
>> fp=fopen("book.bin","rb");
>> if (fp == NULL)
>> {
>> bp2 = (book*)calloc(0, sizeof(book));
>> return bp2;

>Don't cast calloc. I'm not sure what the results of calloc(0, N) are,
>but if they are similar of malloc(), then that pointer is not really
>reliable.
>You most likely want something like this:
> return calloc(0, sizeof book);


book is a type. The parentheses are required. Other than the cast
and parentheses, your code is identical to the OP's. Surely not your
intent.



Remove del for email
  Réponse avec citation
Vieux 04/05/2008, 12h42   #9
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Sat, 3 May 2008 09:33:17 -0700 (PDT), vipps...@gmail.com wrote:
> >On May 3, 7:21 pm, Igal <igal.al...@gmail.com> wrote:
> >> hay, i'm doing this program. having problem wiht realloc in the
> >> function that reads data structures into array (pointer - bp2), this
> >> happens after reading the second record. when call to realloc.
> >> i can't figure out what's wrong, think it's soming got to do with
> >> freeing bp2.
> >> and something called "corruption of the heap".

>
> >> book* LoadBookData(unsigned *size)
> >> {
> >> FILE* fp;
> >> int n = 0;
> >> book *bp2 = NULL;

>
> >> //open book data file
> >> fp=fopen("book.bin","rb");
> >> if (fp == NULL)
> >> {
> >> bp2 = (book*)calloc(0, sizeof(book));
> >> return bp2;

> >Don't cast calloc. I'm not sure what the results of calloc(0, N) are,
> >but if they are similar of malloc(), then that pointer is not really
> >reliable.
> >You most likely want something like this:
> > return calloc(0, sizeof book);

>
> book is a type. The parentheses are required. Other than the cast
> and parentheses, your code is identical to the OP's. Surely not your
> intent.

Yes you are right, thanks for pointing that out.
return calloc(1, sizeof bp2);
`book' is confusing as a type. Maybe book_t or Book. Regardless, my
mistake.


  Réponse avec citation
Vieux 13/05/2008, 14h10   #10
Ulrich Eckhardt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

vippstar@gmail.com wrote:
> On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.com> wrote:
>> On Sat, 3 May 2008 09:33:17 -0700 (PDT), vipps...@gmail.com wrote:
>> >You most likely want something like this:
>> > return calloc(0, sizeof book);

>>
>> book is a type. The parentheses are required. Other than the cast
>> and parentheses, your code is identical to the OP's. Surely not your
>> intent.

> Yes you are right, thanks for pointing that out.
> return calloc(1, sizeof bp2);


Still wrong, bp2 is a pointer. You probably meant *bp2. However, that then
spoils the whole relation because it isn't used to initialise bp2. No, in
the elided original source, bp2 can well be removed completely, so using
sizeof (book) should work.

> `book' is confusing as a type. Maybe book_t or Book. Regardless, my
> mistake.


I beg to differ. I also don't need int_t or Integer. YMMV.

Uli

  Réponse avec citation
Vieux 15/05/2008, 19h52   #11
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: problem with realloc

Ulrich Eckhardt wrote:

> vippstar@gmail.com wrote:
>> On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.com> wrote:
>>> On Sat, 3 May 2008 09:33:17 -0700 (PDT), vipps...@gmail.com wrote:
>>> >You most likely want something like this:
>>> > return calloc(0, sizeof book);
>>>
>>> book is a type. The parentheses are required. Other than the cast
>>> and parentheses, your code is identical to the OP's. Surely not
>>> your intent.

>> Yes you are right, thanks for pointing that out.
>> return calloc(1, sizeof bp2);

>
> Still wrong, bp2 is a pointer. You probably meant *bp2. However, that
> then spoils the whole relation because it isn't used to initialise
> bp2. No, in the elided original source, bp2 can well be removed
> completely, so using sizeof (book) should work.
>
>> `book' is confusing as a type. Maybe book_t or Book. Regardless, my
>> mistake.

>
> I beg to differ. I also don't need int_t or Integer. YMMV.


Also identifiers ending with a '_t' are reserved by POSIX. A pretty wide
sweep if you ask me.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h07.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25690 seconds with 19 queries