V
Vijay Kumar R Zanvar
Hi,
Which section of C99 says that return value
of malloc(3) should not be casted?
Thanks.
Which section of C99 says that return value
of malloc(3) should not be casted?
Thanks.
Vijay Kumar R Zanvar said:Which section of C99 says that return value
of malloc(3) should not be casted?
This might be silly from me, but you mean don't cast as:Richard Heathfield said:Vijay Kumar R Zanvar wrote:
The malloc function - Just Don't Cast.
Vijay said:Hi,
Which section of C99 says that return value
of malloc(3) should not be casted?
Vijay Kumar R Zanvar said:Which section of C99 says that return value
of malloc(3) should not be casted?
lallous said: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?
Vijay said:Hi,
Which section of C99 says that return value
of malloc(3) should not be casted?
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);
Richard Heathfield said:If he is wise, he will believe clc (and then he will find out precisely
*why* his compiler is right).
pete said:Neither of your posts in this thread, mention stdlib.h.
And who's the programmer going to believe, clc or his compiler ?
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))?
lallous said: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))?
lallous said:Hello,
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))?
Yes, I know that.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.
I know that C++ uses new, however C++ enforces type casting and that is whyIf 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.
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?
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.
lallous said:.... snip ...
I use the cast to avoid warning or when using malloc() in a C++
program.
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,
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.
Vijay said:Hi,
Which section of C99 says that return value
of malloc(3) should not be casted?
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.