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 11 of 16 Topic 26120 of 26977
Post > Topic >>

Re: realloc problem, corrupt last item

by Eric Sosman <Eric.Sosman@[EMAIL PROTECTED] > May 8, 2008 at 01:55 PM

K. Jennings wrote:
> On Thu, 08 May 2008 14:27:23 +0000, Richard Heathfield wrote:
> 
>> Igal said:
>>
>>> 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
>>> pointer
>>> bp = (book*)realloc(bp, sizeof(book)); ...
>>> }
>> You have not provided sufficient information for me to help you
>> effectively. On general principles, lose the cast, replace sizeof(book)
>> with sizeof *bp, 
> 
> 	Why? Under what cir***stances they would not return the same 
> value?

     When bp turns out not to be a book* but a book** or a
booklet* or a Book* or ...

     Suppose that the compiler raises no objections to

	ptr = malloc(sizeof(struct messagedetail));

Can you tell whether the malloc() call requests the correct
amount of memory for ptr to point at?  Not until you scroll
around for a while to find the declaration of ptr to make
sure it's not a `struct messagedigest*' instead.  But if
it's written as

	ptr = malloc(sizeof *ptr);

.... you can tell at a glance that it's correct.

-- 
Eric.Sosman@[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 Sat Jul 26 3:52:30 CDT 2008.