Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 有些未在代码中释放数据_C_Heap - Fatal编程技术网

C 有些未在代码中释放数据

C 有些未在代码中释放数据,c,heap,C,Heap,请尝试帮助我,我被这个代码困扰了很长时间,我需要一些帮助。因此,这段代码需要询问用户他有多少朋友,并按大小打开一个名称数组。没有其他选项可以更改其中一个名称和其他朋友更改所有数组。这一切都很好,但堆内存没有被释放。。我会和记忆博士核实一下; 请帮帮我!:) #包括 #包括 #包括 #定义长度20 void printsFriends(int friends,char**friendBook); void changeFriends(int friends,char**friendsBook);

请尝试帮助我,我被这个代码困扰了很长时间,我需要一些帮助。因此,这段代码需要询问用户他有多少朋友,并按大小打开一个名称数组。没有其他选项可以更改其中一个名称和其他朋友更改所有数组。这一切都很好,但堆内存没有被释放。。我会和记忆博士核实一下; 请帮帮我!:)

#包括
#包括
#包括
#定义长度20
void printsFriends(int friends,char**friendBook);
void changeFriends(int friends,char**friendsBook);
内部主(空)
{
inti=0,num=0,friends=0;
字符str[LENGTH];
int标志=0;
int changeTo=0;
printf(“你好,朋友,你好吗?”\n告诉我你有多少朋友?\n”);
scanf(“%d”、&friends);
getchar();
char**friendBook=(char**)malloc(sizeof(char*)*friends);
转变为朋友;
国际单项体育联合会(友谊书)
{
对于(i=0;i
每当调用选项2时,旧的
friendBook[i]
指针(对于每个
i
)都不会被释放。

我不确定DR是什么,但如果您使用linux和
valgrind
,则使用
--track origins=yes
运行它。
#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);
        }
    }
}