home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19558 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.9 KB  |  98 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: use of REALLOC under UNIX system
  5. Message-ID: <C0pC48.G2z@compsci.liverpool.ac.uk>
  6. Date: 11 Jan 93 18:16:56 GMT
  7. Sender: news@compsci.liverpool.ac.uk (News Eater)
  8. Organization: Computer Science, Liverpool University
  9. Lines: 86
  10. Nntp-Posting-Host: goyt.csc.liv.ac.uk
  11.  
  12.  
  13.   I have a problem with some C code
  14.  
  15.   The problem centers around the ANSI function REALLOC. My function looks
  16.   through a sorted list of names and returns a string pointer pointing to a
  17.   to a string that contains only one instance of the name - it possible that
  18.   many instances occur..
  19.  
  20.   The number of
  21.   names varies at different program executions. So instead of
  22.   declaring a large array to hold them I decided to create a pointer to a
  23.   string (char *clast_str). As each name was discovered its length was
  24.   calculated and enough space was allocated (using REALLOC) to contain the
  25.   new name AND the previous names too. The new name is copied using the
  26.   strcat function.
  27.  
  28.   However when I run this, at the first occurence of the REALLOC function
  29.  
  30.   clast_str = realloc(clast_str,bytes)     bytes = length of name + 1
  31.  
  32.                                            `+1' to account for `\0'
  33.  
  34.   the error `Bus error(coredump)' occurs.
  35.  
  36.  
  37.   The full code is as follows :
  38.  
  39.   char *lithology_count (void)
  40.  
  41.   {
  42.           int counter, record_count = 0;
  43.           char search_str[20];
  44.           char *clast_str;
  45.           int bytes = 0;
  46.      
  47.           while (record_count < num_records)
  48.           {
  49.                   counter = 0;
  50.                   printf("\nrecord count = %d",record_count+1);
  51.  
  52.                   strcpy(search_str,clast_record[record_count].lithology);
  53.                   bytes = (strlen(clast_str)+ 1) +
  54.                           (strlen (search_str) + 1);
  55. /*Error Occur Here*/
  56.  
  57.  
  58.                   clast_str = realloc(clast_str,bytes);
  59.  
  60.  
  61.                   if (!clast_str)
  62.                   {
  63.                           printf("\nAllocation Error\n");
  64.                           exit(1);
  65.                   }
  66.  
  67.                   strcat(clast_str,search_str);
  68.                   strcat(clast_str,",");
  69.  
  70.                   while ((record_count <= num_records) && (!strcmp
  71.                     (search_str, clast_record[record_count].lithology)))
  72.                   {
  73.                           counter++;
  74.                           record_count++;
  75.  
  76.                   }
  77.           printf("\ncounter = %d\n",counter);
  78.           }
  79.  
  80.           return (clast_str);
  81.   }
  82.  
  83.   The above code runs when compiled on the Atari St using the Lattice C5
  84.   compiler.
  85.  
  86.   I compile the above code on UNIX using :
  87.  
  88.   cc -Aa -o clast clast.c -lm
  89.  
  90.   Is there another library I should link it with. I have `included' STDLIB as
  91.   one of my header files, the others being CTYPE and STDIO.
  92.  
  93.   I would appreciate any assistanceyou could give.
  94.  
  95.   Ian Rowland (scst81)
  96.  
  97. Keywords: 
  98.