samedi 22 décembre 2018

Why do I have segmentation fault in this if line?

Recently I was making a program and it was giving me segmentation fault. After using gdb, I realized where I had the problem but I can't solve that.

The function is the following:

int readSocket_line(int socket, char * array) {
    int i = 0;
    while (1) {
        if (read(socket, & array[i], 1) < 0) {
            return -1;
            break;
        }
        if ((array[i] == '\n') || (array[i] == ' ')) {
            break;
        }
        i++;
    }
    array[i] = '\0';
    return 0;
}

The error that is shown by gdb is the following:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb3df5b40 (LWP 24457)]
0x08048f70 in readSocket_line (socket=18, array=0xb3df5337 "")
    at sockets_clie.c:14
14            if((array[i] == '\n') || (array[i] == ' ')){ 

I can't understand how can I solve that problem.

I would like some help to solve and understand this error. Thank you!

Aucun commentaire:

Enregistrer un commentaire