Re: malloc
On Fri, 22 Feb 2008 00:39:44 -0500, David T. Ashley wrote:
Then this is even better, yes?
2') p = malloc( sizeof *p);
On Fri, 22 Feb 2008 00:39:44 -0500, David T. Ashley wrote:
<aarklon@gmail. comwrote in message
news:02ce42ce-92fe-4bdc-85d5-6d4f816bbdc2@u6 9g2000hse.googl egroups.com...
>
The compiler treats each of these two identically, and will simply call
malloc with a constant (the same constant in either case).
>
The first form is 33% better than the first. The reason is that in the
second form, you normally have to use the SHIFT key on your keyboard, for 3
keystrokes. For the first form, only two keystrokes. If you had to type
the malloc() statement several million times, the second form may put more
wear on your keyboard and fingers.
news:02ce42ce-92fe-4bdc-85d5-6d4f816bbdc2@u6 9g2000hse.googl egroups.com...
>Hi all,
>>
>suppose i am having a structure as follows
>>
>typedef struct node
>{
> int n;
> struct node * next;
>}sn;
>>
>sn *p;
>>
>now which of the following is the correct way to allocate memory and
>why ?
>>
> OR
>>
>what is the trade off between the following two allocations
>>
>1) p = malloc( sizeof(sn) );
>2) p = malloc( sizeof(*p) );
>>
>suppose i am having a structure as follows
>>
>typedef struct node
>{
> int n;
> struct node * next;
>}sn;
>>
>sn *p;
>>
>now which of the following is the correct way to allocate memory and
>why ?
>>
> OR
>>
>what is the trade off between the following two allocations
>>
>1) p = malloc( sizeof(sn) );
>2) p = malloc( sizeof(*p) );
The compiler treats each of these two identically, and will simply call
malloc with a constant (the same constant in either case).
>
The first form is 33% better than the first. The reason is that in the
second form, you normally have to use the SHIFT key on your keyboard, for 3
keystrokes. For the first form, only two keystrokes. If you had to type
the malloc() statement several million times, the second form may put more
wear on your keyboard and fingers.
2') p = malloc( sizeof *p);
Comment