c - 一些未在代码中释放的数据

标签 c heap-memory

请尝试帮助我,我被这段代码困扰了很长时间,我需要一些帮助。所以这段代码需要询问用户他有多少 friend ,并根据大小打开一个名字数组。比有更改其中一个名称和更改其他 friend 的所有数组的选项。一切正常,但堆内存没有被释放。我会和 DR 核实一下。内存; 请帮我! :)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20

void printsFriends(int friends, char** friendBook);
void changeFriends(int friends, char** friendsBook);
int main(void)
{
    int i = 0, num = 0,friends=0;
    char str[LENGTH];
    int flag = 0;
    int changeTo = 0;
    printf("Hello friend,how U doin'?\nTell me how many friends do you have?\n");
    scanf("%d", &friends);
    getchar();
    char** friendBook = (char**)malloc(sizeof(char*)*friends);
    changeTo = friends;
    if (friendBook)
    {

        for (i = 0; i < changeTo; i++)
        {
            friendBook[i] = malloc(LENGTH*sizeof(char));
        }
        changeFriends(changeTo, friendBook);
        printsFriends(changeTo, friendBook);
        while (!flag)
        {
            printf("\n\nPlease enter one of options:\n1.Change name of friend\n2.Change number of friends\n3.Exit and free the memory\n");
            scanf("%d",&num);
            getchar();
            switch (num)
            {
                case 1:
                    printf("What number of friend do you want to change?\n");
                    scanf("%d", &num);
                    getchar();
                    printf("Enter name to change for\n");
                    fgets(str, LENGTH, stdin);
                    str[strcspn(str, "\n")] = 0;
                    friendBook[num - 1] =realloc(friendBook[num-1], (sizeof(char)*(strlen(str) + 1)));
                    if (*(friendBook + num - 1))
                    {
                        strcpy(*(friendBook + num - 1), str);
                    }
                    printsFriends(changeTo, friendBook);
                    break;
                case 2:
                    printf("Enter number of friends you want:\n");
                    scanf("%d", &changeTo);
                    getchar();
                    friendBook = (char**)realloc(friendBook,sizeof(char*)*changeTo);
                    for (i = 0; i < changeTo; i++)
                    {
                        friendBook[i] = malloc(LENGTH*sizeof(char));
                    }
                    changeFriends(changeTo,friendBook );
                    printsFriends(changeTo, friendBook);                    
                    break;
                case 3:
                    flag = 1;
                    printf("BYE BYE!\n");
                    break;
                default:
                    printf("not good choice, enter again\n");
                    break;

            }
        }
    }

    for (i = 0; i < changeTo; i++)
    {
        free(friendBook[i]);
    }
    free(friendBook);
    system("PAUSE");
    return 0;
}

/*
This function get number of friends and friends book pointer to pointer and prints all the names.
input: number of friends and pointer to pointer of char's(array of strings)
output: none
*/
void printsFriends(int friends, char** friendBook)
{
    int i = 0;
    for (i = 0; i < friends; i++)
    {
        printf("Friend: %s\tLength of friend name %d\n", friendBook[i], strlen(friendBook[i]));
    }
}
/*
this function gets number of friends and friends book and save to the array of strings names from stdin by the length of every string.
input: friends number and array of strings
output: none
*/
void changeFriends(int friends, char** friendBook)
{
    int i = 0;
    char str[LENGTH];
    for (i = 0; i < friends; i++)
    {
        printf("Enter friend number %d: \n", i + 1);
        fgets(str, LENGTH, stdin);
        str[strcspn(str, "\n")] = 0;
        friendBook[i] = realloc(friendBook[i], (sizeof(char)*(strlen(str) + 1)));        // dynamic memory for every string(name)
        if (friendBook[i])
        {
            strcpy(friendBook[i], str);
        }
    }
} 

最佳答案

每当调用选项 2 时,旧的 friendBook[i] 指针(对于每个 i)都不会被释放。

关于c - 一些未在代码中释放的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36534677/

相关文章:

C 如何使用带比较器功能的快速排序

堆栈中的 C++ 类对象作为 Map 的值插入

java - 为什么 JVM HeapMemoryUsage init 值大于 commited/max 值?

c++ - c++程序在内存中的组织方式——栈和堆

c - 为什么即使使用了 volatile 关键字,编译器也会因 strncmp() 而优化掉共享内存读取?

C编译错误: request for member ___ in something not a structure or union

c - 为什么链接的二进制文件的 _size 符号不能正常工作?

c - `stbi_failure_reason()` 不返回 SOI,但文件加载

java - 将堆大小增加至 6GB

C++ 堆/栈说明