我的代码在哪里泄漏?我需要写免费()函数? c

发布于 2025-01-22 03:43:07 字数 1655 浏览 1 评论 0原文

此代码扫描编号然后他用malloc创建数组,然后我扫描字符串,然后用另一个malloc放入数组中,然后对字符串进行排序并打印它们。我用mallocs构建此代码,然后将数组放入数组中,在此代码中有泄漏,我需要在其中放置Free()函数以及如何放置Free()?我尝试了很多次来解决此泄漏,但它不起作用。

#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]);
    }
}

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]);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2025-01-29 03:43:07

对于名称,您正在分配sizeof(char)乘以用户提供的字节数。这需要是sizeof(char *),为您提供足够的空间来满足每个指针值。

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

您需要为NULL终止字符分配一个额外的字节('\ 0')。 sizeof(char)可以保证为1,使该语句冗余。

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

在Main结束之前,您需要释放名称>的每个元素,然后name本身。

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;

您的排序功能可能会尝试在不同大小的缓冲区之间复制字符串。您需要交换指针。

例子:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文