1

I'm including all the code, so if you got any comments I would like to learn!

The function is receiving a string and creates an array of string pointers; each string contains the word that begins with the chosen letter.

My problem is that on the function free_string() I'm getting an error!

I know this is the way to free a matrix, so maybe something went wrong even before that?

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

void main()
{
    char str[SIZE],leteer,**string;
    int size=0;
    printf("please enter the string\n");
    gets(str);
    printf("please enter the letter\n");
    scanf("%c",&leteer);
    string=create_strings_from_letter(str, leteer, &size);
    print_string(string,size);
    free_string(string,size);
}

char ** create_strings_from_letter(char *str, char letter, int * size)

{

    char **string_of_letters;
    char *read = str,*write;
    int count=0,i=0;
    while (*read)//cheaking how much strings to allocate//
    {
        if (*read == tolower(letter)|| *read == toupper(letter))
        {
            (*size)++;
            for (; *read && *read != ' '; read++);
        }
        for (; *read && *read != ' '; read++);
        read++;
    }
    string_of_letters = (char**)malloc((*size)*sizeof(char*));
    read = str;
    while (*read)
    {
        for (; *read && *read != tolower(letter) && *read != toupper(letter);read++);
        if (*read)
        {
            write = read;
            for (; *read && *read != ' '; read++, count++);
            string_of_letters[i] = (char*)malloc((count) * sizeof(char));
            strncpy(string_of_letters[i], write,count);
            string_of_letters[i][count] = '\0';
            count = 0;
            i++;
        }
        else
            break;
    }
    return string_of_letters;


}

void free_string(char ** string, int size)

{

    int i;
    for (i = 0;i<size; i++)
        free(string[i]);

    free(string);
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
NyaSol
  • 537
  • 1
  • 5
  • 21
  • 3
    `string_of_letters[i] = (char*)malloc((count) * sizeof(char));` ==> `string_of_letters[i] = malloc(count + 1);` – mch Aug 28 '18 at 08:41
  • What's the text of the error? – alseether Aug 28 '18 at 08:46
  • 1
    Where are all the `#include`s? - they too are part of a [mcve], especially since you're [casting the return value of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Antti Haapala -- Слава Україні Aug 28 '18 at 08:47
  • eror on the free_string function – NyaSol Aug 28 '18 at 08:47
  • #include #include #include #include – NyaSol Aug 28 '18 at 08:47
  • Please [edit] the question and provide the complete minimal example. – Antti Haapala -- Слава Україні Aug 28 '18 at 08:48
  • Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Aug 28 '18 at 08:57
  • It turns out I made a beginner (which i am) mistake string_of_letters[i] = malloc(count + 1) i have n indexs but n+1 adding the '\0' but what bothers is that he did not warn me about exceeded the limits of the array why is that? thank u all very much!!!!! – NyaSol Aug 28 '18 at 09:58
  • Concerning `create_strings_from_letter`: Reading twice as you did is one concept. It becomes difficult when you have a data stream which can't be processed twice (or would be very inconvenient to handle twice). For such cases, another concept is preferred: store two sizes e.g. `capacity` and `length` where `capacity` has the allocated size and `length` the really used. Whenever, `capacity == length` and additional space is needed, `realloc()` is called. `realloc()` is considered as expensive function. Thus, usually a compromize is used: whenever `length == capacity` it reallocs `2 * capacity`. – Scheff's Cat Aug 28 '18 at 10:28

1 Answers1

0

From the comments, you seem to have found the source of your error, which is writing into a memory address which is out of bounds. So I want to address your other question in the comments.

what bothers is that he did not warn me about exceeded the limits of the array why is that?

This will depend on the compiler options you have set. Some compilers have options to set the warning level and the optimization level. If you do this the warning can be seen.

For example, in the case of GCC, if you set the warning level option to -Wall (Many) and the optimization level to -O2 (Full), you will get a warning when you try to write into an out-of-bounds memory address.

The warning will be something like this:

warning: array subscript is above array bounds [-Warray-bounds]
P.W
  • 26,289
  • 6
  • 39
  • 76