TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Adam Fitzpatrick
from: David Nugent
date: 1996-07-02 01:00:36
subject: Find a word

> FA> char *txt;char *pat;char *ptr;
 > FA> txt = (char*) malloc(strlen(buf)+1);
 > FA> pat = (char*) malloc(strlen(wrd)+1);

 > DN> Just out of interest, why cast the return from malloc()? If it is
 > DN> because it has no prototype, then you should do the /correct/ thing
 > DN> and include stdlib.h.

 > Eh? I thought it was because malloc returns a void*, and
 > most compilers will give you a warning because a char* is
 > different to a void*.

A C++ compiler is required to *issue an error* (not just a warning), but a
C compiler is broken if it needs the cast at all.

What I find a crying shame is that many programmers don't seem to know that
C and C++ are two are different languages with some quite distinct and
different syntax rules. What is even worse is that those same people often
do use malloc() in C++, which is both silly and unnecessary. C++ has its
own type-smart in-built allocators, and even in the extremely rare case
where you need a typeless block of data, you can always new char[size]
which makes more sense anyway.

Ergo, if you're a programmer worth your salt using malloc(), you must be
writing C, therefore you don't need to type-cast the pointer it returns.

--- MaltEd/2 1.0.b6
* Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 107 348 360 633/371 634/388 396
SEEN-BY: 635/301 502 503 506 544 639/252 711/401 409 410 413 430 808 809 932
SEEN-BY: 711/934 712/515 713/888 714/906 800/1
@PATH: 632/348 635/503 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.