C99 Question

  • Thread starter Vijay Kumar R Zanvar
  • Start date
V

Vijay Kumar R Zanvar

Hi,

Which section of C99 says that return value
of malloc(3) should not be casted?

Thanks.
 
M

Martin Dickopp

Vijay Kumar R Zanvar said:
Which section of C99 says that return value
of malloc(3) should not be casted?

None. The standard only defines the language, it does not advise on
good programming style.

Martin
 
L

lallous

Hello,
Richard Heathfield said:
Vijay Kumar R Zanvar wrote:


The malloc function - Just Don't Cast.
This might be silly from me, but you mean don't cast as:
int *i = (int *) malloc(sizeof(int));
?
What other solutions are there then? just assign w/o casting?
 
R

Richard Heathfield

Vijay said:
Hi,

Which section of C99 says that return value
of malloc(3) should not be casted?

The same section that says you should not microwave your cat.

Casting malloc is deprecated by the regular subscribers in this newsgroup
because:

(a) it does nothing good;
(b) it doesn't stop anything bad happening;
(c) it can (in certain circumstances in C90) actually /cause/ something bad
to happen.

Even if you're not persuaded by (a) and (b) together (which you probably
should be), (c) should be a showstopper.

The malloc function - Just Don't Cast.
 
R

Richard Bos

Vijay Kumar R Zanvar said:
Which section of C99 says that return value
of malloc(3) should not be casted?

There is no section of C99 that says that; if there were, you wouldn't
be able to do it. It's a very bad idea all the same.
In fact, it's similar to there not being any law forbidding you to shoot
yourself in the foot. You _are_ allowed to do that; but expect it to
hurt, and do not expect your insurance to pay for the medical costs.

Richard
 
R

Richard Heathfield

lallous said:
Hello,

This might be silly from me, but you mean don't cast as:
int *i = (int *) malloc(sizeof(int));
?

Yes, I mean that this cast is pointless and can be harmful.
What other solutions are there then? just assign w/o casting?

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

works for any object type T. You can omit n if it's 1, of course.

Your example would be better written as: int *i = malloc(sizeof *i);
 
P

pete

Vijay said:
Hi,

Which section of C99 says that return value
of malloc(3) should not be casted?

How about an errata from K&R2 instead ?

142(§6.5, toward the end):
The remark about casting the return value of malloc
("the proper method is to declare ... then explicitly coerce")
needs to be rewritten. The example is correct and works,
but the advice is debatable in the context of the
1988-1989 ANSI/ISO standards. It's not necessary
(given that coercion of void * to ALMOSTANYTYPE * is automatic),
and possibly harmful if malloc,
or a proxy for it, fails to be declared as returning void *.
The explicit cast can cover up an unintended error.
On the other hand, pre-ANSI, the cast was necessary,
and it is in C++ also.

http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
 
P

pete

Richard said:
Yes, I mean that this cast is pointless and can be harmful.


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

works for any object type T. You can omit n if it's 1, of course.

Your example would be better written as: int *i = malloc(sizeof *i);

Neither of your posts in this thread, mention stdlib.h.
In order to write the lines that you wrote, there has to be

#include <stdlib.h>

in the code, somewhere before those lines,
otherwise the compiler will generate a warning which,
(though the warning signifies an absence of stdlib.h to us,)
may suggest a cast, and confuse the programmer into thinking
that the return value of malloc should be cast.
And who's the programmer going to believe, clc or his compiler ?
 
L

lallous

Hello,

Richard Heathfield said:
If he is wise, he will believe clc (and then he will find out precisely
*why* his compiler is right).


I use the cast to avoid warning or when using malloc() in a C++ program.

The stdlib.h would remove the warning? How, compiler switch (like vc's
#pragma warn(disable:n))?
 
R

Richard Heathfield

pete said:
Neither of your posts in this thread, mention stdlib.h.

And who's the programmer going to believe, clc or his compiler ?

If he is wise, he will believe clc (and then he will find out precisely
*why* his compiler is right).
 
R

Richard Bos

I use the cast to avoid warning or when using malloc() in a C++ program.

- don't use malloc() in a C++ program, use new;
- don't confound C with C++, and least of all good C with good C++;
- therefore, don't apply a C++ hack-around to proper C code.
The stdlib.h would remove the warning? How, compiler switch (like vc's
#pragma warn(disable:n))?

Of course not - by the simple expedient of providing a proper
declaration for malloc() and friends. As all good C code should do for
all used functions.

Richard
 
S

Servé Lau

lallous said:
I use the cast to avoid warning or when using malloc() in a C++ program.

First, you should use new in a C++ program, and you don't have to cast new.

Second, the whole point is that if you forget to include stdlib.h and you
try to use malloc, the compiler lets malloc return int instead of void *. If
you don't cast the return value, the compiler should give an error that
there is no automatic conversion from int to pointer type. If you do cast
the return value, the compiler thinks you are saying "shut up, I know what
I'm doing" and will happily generate some code that converts an int to a
pointer. This causes big problemos on platforms where sizeof(int) != sizeof
(void *) and 64 bit platforms are coming pretty soon.
The stdlib.h would remove the warning? How, compiler switch (like vc's
#pragma warn(disable:n))?

Do not remove the warning, write correct code.
 
P

pete

lallous said:
Hello,



I use the cast to avoid warning or when using
malloc() in a C++ program.

The rumors are, that well written C++ programs
which look like C++ programs, don't use malloc.
The stdlib.h would remove the warning? How, compiler switch (like vc's
#pragma warn(disable:n))?

You need to understand the reason for the warning.

malloc returns type void*. The prototype for malloc is in stdlib.h.
If a function, like malloc, doesn't have a prototype in scope,
then the compiler will assume that the function returns type int.
If you assign an int to a pointer, you get a warning or an error.

If stdlib.h is #included, then the compiler will know that
malloc returns type void*. When you assign a void* value
to any type of pointer to an object, there is no problem, no warning.
 
L

lallous

pete said:
The rumors are, that well written C++ programs
which look like C++ programs, don't use malloc.


You need to understand the reason for the warning.

malloc returns type void*. The prototype for malloc is in stdlib.h.
If a function, like malloc, doesn't have a prototype in scope,
then the compiler will assume that the function returns type int.
Yes, I know that.
If you assign an int to a pointer, you get a warning or an error.

If stdlib.h is #included, then the compiler will know that
malloc returns type void*. When you assign a void* value
to any type of pointer to an object, there is no problem, no warning.
I know that C++ uses new, however C++ enforces type casting and that is why
I cast return value of malloc() to the desired type. C would access to
convert void* to any other pointer.

Anyway, thank you all for the info.
Elias
 
F

Flash Gordon

Hello,

This might be silly from me, but you mean don't cast as:
int *i = (int *) malloc(sizeof(int));
?
What other solutions are there then? just assign w/o casting?

Yes. The generally recommended way to use malloc around here is

#include <stdlib.h>

/* lots of stuff */

derf = malloc( N * sizeof *derf );

Where N is the number of objects you want derf to point.

If your compiler complains about the above (given appropriate
definitions of derf and N) then you are not invoking it correctly or I
have made a trypo.
 
P

pete

lallous said:
Yes, I know that.

I know that C++ uses new, however C++ enforces type
casting and that is why
I cast return value of malloc() to the desired type. C would access to
convert void* to any other pointer.

Anyway, thank you all for the info.

And so, to sum up, the big advantage of not casting,
is that the warning will alert the programmer if the code
has changed in some way such that the prototype for malloc
is no longer in scope,
which is something that really does happen in real programs.

Not having the prototype in scope, gives your code undefined behavior,
which means that it might work right in your tests
and still have problems in the field.
 
C

CBFalconer

lallous said:
.... snip ...

I use the cast to avoid warning or when using malloc() in a C++
program.

This is c.l.c. c.l.c++ is another newsgroup, dealing with another
(some say inferior) language, named C++, which is distinct from C.
 
R

Richard Bos

pete said:
And so, to sum up, the big advantage of not casting,
is that the warning will alert the programmer if the code
has changed in some way such that the prototype for malloc
is no longer in scope,

No. The _big_ advantage of not casting malloc() is that you have several
fewer useless casts in your program, just as the big advantage of not
hanging a high voltage sign on batteries is that you're not carrying
several high voltage signs for your mobile phone et cetera.
Both also have other advantages (you will be warned if you forget
<stdlib.h>; you won't be hampered when you try to use your phone), but
the main advantage is that the fewer useless warning signals, the more
attention you will pay if you really _are_ doing something awkward
involving pointers - or if you really are in danger of being
electrocuted.

Richard
 
C

CBFalconer

Richard said:
No. The _big_ advantage of not casting malloc() is that you have
several fewer useless casts in your program, just as the big
advantage of not hanging a high voltage sign on batteries is that
you're not carrying several high voltage signs for your mobile
phone et cetera. Both also have other advantages (you will be
warned if you forget <stdlib.h>; you won't be hampered when you
try to use your phone), but the main advantage is that the fewer
useless warning signals, the more attention you will pay if you
really _are_ doing something awkward involving pointers - or if
you really are in danger of being electrocuted.

And it also encourages you to treat the presence of ANY cast as an
indication of a potential problem. There are very few places they
are really necessary, with calls to variadic functions heading my
list.
 
M

Martin Ambuhl

Vijay said:
Hi,

Which section of C99 says that return value
of malloc(3) should not be casted?

None. This is a question of good programming practice, not of language
legislation.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top