Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc

Reply
Thread Tools

malloc

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      02-21-2008
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) );





 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-21-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> [... brutal context snippage for didactic purposes ...]
>
> what is the trade off between the following two allocations
>
> 1) p = malloc( sizeof(sn) );
> 2) p = malloc( sizeof(*p) );


Using only the context available in this reply -- that
is, looking only at the two lines of code in the question
and not referring back to the declaration of `p' in your
question -- which line is easier to check for correctness?

Q.E.D.

--
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 11:48 pm, Eric Sosman <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > [... brutal context snippage for didactic purposes ...]

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

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

>
> Using only the context available in this reply -- that
> is, looking only at the two lines of code in the question
> and not referring back to the declaration of `p' in your
> question -- which line is easier to check for correctness?
>
> Q.E.D.


Generally in programs involving linked lists i have seen the latter
one being used most often.

now p is simply a pointer variable, if by
accident if p points to NULL , p = malloc( sizeof(*p) ); will not
give correct allocation isn't it....??????


 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-21-2008
(E-Mail Removed) wrote:
>
> On Feb 21, 11:48 pm, Eric Sosman <(E-Mail Removed)> wrote:
> > (E-Mail Removed) wrote:
> > > [... brutal context snippage for didactic purposes ...]

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

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

> >
> > Using only the context available in this reply -- that
> > is, looking only at the two lines of code in the question
> > and not referring back to the declaration of `p' in your
> > question -- which line is easier to check for correctness?
> >
> > Q.E.D.

>
> Generally in programs involving linked lists i have seen the latter
> one being used most often.
>
> now p is simply a pointer variable, if by
> accident if p points to NULL , p = malloc( sizeof(*p) ); will not
> give correct allocation isn't it....??????


sizeof doesn't evaluate its operand.
(sizeof *p) is a compile time constant,
p doesn't even have to be initialized.

If p points to type int,
then you have all the information that you need,
in order to determine that (sizeof *p) equals (sizeof(int)).
The value of p doesn't matter in that case.

--
pete
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-21-2008
pete wrote:
> (E-Mail Removed) wrote:
>> On Feb 21, 11:48 pm, Eric Sosman <(E-Mail Removed)> wrote:
>>> (E-Mail Removed) wrote:
>>>> [... brutal context snippage for didactic purposes ...]
>>>> what is the trade off between the following two allocations
>>>> 1) p = malloc( sizeof(sn) );
>>>> 2) p = malloc( sizeof(*p) );
>>> Using only the context available in this reply -- that
>>> is, looking only at the two lines of code in the question
>>> and not referring back to the declaration of `p' in your
>>> question -- which line is easier to check for correctness?
>>>
>>> Q.E.D.

>> Generally in programs involving linked lists i have seen the latter
>> one being used most often.
>>
>> now p is simply a pointer variable, if by
>> accident if p points to NULL , p = malloc( sizeof(*p) ); will not
>> give correct allocation isn't it....??????

>
> sizeof doesn't evaluate its operand.
> (sizeof *p) is a compile time constant,
> p doesn't even have to be initialized.
>
> If p points to type int,
> then you have all the information that you need,
> in order to determine that (sizeof *p) equals (sizeof(int)).
> The value of p doesn't matter in that case.


... and the reason malloc(sizeof *p) is preferred is that
you don't need to worry that you might accidentally write the
wrong type. You don't need to go back to the declaration of
p to discover what type to write, nor to check that the size
you wrote is the proper one.

Mismatches can be particularly insidious and hard to
catch. You'll see something like

int32_t *ptr;
/* 193 lines later: */
ptr = malloc(N * sizeof(int));

.... and it may work just fine on the platform where you wrote
and tested the code, where int32_t and int happen to have the
same size. But then you move the code to another platform
where int is only 16 bits wide, and suddenly the allocation
is no longer big enough and you start getting mysterious bugs.

A particularly dangerous form occurs when there are many
related types with similar names:

typedef struct { ... } MessageHeader;
typedef struct { ... } MessagePayload;
typedef struct { ... } MessageSummary;
...
MessageHeader *hdr;
MessagePayload *pay;
MessageSummary *sum;
...
hdr = malloc(sizeof(MessageHeader));
pay = malloc(sizeof(MessageHeader));
sum = malloc(sizeof(MessageSummary));

.... and as before, with the quirks of structure padding and
so on, it might actually work for a quite a while before
the error is discovered.

Using the `ptr = malloc(count * sizeof *ptr)' form (the
multiplication can be omitted if the count is known to be 1)
avoids such pitfalls. It doesn't guarantee that there are
no errors -- maybe `count' should have been `(count + 1)'
or some such -- but at least you're allocating "things" of
the proper size.

--
(E-Mail Removed)
 
Reply With Quote
 
David T. Ashley
Guest
Posts: n/a
 
      02-22-2008
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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) );


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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-22-2008
"David T. Ashley" wrote:
> <(E-Mail Removed)> wrote in message
>
>> suppose i am having a structure as follows
>>
>> typedef struct node {
>> int n;
>> struct node * next;
>> }sn;
>>
>> sn *p;
>>

.... snip ...
>>
>> 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).


True.

>
> 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.


Utter nonsense. See some of the other answers, including Sosman.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
David T. Ashley
Guest
Posts: n/a
 
      02-22-2008
"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "David T. Ashley" wrote:
>> <(E-Mail Removed)> wrote in message
>>
>>> suppose i am having a structure as follows
>>>
>>> typedef struct node {
>>> int n;
>>> struct node * next;
>>> }sn;
>>>
>>> sn *p;
>>>

> ... snip ...
>>>
>>> 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).

>
> True.
>
>>
>> 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.

>
> Utter nonsense. See some of the other answers, including Sosman.


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.

---------

All kidding and bad humor aside ...

The superior form is the one that minimizes the probability of human error.
The second form is superior because it localizes the search to one line of
code.

 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      02-22-2008


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


I have seldom seen people use *p. Normally you use only the type definition
itself and try not to use a variable at all. I think this is because some
early compilers would only allow type definitions. All the 'guidelines' at
companies I have coded for always ask to use type definitions only, never
variables.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-22-2008
MisterE wrote:
>> what is the trade off between the following two allocations
>>
>> 1) p = malloc( sizeof(sn) );
>> 2) p = malloc( sizeof(*p) );

>
> I have seldom seen people use *p. Normally you use only the type definition
> itself and try not to use a variable at all. I think this is because some
> early compilers would only allow type definitions. All the 'guidelines' at
> companies I have coded for always ask to use type definitions only, never
> variables.
>

Yet more nonsense, verging on *******s. Where is the variable in
sizeof(*p)?

--
Ian Collins.
 
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