home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19559 < prev    next >
Encoding:
Text File  |  1993-01-11  |  3.0 KB  |  122 lines

  1. Path: sparky!uunet!pipex!bnr.co.uk!uknet!mucs!lucs!scst81
  2. From: scst81@csc.liv.ac.uk (Mr. I. Rowland)
  3. Newsgroups: comp.lang.c
  4. Subject: realloc (again)
  5. Keywords: Realloc
  6. Message-ID: <C0pGL7.GqI@compsci.liverpool.ac.uk>
  7. Date: 11 Jan 93 19:53:30 GMT
  8. Sender: news@compsci.liverpool.ac.uk (News Eater)
  9. Organization: Computer Science, Liverpool University
  10. Lines: 109
  11. Nntp-Posting-Host: rib.csc.liv.ac.uk
  12.  
  13. Following my last communication I developed another smaller program that
  14.   uses REALLOC in the same way as my other code:
  15.  
  16.   #include <stdio.h>
  17.   #include <stdlib.h>
  18.   #include <string.h>
  19.  
  20.   #define noofstr 5
  21.  
  22.   #define bufsiz 255
  23.  
  24.   void main (void)
  25.  
  26.   {
  27.           char *array; char buff[bufsize];
  28.           int i;
  29.           int bytes ;
  30.  
  31.           for (i=0; i < noofstr; i++)
  32.           {
  33.                   printf("Enter string %d : ",i);
  34.                   scanf("%255s",buff);
  35.                   bytes = strlen(buff)+1;
  36.                   array = reilloc(array, (bytes));
  37.  
  38.                   if (array== NULL)
  39.                   {
  40.                           printf("Can't allocate");
  41.                           exit(1);
  42.                   }
  43.           strcat (array,buff);
  44.           }
  45.  
  46.  
  47.  
  48.                   printf(array);
  49.   }
  50.  
  51.  
  52.  
  53.   When this program is run it ask for 5 string inputs which are strung
  54.   together string by string using strcat. However the program seems to
  55.   rather temperamental - sometimes it works sometimes it doesn't. For
  56.   example
  57.  
  58.   Enter string 0 : oiuqrtq
  59.   Enter string 1 : [qoeri
  60.   Enter string 2 : [oqreu
  61.   Enter string 3 : erqui                   O.K.
  62.   Enter string 4 : [erou
  63.   oiuqrtq[qoeri[oqreuerqui[erou
  64.  
  65.  
  66.   Enter string 0 : asdfgads
  67.   Enter string 1 : asdafadfg
  68.   Enter string 2 : afgadfgadf
  69.   Enter string 3 : adfgadfa
  70.   Enter string 4 : adfgadf
  71.   asdfgads@adfgadf                Not produced all of characters
  72.  
  73.  
  74.   Enter string 0 : aasd
  75.   Enter string 1 : adf
  76.   Enter string 2 : asdf                     O.K.
  77.   Enter string 3 : asdf
  78.   Enter string 4 : asdf
  79.   aasdadfasdfasdfasdf
  80.  
  81.   Enter string 0 : abcdefg
  82.   Enter string 1 : abcdefgh
  83.   Enter string 2 : abcdefghi                    O.K.
  84.   Enter string 3 : abcdefghij
  85.   Enter string 4 : abcdefghijk
  86.   abcdefgabcdefghabcdefghiabcdefghijabcdefghijk
  87.  
  88.  
  89.   Enter string 0 : adfgadf
  90.   Enter string 1 : adfagdf
  91.   Enter string 2 : adfagdf                      ?
  92.   Enter string 3 : adfagdfg
  93.   Memory fault(coredump)
  94.  
  95.   Enter string 0 : asdfgh
  96.   Enter string 1 : asdfgh
  97.   Enter string 2 : asdfgh
  98.   Enter string 3 : sdfghjj
  99.   Enter string 4 : asdfghjk
  100.   Memory fault(coredump)
  101.  
  102.   Enter string 0 : a;'dfg
  103.   Enter string 1 : a'ldfg
  104.   Enter string 2 : 'aldfk
  105.   Enter string 3 : 'ladfkf
  106.   Enter string 4 : a'dlfggg
  107.  
  108.   Pid 13488 received a SIGSEGV for stack growth failure.
  109.   Possible causes: insufficient memory or swap space,
  110.   or stack size exceeded maxssiz.
  111.   Memory fault(coredump)
  112.  
  113.  
  114.  
  115.   Is this the fault of the program? Is it the operating system? What can
  116.   to be done to prevent it from happening?
  117.  
  118.   Again this program works on my Atari St using the Lattice C5 compiler.
  119.  
  120.   Ian Rowland (scst81)
  121. +
  122.