0

I try to read in a file multiple times instead of just once. While trying that I got alot of segementation faults. The part of the program with the while loop looks like this:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

/* General use buffer */
#define STRLEN 8196
char    string[STRLEN];


int lines = 1024;
char    **line;
int linemax;

int longest=0;


int main(){
int len,i;
int zwei = 1;
FILE * fp;
char    *s;
int debug =  0;
line=(char **)malloc(sizeof(char *) * 1024);
do{
    if ( (fp = fopen("rhel7_160731_0606.nmon", "r")) == NULL) {
        perror("failed to open file");      

                perror("fopen");
        exit(75);
    }
printf("where is the problem1,3\n");
    for (i = 0; fgets(string, STRLEN, fp) != NULL; i++) {
        if (i >= lines) {
            lines += 1024;
            line = (char **)realloc((void *)line, sizeof(char *) * lines);
        }

        if (string[strlen(string)-1] == '\n')
            string[strlen(string)-1] = 0;
        if (string[strlen(string)-1] == '\r')
            string[strlen(string)-1] = 0;
        if (string[strlen(string)-1] == ' ')
            string[strlen(string)-1] = 0;
        if (string[strlen(string)-1] == ',')
            string[strlen(string)-1] = 0;
        len = strlen(string) + 1;

        if (len > longest)
            longest = len;
        s = malloc(len);
        strcpy(s, string);
        line[i] = (char *)s;
    }
    linemax = i;
    lines = i;

    if (debug)
        for (i = 0; i < linemax; i++)
            printf("line %d lastline %s\n", i, line[i-1]);

zwei++;

}while(zwei<4);

return 0;
}

It hangs nothing or ends with a segmentation fault.

11
  • 2
    what does your debugger say? Commented Sep 16, 2016 at 17:35
  • Program received signal SIGSEGV, Segmentation fault. 0x00000000004009df in main () at test.c:53 53 line[i] = (char *)s; Commented Sep 16, 2016 at 17:44
  • which debugger is that? Commented Sep 16, 2016 at 17:45
  • So now this is what happens: #0 __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95 #1 0x00007ffff7a8924b in _IO_fgets (buf=0xeb2d50 "\002", n=8196, fp=0xea1c40) at iofgets.c:50 #2 0x000000000040a7f9 in main (argc=8, argv=0x7fffffffdf68) It runs twice and then something goes wrong. Is it a Buffer overflow? Commented Sep 16, 2016 at 17:56
  • 3
    } while(zwei>4); is never true, so it cannot loop twice, it's not the cause but I wonder how much else is careless. Commented Sep 16, 2016 at 17:58

1 Answer 1

2

You seem to have forgotten to allocate memory for line. It fails here: line[i] = (char *)s. I think you need to set lines to zero, since you reallocate line only if your iterator i gets grower than lines.

Also, fix this: while(zwei > 4) to while(zwei < 4). And, you need to free the memory you allocate - because you store all the pointers in line, it is not going to be complicated - just one loop.

Sign up to request clarification or add additional context in comments.

8 Comments

It's a global variable, therefore initialised to 0 or NULL which realloc can deal with. But that is a good catch.
@WeatherVane I mean, he tries to write data in line[i], but line, as you correctly say, sets to NULL by default.
how do I free the memory?
@bastel Using free( line[i] ) in a single, say, for loop. I have dealt with it putting the following loop right before while statement: for (i = 0; i < lines; i++){free( (void*) line[i] );} But if you want to store all the lines until the end, you may need something more elaborate.
@bastel I don't think you need to malloc it first, just set lines to 0, and, as WeatherVane noticed, realloc can deal with NULL and at the first time it will work as malloc
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.