'Where my code is leak? how i need to write free() function? C

This code scans number then he create array with malloc, then i scan strings, i put then inside the array with another malloc, then i sort the strings and print them. I build this code with mallocs and i put array inside array,i have leaks in this code, where i need to put free() function and how i put free()? I tried many times to solve this leaks but its not working.

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

void sort(char** names, int length);
void print_array(char** names, int length);

int main()
{
    int num_of_friends = 0;
    int i = 0;
    char name[50] = { 0 };
    char** names = NULL;
    printf("Enter number of friends: ");
    scanf("%d", &num_of_friends);
    getchar();
    names = malloc(sizeof(char) * num_of_friends);
    for ( i = 0; i < num_of_friends; i++)
    {
        printf("Enter name of friend %d: ", i + 1);
        fgets(name, 50, stdin);
        name[strcspn(name, "\n")] = 0;
        names[i] = malloc(sizeof(char) * strlen(name));
        strcpy(names[i], name);
    }
    sort(names, num_of_friends);
    print_array(names, num_of_friends);
    getchar();
    return 0;
}
void sort(char** names, int length)
{
    char temp[50] = { 0 };
    int i = 0;
    int j_min = 0;
    int j = 0;
    for ( i = 0; i < length - 1; i++)
    {
        j_min = i;
        for ( j = i+1; j < length; j++)
        {
            if (strcmp(names[j], names[j_min]) < 0)
            {
                j_min = j;
            }
        }
        if (j_min != i)
        {
            strcpy(temp, names[i]);
            strcpy(names[i], names[j_min]);
            strcpy(names[j_min], temp);
        }
    }
}
void print_array(char** names, int length)
{
    int i = 0;
    for (i = 0; i < length; i++)
    {
        printf("Friend %d: %s \n", i + 1, names[i]);
    }
}


Solution 1:[1]

For names, you are allocating sizeof (char) times the user provided number of bytes. This is needs to be sizeof (char *), giving you enough room for each pointer value.

names = malloc(sizeof (char *) * num_of_friends);

You need to allocate one additional byte for the null terminating character ('\0'). sizeof (char) is guaranteed to be 1, making that statement redundant.

names[i] = malloc(strlen(name) + 1);

Before the end of main, you need to free each element of names, and then names itself.

sort(names, num_of_friends);
print_array(names, num_of_friends);
getchar();

for (i = 0; i < num_of_friends; i++)
    free(names[i]);
free(names);

return 0;

Your sort function may attempt to copy strings between buffers of differing size. You need to swap pointers instead.

Example:

void swap(char **a, char **b) {
    char *c = *a;
    *a = *b;
    *b = c;
}

void sort(char **names, size_t length) {
    for (size_t i = 0; i < length - 1; i++)
        for (size_t j = i + 1; j < length; j++)
            if (strcmp(names[i], names[j]) > 0)
                swap(names + i, names + j);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1