home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8976 < prev    next >
Encoding:
Text File  |  1992-09-01  |  2.7 KB  |  89 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!world!levin
  3. From: levin@world.std.com (Levin F Magruder)
  4. Subject: Questions about malloc and free and memory models
  5. Message-ID: <BtxLAx.E1z@world.std.com>
  6. Organization: The World Public Access UNIX, Brookline, MA
  7. Date: Wed, 2 Sep 1992 03:20:56 GMT
  8. Lines: 79
  9.  
  10. I'm posting here because I think my problems have to do with 
  11. dos, not c.  Correct me if I'm wrong.
  12.  
  13. The program below does nothing but store lines from a file in
  14. malloced memory and then attempt to free that memory.  I was
  15. getting "exception 13" from QEMM, the manual said that often
  16. meant segment wraparound, which I don't really understand,
  17. but I guessed that using far pointers would make it less likely,
  18. and it seems to've worked.  But now the compiler (Borland C++ 2.0)
  19. complains about suspicious pointer conversions.
  20.  
  21. Can someone point me to an introductory article on pc segments?
  22. Is there a way to avoid dealing with this when using dos?  Or do
  23. I just have a bug in my program?
  24.  
  25. I also don't understand the numbers I get from coreleft.  They
  26. show memory sucked up after the mallocing, as I'd expect, but 
  27. none freed at all until the very last free, when it is all freed
  28. at once.  I thought this was perhaps because malloc works with
  29. chunks of memory, but even when I do huge files I get the same 
  30. pattern.
  31.  
  32. ------
  33. #include <stdio.h>
  34. #include "lines.h"
  35. #include <alloc.h>
  36.  
  37. main(int argc, char ** argv)
  38. {
  39.     FILE *infile ;
  40.     struct line linedesc ;
  41.     struct line far *linedescp, far *nextlinep ;
  42.     char buffer[MAXLINE+1] ;
  43.  
  44.     infile = fopen(argv[1], "rt") ;
  45.     if(infile == NULL)
  46.     {
  47.         printf("Can't open %s for reading", argv[1]) ;
  48.         exit(-1) ;
  49.     }
  50.     printf("before: %u\n", coreleft()) ;
  51.     linedescp = &linedesc ;
  52.     while(fgets(buffer, MAXLINE, infile))
  53.     {
  54.         linedescp->text = (char *) malloc(strlen(buffer)+1) ;
  55.         strcpy(linedescp->text, buffer) ;
  56.         linedescp->next = (struct line far *) malloc(sizeof(struct line)) ;
  57.         linedescp = linedescp->next ;
  58.     }
  59.     linedescp->text = NULL ;
  60.     linedescp->next = NULL ;
  61.     printf("after: %u\n", coreleft()) ;
  62.  
  63.     /* now free it all */
  64.  
  65.     linedescp = &linedesc ;
  66.     while(linedescp->text != NULL)
  67.     {
  68.         free(linedescp->text) ;
  69.         nextlinep = linedescp->next ;
  70.         free(linedescp) ;
  71.         linedescp = nextlinep ;
  72.      }
  73.      printf("Before last free: %u\n", coreleft()) ;
  74.      /* freed all but last element.  free the linedesc, but nothing was
  75.         malloced for text, so don't free -> text */
  76.      free(linedescp) ;
  77.      printf("After last free: %u\n", coreleft()) ;
  78.      return ;
  79. }
  80. ---lines.h---
  81. struct line
  82. {
  83.     char *text ;
  84.     struct line far *next ;
  85.     struct line far *prev ;
  86. } ;
  87.  
  88. #define MAXLINE 200
  89.