home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / os2 / programm / 4516 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  1.4 KB

  1. Path: sparky!uunet!vnet.ibm.com
  2. From: dmm@vnet.ibm.com (dave)
  3. Message-ID: <19920828.124723.315@almaden.ibm.com>
  4. Date: Fri, 28 Aug 92 15:37:19 EDT
  5. Newsgroups: comp.os.os2.programmer
  6. Subject: Re: C Set/2 Question in malloc
  7. Organization: IBM Canada Lab
  8. News-Software: UReply 3.0
  9. X-X-From: dmm@vnet.ibm.com (Dave Mooney)
  10. References: <1992Aug28.154836.12867@natinst.com> <19920828.105606.807@almaden.ibm.com>
  11. Lines: 24
  12.  
  13.             <59958@mimsy.umd.edu>
  14.  
  15. In <59958@mimsy.umd.edu> Jesus Rodriguez writes:
  16. > The problem is not malloc.h or the need for stdlib.h  it is as someone
  17. > posted before.
  18. >
  19. >>>>  if  (( array= malloc(50 * sizeof(long)))!= NULL)
  20. >
  21. > this line should read
  22. > if (( array = (long *) malloc(50 * sizeof(long))) != NULL)
  23. >               ^^^^^^^ this is what needs to be added to the line.
  24.  
  25. The problem is too <malloc.h> and the need to include <stdlib.h>.  malloc
  26. is defined as returning a (void *), which is assignment compatible with
  27. any data pointer ("pointer to an object type or an incomplete type" in
  28. ANSIspeak) without casting.  If you have to cast the return type from
  29. malloc() to get it past an ANSI-conformant compiler, then you know that
  30. there is a problem in your code.  If the original poster changes his
  31. code to include <stdlib.h> instead of <malloc.h>, it will be portable to
  32. any platform which has an ANSI-compliant compiler.  Your solution will
  33. fail, for example, on the AS/400 and in some cases in large-model 16-bit
  34. DOS or OS/2.
  35.  
  36. dave
  37.