Re: problem with realloc



On May 4, 7:51 am, Barry Schwarz <schwa...@xxxxxxxx> wrote:
On Sat, 3 May 2008 09:33:17 -0700 (PDT), vipps...@xxxxxxxxx wrote:
On May 3, 7:21 pm, Igal <igal.al...@xxxxxxxxx> 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.


.



Relevant Pages

  • Re: problem with realloc
    ... return calloc(0, sizeof book); ... and parentheses, your code is identical to the OP's. ... Still wrong, bp2 is a pointer. ... Also identifiers ending with a '_t' are reserved by POSIX. ...
    (comp.lang.c)
  • Re: problem with realloc
    ... return calloc(0, sizeof book); ... Other than the cast ... and parentheses, your code is identical to the OP's. ... Still wrong, bp2 is a pointer. ...
    (comp.lang.c)
  • Re: problem with realloc
    ... function that reads data structures into array (pointer - bp2), ... when call to realloc. ... freeing bp2. ... That's actually a malloc(), since bp2 was initialized to NULL before, ...
    (comp.lang.c)
  • Re: problem with realloc
    ... function that reads data structures into array (pointer - bp2), ... freeing bp2. ... and parentheses, your code is identical to the OP's. ...
    (comp.lang.c)
  • Re: problem with realloc
    ... function that reads data structures into array, ... freeing bp2. ... Here, you exit on realloc() failure, above, you return NULL when calloc ... the pointer you pass to realloc_MUST_ have been acquired by ...
    (comp.lang.c)