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

  1. Xref: sparky comp.os.os2.programmer:4352 comp.lang.c:12592
  2. Path: sparky!uunet!kithrup!hoptoad!decwrl!elroy.jpl.nasa.gov!swrinde!gatech!rutgers!njitgw.njit.edu!hertz.njit.edu!dic5340
  3. From: dic5340@hertz.njit.edu (David Charlap)
  4. Newsgroups: comp.os.os2.programmer,comp.lang.c
  5. Subject: Re: malloc causes a segmentation violation!?
  6. Message-ID: <1992Aug20.211646.14022@njitgw.njit.edu>
  7. Date: 20 Aug 92 21:16:46 GMT
  8. References: <1992Aug19.155648.6210@funet.fi> <1992Aug19.171317.9312@bnr.ca> <chuckb.714275722@milton>
  9. Sender: news@njit.edu
  10. Organization: New Jersey Institute of Technology, Newark, N.J.
  11. Lines: 31
  12. Nntp-Posting-Host: hertz.njit.edu
  13.  
  14. In article <chuckb.714275722@milton> chuckb@milton.u.washington.edu (Chuck Bass) writes:
  15. >I am wondering if this is common among people who program in C.  I
  16. >haven't been programing in multiple environments (TurboC and an SGI
  17. >workstation only).  I write code that runs on both and do *lots* of
  18. >dynamic memory allocation.  Is this a bad habit, terrible habit, good
  19. >habit?  It seems to me that a general program should be able to
  20. >adjust to the required size dynamically.  
  21.  
  22. Bugs in the compiler/library aside, C programs can and should use
  23. malloc().  Malloc, free and realloc are all defined by K&R in their
  24. ANSI-C book.  It's part of <stdlib.h>
  25.  
  26. C++ programs, however, should not.  They should use the "new" operator
  27. for dynamically allocating and deallocating storage.
  28.  
  29. For instance:
  30.  
  31. To allocate an integer:
  32.         pInteger = new int;
  33.  
  34. To allocate space for 100 integers:
  35.         pInteger = new int[100];
  36.  
  37.  
  38. and so on.
  39.  
  40. -- 
  41.    |)  David Charlap           "I don't even represent myself
  42.   /|_  dic5340@hertz.njit.edu   sometimes so NJIT is right out!.
  43.  ((|,)
  44.   ~|~  Hi! I am a .signature virus, copy me into your .signature file.
  45.