Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc

Reply
Thread Tools

malloc

 
 
Tejas Kokje
Guest
Posts: n/a
 
      03-02-2008
pete wrote:

> Tejas Kokje wrote:
>>
>> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>
>> > what is the trade off between the following two allocations
>> >
>> > 1) p = malloc( sizeof(sn) );
>> > 2) p = malloc( sizeof(*p) );

>>
>> I don't think there is any tradeoff as compiler
>> already knows pointer type of p and that *p is of type sn.
>> So sizeof operator can use sn or *p.
>>
>> However, I would prefer first syntax as it just tell me
>> exactly what data type is used in sizeof.
>> In second case, I will have to tagjump to know type of p.

>
> "Type (sn)", doesn't really mean a lot more to me than "type (*p)".
>


It certainly means something in my book. It certainly saves me extra step to
find type of *p, if I want to know how much memory should be allocated by
malloc.

Tejas Kokje
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      03-02-2008
Tejas Kokje wrote:
>
> pete wrote:
>
> > Tejas Kokje wrote:
> >>
> >> (E-Mail Removed) wrote:

> >
> >> > what is the trade off between the following two allocations
> >> >
> >> > 1) p = malloc( sizeof(sn) );
> >> > 2) p = malloc( sizeof(*p) );
> >>
> >> I don't think there is any tradeoff as compiler
> >> already knows pointer type of p and that *p is of type sn.
> >> So sizeof operator can use sn or *p.
> >>
> >> However, I would prefer first syntax as it just tell me
> >> exactly what data type is used in sizeof.
> >> In second case, I will have to tagjump to know type of p.

> >
> > "Type (sn)", doesn't really mean a lot more to me than "type (*p)".
> >

>
> It certainly means something in my book.
> It certainly saves me extra step to find type of *p,
> if I want to know how much memory should be allocated by malloc.


I'm not following you.

You're saying that there could be some situation upon seeing
p = malloc(sizeof sn);
where I won't need to know what type p points to,
but that that knowing how much (sizeof sn) is,
will be useful to me.
That seems unlikely.


p = malloc(sizeof *p)
tells me that enough memory has been requested
for one object of the type that p points to,
without me having to look up anything.

--
pete
 
Reply With Quote
 
 
 
 
William Pursell
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 7:20 am, Tejas Kokje <(E-Mail Removed)> wrote:
> pete wrote:
> > Tejas Kokje wrote:

>
> >> (E-Mail Removed) wrote:

>
> >> > what is the trade off between the following two allocations

>
> >> > 1) p = malloc( sizeof(sn) );
> >> > 2) p = malloc( sizeof(*p) );

>
> >> I don't think there is any tradeoff as compiler
> >> already knows pointer type of p and that *p is of type sn.
> >> So sizeof operator can use sn or *p.

>
> >> However, I would prefer first syntax as it just tell me
> >> exactly what data type is used in sizeof.
> >> In second case, I will have to tagjump to know type of p.

>
> > "Type (sn)", doesn't really mean a lot more to me than "type (*p)".

>
> It certainly means something in my book. It certainly saves me extra step to
> find type of *p, if I want to know how much memory should be allocated by
> malloc.
>


It does not save you a step unless you are very careless. If
you don't know the type of p, then you don't know if the
malloc is correct, since it might not be allocating
enough space. If p is not of type sn, but something
bigger, then you have a problem. If the allocation
is:

p = malloc( sizeof *p );

then you know it is correct without having to check the
declaration of p. If it is:

p = malloc( sizeof(T));

then you must check the declaration to make sure that
p is of type T. So the second form requires an extra
step, but the first does not. Many will argue that
this justifies the cast, and recommend:

p = (T) malloc( sizeof(T));

which adds additional complexity and many more steps when
the type of p is changed from T to TT.

 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      03-02-2008
On Fri, 22 Feb 2008 14:02:48 +0100, "Joachim Schmitz"
<(E-Mail Removed)> wrote:

> "David T. Ashley" <(E-Mail Removed)> schrieb im Newsbeitrag
> news(E-Mail Removed) ...

<snip><sarcastically>
> > I actually would improve on the second form somewhat.
> >
> > #define M(ptr) ptr=malloc(sizeof(*ptr));
> >
> > M(ptr)
> >
> > That brings the whole affair down to 6 keystrokes if one does it
> > repetitively -- quite an improvement.


6 keystrokes PLUS 2 SHIFTS (which you were counting upthread) on each
usage IF your pointers are all named ptr -- but at most one per scope
can be, and even that may be a poor choice for other reasons.

> Further improvement:
> #define M(p) p=malloc(sizeof(*p));
>
> Saves 6 keystrokes first time and additional 2 on every repitition
>

Saves 6 in the definition, but no change in the usages.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments