0

I am trying to compile a program I wrote in C but can't get rid of a 'bus error' when running the program. I came across other threads mentioning 'string literals' and memory issues but I think it's time I ask for a fresh look over my code.

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

Counting the words:

int     count(char *str)
{
    int i = 0;
    int k = 0;

    while (str[i])
    {
        if (str[i] != ' ')
        {
            while (str[i] != ' ')
                i++;
            k++;
        }
        else
            i++;
    }
    return (k);
}

Extracting the words:

void    extract(char *src, char **dest, int i, int k)
{
    char    *tabw;
    int     j = 0;

    while (src[i + j] != ' ')
        j++;

    tabw = (char*)malloc(sizeof(char) * j + 1);

    j = 0;

    while (src[i + j] != ' ')
    {
        tabw[j] = src[i + j];
        j++;
    }

    tabw[j] = '\0';
    dest[k] = &tabw[0];

    return;
}

Splitting the string into words:

char    **split(char *str)
{
    int     i = 0;
    int     k = 0;
    char    **dest;

    dest = (char**)malloc(sizeof(*dest) * count(str) + 1);

    while (str[i] != '\0')
    {
        while (str[i] == ' ')
            i++;

        if (str[i] != ' ')
            extract(str, dest, i, k++);

        while (str[i] != ' ')
            i++;
    }
    dest[k] = 0;
    return (dest);
}

Printing:

void    ft_putchar(char c)
{
    write(1, &c, 1);
}

void    print(char **tab)
{
    int     i = 0;
    int     j;

    while (tab[i])
    {
        j = 0;
        while (tab[i][j])
        {
            ft_putchar(tab[i][j]);
            j++;
        }
        ft_putchar('\n');
        i++;
    }
}

int     main()
{
    print(split("  okay  blue     over"));
}

Do you guys have any idea? Thanks!

Wizzardzz
  • 781
  • 1
  • 9
  • 32
  • 1
    what is a bus-error? can you be more specific? Also please post a [MCVE]# – Kami Kaze Jun 12 '18 at 14:10
  • @KamiKaze: https://en.wikipedia.org/wiki/Bus_error – andreee Jun 12 '18 at 14:11
  • 1
    @andreee isn't this the same as sigsegv? – Kami Kaze Jun 12 '18 at 14:15
  • 1
    @KamiKaze no, see [this question](https://stackoverflow.com/questions/838540/bus-error-vs-segmentation-fault). – andreee Jun 12 '18 at 14:17
  • 1
    @achal, I don't see that. `count` is called outside of `sizeof`. – Paul Ogilvie Jun 12 '18 at 14:19
  • When calling any of the heap allocation functions: `malloc` `calloc` `realloc` 1) the returned type is `void*` which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. 2) Always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Jun 13 '18 at 20:16
  • the count() function claims that it counts the words. However, it only produces an accurate count if the input data ends with a space – user3629249 Jun 13 '18 at 20:21
  • the expression: `sizeof( char )` is defined in the C standard as 1. Multiplying anything by 1 has no effect. Suggest removing that expression – user3629249 Jun 13 '18 at 20:23
  • the posted code calls `malloc()` several times, but never passes any of the allocated heap memory pointers to `free()`. This results in a memory leak that you should fix – user3629249 Jun 13 '18 at 20:25
  • regarding: `void ft_putchar(char c)` This function does exactly the same as the `putchar()` function, so why clutter the code with this unneeded function? – user3629249 Jun 13 '18 at 20:29
  • the posted code is NOT producing an array of pointers to char strings. 1) nothing is being allocated for the characters strings 2) the strings are not being NUL terminated. So the code is writing to memory that it does not own and has not terminated the strings, so the print() operation just keeps on printing until it encounters a (random) NUL byte – user3629249 Jun 13 '18 at 20:32
  • @user3629249 Thank you very much for the precisions. Any chance you could provide a corrected version of the code? – Wizzardzz Jun 15 '18 at 12:19

1 Answers1

2

while (str[i] != ' ') in count goes beyond end-of-string if no space is encountered (e.g. at end of line). I see you make this mistake at multiple places (in both extract and split): you assume you will see a space, but that is not necessarily true. For example, the last word of the string you pass in main is not followed by a space.

Use: while (str[i] != ' ' && str[i] != 0 )

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Thank you very much Paul, it seems so obvious now! Bus error wasn't ringing that bell to me tho.. I am relatively new to C and programming in general, do you think that program is well optimized (apart from not using standard functions whenever possible which is on purpose)? – Wizzardzz Jun 12 '18 at 14:38