www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/11/11:44:32

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 11 Nov 1996 10:55:57 -0500
Message-Id: <9611111555.AA01841@quasar.bloomberg.com >
To: mert0407 AT sable DOT ox DOT ac DOT uk
Cc: djgpp AT delorie DOT com
In-Reply-To: <566def$b@news.ox.ac.uk> (mert0407@sable.ox.ac.uk)
Subject: Re: array of pointers to strings, BC++, DJGPP and weird printing , behavior
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   Errors-To: postmaster AT bloomberg DOT com
   From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
   Newsgroups: comp.os.msdos.djgpp
   Date: 11 Nov 1996 05:26:39 GMT
   Organization: Oxford University, England
   Lines: 62
   References: <Pine DOT SUN DOT 3 DOT 91 DOT 961110152547 DOT 8417U-100000 AT is>
   Nntp-Posting-Host: sable.ox.ac.uk
   X-Newsreader: TIN [version 1.2 PL2]
   Dj-Gateway: from newsgroup comp.os.msdos.djgpp
   Content-Type: text
   Content-Length: 2485

   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 -

OK you are wrong.  Warnings be damned!  I will always maintain that the
explicit cast is good coding style because it documents what you wanted to do.
The warning just tells you that you forgot to include the header file!  It
tells no one anything about whether you meant to allocate a pointer to int or
an int and got the variable declaration wrong.  Six months later or when
someone else needs to use or modify your code you will be glad for the casts.
And while I do not subscribe to comp.lang.c, you can quote me there and I will
go defend myself if needed.  LONG LIVE EXPLICIT TYPE CASTS!

   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++).

Anyone coding professionally, and I don't mean in their living room for money,
on large teams on that news group?  I've been coding in many languages for 15
years and I use explicit casts, as well as other code documentation tools,
extensively when I code in "C" or C++.  Personally, I long for Modula2 where
the typecasts were REQUIRED!  

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


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