Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: realloc pro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 16 Topic 26120 of 26977
Post > Topic >>

Re: realloc problem, corrupt last item

by Igal <igal.alkon@[EMAIL PROTECTED] > May 8, 2008 at 07:33 AM

> You have not provided sufficient information for me to help you
> effectively. On general principles, lose the cast, replace sizeof(book)
> with sizeof *bp, use a temp to catch the result of realloc in case it
> fails and returns NULL, and make sure you have #included <stdlib.h>.
Note
> that bp is a copy of the calling function's pointer; the original will
not
> be updated automatically, so if you want to keep the new value you must
> provide a mechanism for the caller to assign this new value to the
> original pointer.

here's the full function.
i call it like this:

bp = AddBook(bp, &BOOK_SIZE);

/*#############################
# Add Book Function           #
#############################*/
book* AddBook(book *bp, unsigned *size)
{
	book temp;
	unsigned n = *size; //n = size of book array
	bool check = FALSE;
	char str_cn[80];
	int i;

	printf("%d", (int)(sizeof(bp)/sizeof(bp[0])));

	n++;
	bp = (book*)realloc(bp, sizeof(book));
	if (bp == NULL) { perror("ERROR [realloc - AddBook()]");
exit(ERR_REALOC); }


	printf("\n[Add Book to Catalog]\n");

	//get catalog number
	while(check != TRUE)
	{
		printf("Book Catalog Number: ");
		_flushall();
		gets(str_cn);

		//check if string is longer then 9 digits. if longer then error.
		if(strlen(str_cn) > 9) { check = FALSE; printf("[E] catalog number
too long, 9 digit max.\n"); continue; }

		//check each letter in string if a digit, if all are then check =
TRUE, else check = FALSE
		i=0;
		while(str_cn[i])
		{
			if(isdigit(str_cn[i])) { check = TRUE; i++; continue; }
			else { check = FALSE; printf("[E] catalog number not valid, try
again.\n"); break; }
		}

		if(check == TRUE) { temp.cn = atol(str_cn); } //if all OK, put value
in str_cn
	}
	check = FALSE;

	... here i get data into temp, and check validation ...

	*size = n;

	bp[n-1].cn = temp.cn;
	bp[n-1].Units = temp.Units;
	bp[n-1].Year = temp.Year;
	bp[n-1].Price.CostPrice = temp.Price.CostPrice;
	bp[n-1].Price.RetailPrice = temp.Price.RetailPrice;

	strcpy(bp[n-1].Publisher, temp.Publisher);
	strcpy(bp[n-1].BookName, temp.BookName);
	strcpy(bp[n-1].Author.Author1, temp.Author.Author1);
	strcpy(bp[n-1].Author.Author2, temp.Author.Author2);
	strcpy(bp[n-1].Author.Author3, temp.Author.Author3);

	return bp;
}
 




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

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:57:26 CDT 2008.