Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc

Reply
Thread Tools

malloc

 
 
Duncan Muirhead
Guest
Posts: n/a
 
      02-22-2008
On Fri, 22 Feb 2008 00:39:44 -0500, David T. Ashley wrote:

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

Then this is even better, yes?
2') p = malloc( sizeof *p);



 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      02-22-2008
Ian Collins <(E-Mail Removed)> writes:
> 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)?


Well, p is a variable. As for *p, I suppose you could say it's a
variable that doesn't currently exist (it depends on what you mean by
"variable", a term that the standard doesn't define).

I'm skeptical that early compilers didn't support "sizeof expr"; as
someone else mentioned, it's documented in K&R1. It's certainly not
something worth worrying about for implementations still in use
outside a computer museum.

IMNSHO, the
p = malloc(sizeof *p);
form, or for allocating an array:
p = malloc(N * sizeof *p);
is clearly superior, but I don't know how often it's used. I seldom
see it outside my own code and this newsgroup.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 8:33 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > On Feb 22, 2:55 am, pete <(E-Mail Removed)> 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) );

> <snip>
> > I think this program will make things clear

>
> Sorry, not to me. What does it make clear to you?
>
> <snip>> printf("\n sizeof p = %d", sizeof(p));
> > printf("\n sizeof *p = %d", sizeof(*p));
> > printf("\n sizeof q = %d",sizeof(q));
> > printf("\n sizeof *q = %d",sizeof(*q));
> > printf("\n sizeof NULL = %d",sizeof(NULL));
> > printf("\n sizeof r = %d",sizeof(r));

>
> <snip>
>
> To print sizes with %d, cast the sizeof expression to int. This is
> safe since all these sizes will be small. Alternatively use the %zu
> format if you have a suitable printf.
>
>

AFAIK sizeof operator 's return type is size_t and the type of size_t
may vary across implementations.in most implementations i have seen
size_t is treated as unsigned int.

Am I correct ...???

any way i haven't seen a format specifier %zu but I have seen %lu


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Feb 22, 8:33 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> (E-Mail Removed) writes:
>> > On Feb 22, 2:55 am, pete <(E-Mail Removed)> 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) );

>> <snip>
>> > I think this program will make things clear

>>
>> Sorry, not to me. What does it make clear to you?
>>
>> <snip>> printf("\n sizeof p = %d", sizeof(p));
>> > printf("\n sizeof *p = %d", sizeof(*p));
>> > printf("\n sizeof q = %d",sizeof(q));
>> > printf("\n sizeof *q = %d",sizeof(*q));
>> > printf("\n sizeof NULL = %d",sizeof(NULL));
>> > printf("\n sizeof r = %d",sizeof(r));

>>
>> <snip>
>>
>> To print sizes with %d, cast the sizeof expression to int. This is
>> safe since all these sizes will be small. Alternatively use the %zu
>> format if you have a suitable printf.
>>
>>

> AFAIK sizeof operator 's return type is size_t and the type of size_t
> may vary across implementations.in most implementations i have seen
> size_t is treated as unsigned int.
>
> Am I correct ...???
>
> any way i haven't seen a format specifier %zu but I have seen %lu


%zu is new in C99. For C90 code using %lu and casting the corresponding
argument to unsigned long is the best method, as is constantly
discussed in this group.

 
Reply With Quote
 
CBFalconer
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.


Then you haven't been learning from the better C programmers. No
real C compiler has ever refused to use the variable.

--
[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
 
Willem
Guest
Posts: n/a
 
      02-23-2008
)> what is the trade off between the following two allocations
)>
)> 1) p = malloc( sizeof(sn) );
)> 2) p = malloc( sizeof(*p) );

MisterE wrote:

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

I seriously doubt that the 'guidelines' specifically tell you to use
sizeof(type) rather than sizeof(*ptr). It's more likely that whoever
wrote the original guidelines simply expressed that you should use sizeof
when allocating memory, and used sizeof(type) as an example, possibly
because he was not aware of the sizeof(*ptr) possibility.

And, of course, after that some braindead clown sees you use
'sizeof(*ptr)', proceeds to tell you "That doesn't work, because
sizeof() works on types", and when you prove to him that it does work,
he proceeds to tell you 'the company standard demands 'sizeof(type)'.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      02-23-2008
Richard wrote:
) "MisterE" <(E-Mail Removed)> writes:
)
)>> 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
)
) Then you are not a C programmer of much experience. It is infinitely
) superior for code maintenance.

'It is a lot better to do X' often does not translate to 'Most people do X'.

But, conversely this also means:

'Most people do X' does not imply 'X is better'.


In any case, a good programmer often doesn't do 'what most people do'.
Otherwise he'd be an average programmer.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Tejas Kokje
Guest
Posts: n/a
 
      03-01-2008
(E-Mail Removed) wrote:

> 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) );


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.

Tejas Kokje
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-01-2008
Tejas Kokje <(E-Mail Removed)> writes:
> (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.


There's no tradeoff as far as the compiler is concerned, but code is
written for human readers, not just for the compiler.

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


It doesn't *tell* you what the data type is; it *assumes* a certain
data type. Suppose p is declared a a pointer to sn, but suppose there's
another type called "sm", and you accidentally type:
p = malloc( sizeof(sm) );
The compiler won't detect the error.

The advantage of the second form, which I'd write as:
p = malloc(sizeof *p);
is that you don't have to know the type to know that it's correct;
it's automatically correct.

Similarly, if you write an assignment
p = q;
the assignment itself doesn't tell you what type is being assigned.
If you need to know the type, you can search for the declarations of p
and q. That doesn't mean you should write:
p = (sn*)q;
to make the type explicit.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-01-2008
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)".

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