www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/11/03:57:28

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: array of pointers to strings, BC++, DJGPP and weird printing , behavior
Date: 11 Nov 1996 05:26:39 GMT
Organization: Oxford University, England
Lines: 62
Message-ID: <566def$b@news.ox.ac.uk>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 961110152547 DOT 8417U-100000 AT is>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Eli Zaretskii (eliz AT is DOT elta DOT co DOT il) wrote:

: On Sat, 9 Nov 1996, Fletcher Matlock wrote:

: > : > 	KeyWords = (char **) malloc ( sizeof(char) );
: > I've never seen this     ^^ before, what did I miss and when?

: This is the proper way to allocate memory for an array of pointers to 
: char.  `malloc' returns a void pointer, and you should always cast it to 
: a proper type before using the result.

To cast or not to cast... that is the question. Whether 'tis more
important to get warnings of missing headers in C than to write
warningless code in C++. 

Please correct me if I'm wrong, but -

In C:

If you forget to #incldue <stdlib.h>, the compiler will not warn you when
you implicitly define the function int malloc (...), and will then put a
cast in from integer result to (xxx *).  I believe this works on gcc, but
whether it is portable is another matter. The point is, you are
suppressing an important warning.

If you leave out the cast, it will complain about an implicit cast from
integer to pointer, which indicates the problem.

If you #include the header, it works both ways with no warnings.

In C++:

If you put an explicit cast and forget the library, it will warn you
about the implicit declaration of 'int malloc(...). If you leave out the
cast, it will also complain about the implicit cast.

If you #include <stdlib.h> and make the cast explicit, it works, of
course. If you don't make the cast explicit, it gives the warning about
ANSI C++ forbidding the implicit conversion from void * in assignment.

Conclusion: In C, the explicit cast is a bad idea because it can mask a
warning of a potentially serious error. In C++, the cast is necessary to
remove an ANSI warning.

Is this correct, or am I completely misled? If so, why did ANSI pass the
warning, because the first idea seems better to me?

I got flamed in comp.lang.c for posting a code fragment which used the
explicit cast, which is the way I have always written it (mainly because
I usually use C++).

--
George Foot

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ |""""""""""""""""""""""|
>Email: george DOT foot AT merton DOT ox DOT ac DOT uk      < |Snail Mail:(*)        |
>   or: mert0407 AT sable DOT ox DOT ac DOT uk          < | _     George Foot    |
>  Web: http://users.ox.ac.uk/~mert0407/ < |(@)    Merton College |
>  Ftp: mc31.merton.ox.ac.uk (#)         < |~~~~   Oxford OX1 4JD |
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ |______________________|
(#) Theoretically...    ||    (*) Please allow 28 days for delivery

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019