🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.os2.programmer
Article #1229

Re: C Set/2 Question in malloc

#1229
From: dave
Date: Fri, 28 Aug 1992 19:37
24 lines
1502 bytes
            <59958@mimsy.umd.edu>

In <59958@mimsy.umd.edu> Jesus Rodriguez writes:
> The problem is not malloc.h or the need for stdlib.h  it is as someone
> posted before.
>
>>>>  if  (( array= malloc(50 * sizeof(long)))!= NULL)
>
> this line should read
> if (( array = (long *) malloc(50 * sizeof(long))) != NULL)
>               ^^^^^^^ this is what needs to be added to the line.

The problem is too <malloc.h> and the need to include <stdlib.h>.  malloc
is defined as returning a (void *), which is assignment compatible with
any data pointer ("pointer to an object type or an incomplete type" in
ANSIspeak) without casting.  If you have to cast the return type from
malloc() to get it past an ANSI-conformant compiler, then you know that
there is a problem in your code.  If the original poster changes his
code to include <stdlib.h> instead of <malloc.h>, it will be portable to
any platform which has an ANSI-compliant compiler.  Your solution will
fail, for example, on the AS/400 and in some cases in large-model 16-bit
DOS or OS/2.

dave
Technical Details
Message-ID: <19920828.124723.315@almaden.ibm.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!vnet.ibm.com
References: <1992Aug28.154836.12867@natinst.com> <19920828.105606.807@almaden.ibm.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!vnet.ibm.com
From: dmm@vnet.ibm.com (dave)
Message-ID: <19920828.124723.315@almaden.ibm.com>
Date: Fri, 28 Aug 92 15:37:19 EDT
Newsgroups: comp.os.os2.programmer
Subject: Re: C Set/2 Question in malloc
Organization: IBM Canada Lab
News-Software: UReply 3.0
X-X-From: dmm@vnet.ibm.com (Dave Mooney)
References: <1992Aug28.154836.12867@natinst.com> <19920828.105606.807@almaden.ibm.com>
Lines: 24