473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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;
}
Jun 27 '08 #1
10 1932
On May 3, 7:21 pm, Igal <igal.al...@gmail.comwrote:
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;
Jun 27 '08 #2

"Igal" <ig********@gmail.comwrote in message
news:53**********************************@e39g2000 hsf.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
Jun 27 '08 #3
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 ---
Jun 27 '08 #4
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

Jun 27 '08 #5
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 helped a lot. i just didn't know i can use bp2 just as
normal array.
Jun 27 '08 #6
On May 4, 2:53 am, Igal <igal.al...@gmail.comwrote:
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 helped 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.
Jun 27 '08 #7
On Sat, 3 May 2008 09:33:17 -0700 (PDT), vi******@gmail.com wrote:
>On May 3, 7:21 pm, Igal <igal.al...@gmail.comwrote:
>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
Jun 27 '08 #8
On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.comwrote:
On Sat, 3 May 2008 09:33:17 -0700 (PDT), vipps...@gmail.com wrote:
On May 3, 7:21 pm, Igal <igal.al...@gmail.comwrote:
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.
Jun 27 '08 #9
vi******@gmail.com wrote:
On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.comwrote:
>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

Jun 27 '08 #10
Ulrich Eckhardt wrote:
vi******@gmail.com wrote:
>On May 4, 7:51 am, Barry Schwarz <schwa...@dqel.comwrote:
>>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.

Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
3
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD...
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
13
by: coosa | last post by:
Dear all, Using the conio implementation i wanted to create a dynamic string, whereby its size would be determined after each keyboard hit; in other words, i don't want to ask the user to...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
15
by: Igal | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.