malloc

D

Duncan Muirhead

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

Keith Thompson

Ian Collins said:
Yet more nonsense, verging on bollocks. 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.
 
A

aarklon

(e-mail address removed) wrote:
(e-mail address 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) );
I think this program will make things clear

Sorry, not to me. What does it make clear to you?

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
 
S

santosh

(e-mail address removed) wrote:
(e-mail address 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) );
I think this program will make things clear

Sorry, not to me. What does it make clear to you?

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

CBFalconer

MisterE said:
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.
 
W

Willem

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

Willem

Richard 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
)
) 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
 
T

Tejas Kokje

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
 
K

Keith Thompson

Tejas Kokje said:
(e-mail address 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.
 
P

pete

Tejas said:
(e-mail address removed) wrote:

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

Tejas Kokje

pete said:
"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
 
P

pete

Tejas said:
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.
 
W

William Pursell

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

David Thompson

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 :cool:
Saves 6 in the definition, but no change in the usages.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Function giving exception 9
malloc and functions 24
Lexical Analysis on C++ 1
simplebinary tree 7
malloc 40
Infinite loop problem in linklist 7
malloc and maximum size 56
Naive Custom Malloc Implementation 8

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top