-3

Please go to line 29 and read the comment.

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

int main()
{
    char* messenges[5];
    char* allstrings;
    register int i;
    int total = 0;

    for (i = 0; i < 5; i++)
    {
        printf("Enter a string: ");
        messenges[i] = (char*)malloc(5 * sizeof(char));
        scanf("%s", messenges[i]);
        total += strlen(messenges[i]);
    }

    allstrings = (char*)malloc(total);
    strcpy(allstrings, messenges[0]);

    for (i = 1; i < 5; i++)
    {
        strcat(allstrings, " ");
        strcat(allstrings, messenges[i]);
    }
    printf("%s", allstrings);
    free(allstrings); // This does not work..WHY?(Visual Studio 2015)
    for (i = 0; i < 5; i++)
    {
        free(messenges[i]);// This works fine.
    }
    getch();
    return 0;
}

Above code doesn't free allstrings but it can free messenges[i]. Why doesn't it work for allstrings as well?

WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • 2
    How does it not work? – afic Jan 15 '18 at 07:15
  • Also, there is no need to cast the result of malloc. – Jodocus Jan 15 '18 at 07:17
  • 1
    Try to really count how much characters are *allocated* and *used* including the terminating null. You should soon realize that you are doing past end array accesses => Undefined Behaviour. – Serge Ballesta Jan 15 '18 at 07:19
  • Slightly off topic, getch() is from conio.h, which you did include. Consider using getchar() from stdio.h instead. – afic Jan 15 '18 at 07:26
  • Welcome to Stack Overflow! 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 Jan 15 '18 at 09:29

2 Answers2

3

It probably fails because you seem to have forgotten that char strings in C are really called null-terminated byte strings. That null-terminator part is quite important, and it needs an extra byte or you will write out of bounds.

Writing out of bounds leads to undefined behavior, and might overwrite internal data used by the memory allocator leading free to "not work".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

First of all strings in c are null terminated char array. You didn't allocate enough space to store them. Also you didn't allocate enough space for those blanks you have concatenated. Long story short by writing to memory that is not allocated by you has invoked undefined behavior.

Also it is not clear how you can say that free() call failed. What is your idea of not freeing the memory?

In this case for example, you can consider two things which will help you allocate enough memory. strcat and strcpy both take into account the \0 and also strcat overwrites the \0 in the source string.

Also there are many many problems in your code - starting from not allocating enough memory - casting the return value of malloc, not chekcing the return value of malloc and using scanf without length modifier.

user2736738
  • 30,591
  • 5
  • 42
  • 56