Re: C or C++ for FOSS/linux?



Hadron <hadronquark@xxxxxxxxx> wrote:
"Chris F.A. Johnson" <cfajohnson@xxxxxxxxx> writes:
On 2006-12-29, Sebastian 'lunar' Wiesner wrote:
Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> typed

[snip]

int *i = (int *) malloc (100*sizeof(int));

I don't know much of C programming, but that is known to me as the way
you allocate an array of 100 integers on the heap in C.

Wrong; it's:

int *i = malloc (100*sizeof(int));

Even better, make that

int *i = malloc( 100 * sizeof *i );

to avoid two changes if you need to change the type of 'i', one of
wich you can easily forget;-)

You should not cast the value returned by malloc; that can mask
errors.

I have heard this before : but what errors can it mask?

Casting the return value of malloc() keeps the compiler from
complaining if you forgot to include <stdlib.h> where the
prototype for malloc() is. That, in turn, will force the
compiler to assume that malloc() returns an int and convert
the return value to an int before casting it back, as requested
by the explicit cast, to a pointer. This normally isn't an
obvious problem on architectures where an int has the same size
as a pointer and where data and addresses get returned via the
same registers (like i368) but on other architectures this can
lead to strange and hard to find bugs...

Casting the return value of malloc() is only necessary if you
compile with a C++ compiler - it isn't allowed to do it auto-
matically.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: what will happen after i use free()???
    ... Inserting a cast before malloc is not needed, ... 56 bits while int is only 48 bits. ... So casting that value requires some ... Use cast only if you are absolutely sure that yor really needs the ...
    (comp.lang.c)
  • Re: gcc knows about malloc()
    ... speaking the resultant problems aren't caused by casting the ... the return value is assumed to be an int. ... Your last sentence has no intelligible meaning, since there is no way that castint that int to a pointer helps at all. ... it is malloc's *return value* that is cast, ...
    (comp.lang.c)
  • Re: Casting the return value of malloc() ?
    ... This is a FAQ ... FAQ 7.7b "What's wrong with casting malloc's return value?" ... C++ requires a cast on the return of malloc(). ... including stdlib.h the return value is int (or int * cant remember ...
    (comp.lang.c)
  • Re: what will happen after i use free()???
    ... Using malloc() without a declaration will cause undefined behaviour, ... but casting the result of mallocwill not. ... malloc has given back to the caller -> undefined behavior is ... Conversion from int to pointer is not itself undefined, ...
    (comp.lang.c)
  • Re: Casting return of malloc
    ... Not casting malloc can also mask undefined behaviour, ... "FWIW, I would advise most C programmers *not* to cast malloc calls, ... "1) Style rules should be developed by considering all applicable ...
    (comp.lang.c)