home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pipex!bnr.co.uk!uknet!mucs!lucs!scst81
- From: scst81@csc.liv.ac.uk (Mr. I. Rowland)
- Newsgroups: comp.lang.c
- Subject: use of REALLOC under UNIX system
- Message-ID: <C0pC48.G2z@compsci.liverpool.ac.uk>
- Date: 11 Jan 93 18:16:56 GMT
- Sender: news@compsci.liverpool.ac.uk (News Eater)
- Organization: Computer Science, Liverpool University
- Lines: 86
- Nntp-Posting-Host: goyt.csc.liv.ac.uk
-
-
- I have a problem with some C code
-
- The problem centers around the ANSI function REALLOC. My function looks
- through a sorted list of names and returns a string pointer pointing to a
- to a string that contains only one instance of the name - it possible that
- many instances occur..
-
- The number of
- names varies at different program executions. So instead of
- declaring a large array to hold them I decided to create a pointer to a
- string (char *clast_str). As each name was discovered its length was
- calculated and enough space was allocated (using REALLOC) to contain the
- new name AND the previous names too. The new name is copied using the
- strcat function.
-
- However when I run this, at the first occurence of the REALLOC function
-
- clast_str = realloc(clast_str,bytes) bytes = length of name + 1
-
- `+1' to account for `\0'
-
- the error `Bus error(coredump)' occurs.
-
-
- The full code is as follows :
-
- char *lithology_count (void)
-
- {
- int counter, record_count = 0;
- char search_str[20];
- char *clast_str;
- int bytes = 0;
-
- while (record_count < num_records)
- {
- counter = 0;
- printf("\nrecord count = %d",record_count+1);
-
- strcpy(search_str,clast_record[record_count].lithology);
- bytes = (strlen(clast_str)+ 1) +
- (strlen (search_str) + 1);
- /*Error Occur Here*/
-
-
- clast_str = realloc(clast_str,bytes);
-
-
- if (!clast_str)
- {
- printf("\nAllocation Error\n");
- exit(1);
- }
-
- strcat(clast_str,search_str);
- strcat(clast_str,",");
-
- while ((record_count <= num_records) && (!strcmp
- (search_str, clast_record[record_count].lithology)))
- {
- counter++;
- record_count++;
-
- }
- printf("\ncounter = %d\n",counter);
- }
-
- return (clast_str);
- }
-
- The above code runs when compiled on the Atari St using the Lattice C5
- compiler.
-
- I compile the above code on UNIX using :
-
- cc -Aa -o clast clast.c -lm
-
- Is there another library I should link it with. I have `included' STDLIB as
- one of my header files, the others being CTYPE and STDIO.
-
- I would appreciate any assistanceyou could give.
-
- Ian Rowland (scst81)
-
- Keywords:
-