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 9 of 16 Topic 26120 of 26822
Post > Topic >>

Re: realloc problem, corrupt last item

by Kenneth Brody <kenbrody@[EMAIL PROTECTED] > May 8, 2008 at 10:56 AM

Igal wrote:
[...]
>         bp = (book*)realloc(bp, sizeof(book));

Here, you allocate room for one book.

I assume you really want:

          bp = realloc(bp, sizeof(*bp) * n);

which will allocate room for n books.

[...]
>         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;

Unless n==1, all of the above invoke UB by acessing memory
that does not belong to bp.

[...]

-- 
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        | www.hvcomputer.com | #include              |
| kenbrody/at\spamcop.net | www.fptech.com     |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@[EMAIL PROTECTED]
>
 




 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 Wed Jul 9 1:32:34 CDT 2008.