鱼C论坛

 找回密码
 立即注册
查看: 1557|回复: 3

[已解决]关于C语言malloc内存申请失败的问题

[复制链接]
发表于 2019-2-4 16:05:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 牛奶刺身 于 2019-2-4 16:35 编辑

总体代码如下(实现通讯录):  (问题描述在下面
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Cont
{
        char name[20];
        char phone[12];
        char company[40];
        struct Cont *next;
};

void addPerson(struct Cont **);
struct Cont *findPerson(struct Cont **);
void changePerson(struct Cont **);
void delPerson(struct Cont **);
void displayPerson(struct Cont **);
void Input(struct Cont *);
void Print(struct Cont *);

void Input(struct Cont *news)
{
        printf("请输入姓名:");
        scanf("%s", news->name);
        printf("请输入电话:");
        scanf("%s", news->phone);
        printf("请输入公司:");
        scanf("%s", news->company);
        getchar();
}

void Print(struct Cont *display)
{
        printf("姓名:%s\n", display->name);
        printf("电话:%s\n", display->phone);
        printf("公司:%s\n", display->company);
}

void addPerson(struct Cont **data)
{
        struct Cont *person, *temp;
        static struct Cont *tail;

        person = (struct Cont *)malloc(sizeof(struct Cont));
        if (person == NULL);
        {
                printf("内存分配失败!\n");
                exit(1);
        }

        Input(person);

        while (1)
        {
                if (*data == NULL)
                {
                        *data = person;
                        person->next = NULL;
                }
                else
                {
                        tail->next = person;
                        person->next = NULL;
                        break;
                }
        }
}

struct Cont *findPerson(struct Cont **data)
{
        char input[20];
        struct Cont *search;
        
        search = *data;
        scanf("%s", input);
        getchar();
        while (search != NULL)
        {
                if (!strcmp(search->name, input))
                {
                        break;
                }
                else
                {
                        search = search->next;
                }
        }
        
        if (search == NULL)
        {
                printf("抱歉,未找到该联系人!\n");
                return search;
        }
        
        Print(search);
        
        return search;
}

void changePerson(struct Cont **data)
{
        struct Cont *search;
        char ch; 

        printf("请输入要更改的联系人:");
        search = findPerson(data);

        if (search == NULL)
        {
                printf("请问是否添加新的联系人(Y/N)?"); 
                do
                {
                        scanf("%c", &ch);
                        getchar();
                } while (ch != 'Y' && ch != 'N');
        }
        else
        {
                printf("请问是否更改该联系人信息(Y/N)?");
                do
                {
                        scanf("%c", &ch);
                        getchar();
                } while (ch != 'Y' && ch != 'N');
        }
        
        if (ch == 'N')
        {
                return;
        }
        addPerson(&search);
}

void delPerson(struct Cont **data)
{
        struct Cont *previous, *temp;
        char ch;
        
        printf("请输入要删除的联系人:");
        previous = findPerson(data);
        
        if (previous == NULL)
        {
                printf("未找到该联系人!\n");
                return;
        }
        
        printf("请问是否删除该联系人信息(Y/N)?");
        do
        {
                scanf("%c", &ch);
                getchar();
        } while (ch != 'Y' && ch != 'N');
        
        if (ch == 'N')
        {
                return;
        }

        temp = previous;
        previous = previous->next;

        free(temp);
}

void displayPerson(struct Cont **data)
{
        while (*data != NULL)
        {
                Print(*data);
                *data = (*data)->next;
        }
}

int main(void)
{
        struct Cont *list = NULL;
        int index;
        
        printf("=======================\n");
        printf("=   欢迎使用本程序!  =\n");
        printf("=   (指令功能如下) : =\n");
        printf("=  1.插入新的联系人; =\n");
        printf("=  2.查找已有联系人; =\n");
        printf("=  3.更改已有联系人; =\n");
        printf("=  4.删除已有联系人; =\n");
        printf("=  5.显示当前通讯录; =\n");
        printf("=  6.退出通讯录程序   =\n");
        printf("=======================\n");
        
        do
        {
                printf("请输入指令:");
                scanf("%d", &index);
                getchar();
                switch (index)
                {
                        case 1 : addPerson(&list); break;
                        case 2 : printf("请输入要查找的联系人:");findPerson(&list); break;
                        case 3 : changePerson(&list); break;
                        case 4 : delPerson(&list); break;
                        case 5 : displayPerson(&list); break;
                        case 6 : break;
                        default : break;
                }
        } while (index != 6);
        
        printf("感谢使用本程序!\n");
        
        free(list);
        
        return 0;
}



出问题的地方是addPerson函数,运行时直接返回申请内存失败

求大神解答一下,拜托拜托!
最佳答案
2019-2-5 04:13:13
本帖最后由 jackz007 于 2019-2-5 04:29 编辑

    你的代码问题很多,几乎无法实现预设的所有功能,我已经帮你全面修改了一下,当然,可能还有未发现的错误。

    由于改动的地方较多,就不一一叙述,楼主就参照自己的代码参考吧。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Cont
{
        char name[20];
        char phone[12];
        char company[40];
        struct Cont *next;
};

void addPerson(struct Cont **);
struct Cont *findPerson(struct Cont **);
void changePerson(struct Cont **);
void delPerson(struct Cont **);
void displayPerson(struct Cont **);
void Input(struct Cont *);
void Print(struct Cont *);

void Input(struct Cont * news)
{
        printf("请输入姓名:")     ;
        scanf("%s", news->name)    ;
        printf("请输入电话:")     ;
        scanf("%s", news->phone)   ;
        printf("请输入公司:")     ;
        scanf("%s", news->company) ;
}

void Print(struct Cont *display)
{
        printf("姓名:%s\n", display->name);
        printf("电话:%s\n", display->phone);
        printf("公司:%s\n", display->company);
}

void addPerson(struct Cont **data)
{
        struct Cont * person , * tail , * temp                   ;

        person = (struct Cont *)malloc(sizeof(struct Cont))      ;
        if (person == NULL) {
                printf("内存分配失败!\n")                       ;
                exit(1)                                          ;
        }
        Input(person)                                            ;
        person -> next = NULL                                    ;
        if (* data == NULL) {
                * data = person                                  ;
        } else {
                tail = * data                                    ;
                while (tail -> next != NULL) tail = tail -> next ;
                tail -> next = person                            ;
        }
}

struct Cont * findPerson(struct Cont **data)
{
        char input[20]                                      ;
        struct Cont * search                                ;
        
        search = * data                                     ;
        printf("请输入姓名:")                              ;
        scanf("%s" , input)                                 ;
        while (search != NULL) {
                if (! strcmp(search -> name , input)) break ;
                search = search -> next                     ;
        }
        return search                                       ;                                       
}

struct Cont * SearchPerson(struct Cont **data)
{
        struct Cont * search                                ;
        search = findPerson(data)                           ;
        if(search == NULL) printf("抱歉,联系人未找到\n")   ;
        else Print(search)                                  ;
        return search                                       ; 
}

void changePerson(struct Cont **data)
{
        struct Cont * search                                               ;
        char ch                                                            ;  
        search = findPerson(data)                                          ;
        if (search == NULL) {
                printf("抱歉,没有找到该联系人信息\n")                     ;
                printf("请问是否添加新的联系人(Y/N)?")                    ; 
                do {
                        ch = getchar()                                     ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
                if (ch == 'Y' || ch == 'y') {
                        addPerson(data)                                    ;
                        printf("成功添加联系人信息\n")                     ; 
                }
        } else {
                printf("请问是否更改该联系人信息(Y/N)?")                  ;
                do {
                        ch = getchar()                                     ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
                if (ch == 'Y' || ch == 'y') {
                        Input(search)                                      ;        
                        printf("成功修改联系人信息\n")                     ;
                }
        }
}

void delPerson(struct Cont **data)
{
        struct Cont * previous , * temp       ;
        char ch                               ;
        bool f                                ;
        
        temp = findPerson(data)               ;
        if (temp == NULL) {
                printf("未找到该联系人!\n")  ;
        } else {
                printf("请问是否删除该联系人信息(Y/N)?")                       ;
                do {
                        ch = getchar()                                          ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n')      ;
                if(ch == 'Y' || ch == 'y') {
                        f = false                                               ;
                        if (temp == * data) {
                                * data = temp -> next                           ;
                                f = true                                        ;    
                        } else {
                                previous = * data                               ;
                                while (previous != NULL) {
                                        if (previous -> next == temp) {
                                                previous -> next = temp -> next ;
                                                f = true                        ;
                                                break                           ;
                                        }
                                }
                        }
                        if (f) {
                                free(temp)                                      ;
                                printf("成功删除该联系人!\n")                  ;
                        } else {
                                printf("抱歉,删除联系人失败!\n")              ;
                        }
                }
        }
}

void displayPerson(struct Cont **data)
{
        struct Cont * walk                   ;
        walk = * data                        ;
        if (walk != NULL) {
                while (walk != NULL) {
                        Print(walk)          ;
                        walk = walk -> next  ;
                }
        } else {
                printf("抱歉,尚无数据录入") ;
        }
}

int main(void)
{
        struct Cont *list = NULL , * p     ;
        char c[20]                         ;
        
        printf("=======================\n");
        printf("=   欢迎使用本程序!  =\n");
        printf("=   (指令功能如下) : =\n");
        printf("=  1.插入新的联系人; =\n");
        printf("=  2.查找已有联系人; =\n");
        printf("=  3.更改已有联系人; =\n");
        printf("=  4.删除已有联系人; =\n");
        printf("=  5.显示当前通讯录; =\n");
        printf("=  6.退出通讯录程序   =\n");
        printf("=======================\n");
        
        do {
                printf("请输入指令:")                         ;
                gets(c)                                        ;
                switch (c[0]) {
                        case '1' : addPerson(&list); break     ;
                        case '2' : SearchPerson(&list) ; break ;
                        case '3' : changePerson(&list); break  ;
                        case '4' : delPerson(&list); break     ;
                        case '5' : displayPerson(&list); break ;
                        case '6' : break                       ;
                        default  : break                       ;
                }
        } while (c[0] != '6')        ;
        printf("感谢使用本程序!\n") ;
        free(list)                   ;
        return 0                     ;
}
屏幕截图(2).png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-2-4 16:33:12 | 显示全部楼层
我的天,犯了个很二的错误,在判断语句的后面加了个分号哭唧唧,没脸见人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-5 04:13:13 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-2-5 04:29 编辑

    你的代码问题很多,几乎无法实现预设的所有功能,我已经帮你全面修改了一下,当然,可能还有未发现的错误。

    由于改动的地方较多,就不一一叙述,楼主就参照自己的代码参考吧。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Cont
{
        char name[20];
        char phone[12];
        char company[40];
        struct Cont *next;
};

void addPerson(struct Cont **);
struct Cont *findPerson(struct Cont **);
void changePerson(struct Cont **);
void delPerson(struct Cont **);
void displayPerson(struct Cont **);
void Input(struct Cont *);
void Print(struct Cont *);

void Input(struct Cont * news)
{
        printf("请输入姓名:")     ;
        scanf("%s", news->name)    ;
        printf("请输入电话:")     ;
        scanf("%s", news->phone)   ;
        printf("请输入公司:")     ;
        scanf("%s", news->company) ;
}

void Print(struct Cont *display)
{
        printf("姓名:%s\n", display->name);
        printf("电话:%s\n", display->phone);
        printf("公司:%s\n", display->company);
}

void addPerson(struct Cont **data)
{
        struct Cont * person , * tail , * temp                   ;

        person = (struct Cont *)malloc(sizeof(struct Cont))      ;
        if (person == NULL) {
                printf("内存分配失败!\n")                       ;
                exit(1)                                          ;
        }
        Input(person)                                            ;
        person -> next = NULL                                    ;
        if (* data == NULL) {
                * data = person                                  ;
        } else {
                tail = * data                                    ;
                while (tail -> next != NULL) tail = tail -> next ;
                tail -> next = person                            ;
        }
}

struct Cont * findPerson(struct Cont **data)
{
        char input[20]                                      ;
        struct Cont * search                                ;
        
        search = * data                                     ;
        printf("请输入姓名:")                              ;
        scanf("%s" , input)                                 ;
        while (search != NULL) {
                if (! strcmp(search -> name , input)) break ;
                search = search -> next                     ;
        }
        return search                                       ;                                       
}

struct Cont * SearchPerson(struct Cont **data)
{
        struct Cont * search                                ;
        search = findPerson(data)                           ;
        if(search == NULL) printf("抱歉,联系人未找到\n")   ;
        else Print(search)                                  ;
        return search                                       ; 
}

void changePerson(struct Cont **data)
{
        struct Cont * search                                               ;
        char ch                                                            ;  
        search = findPerson(data)                                          ;
        if (search == NULL) {
                printf("抱歉,没有找到该联系人信息\n")                     ;
                printf("请问是否添加新的联系人(Y/N)?")                    ; 
                do {
                        ch = getchar()                                     ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
                if (ch == 'Y' || ch == 'y') {
                        addPerson(data)                                    ;
                        printf("成功添加联系人信息\n")                     ; 
                }
        } else {
                printf("请问是否更改该联系人信息(Y/N)?")                  ;
                do {
                        ch = getchar()                                     ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
                if (ch == 'Y' || ch == 'y') {
                        Input(search)                                      ;        
                        printf("成功修改联系人信息\n")                     ;
                }
        }
}

void delPerson(struct Cont **data)
{
        struct Cont * previous , * temp       ;
        char ch                               ;
        bool f                                ;
        
        temp = findPerson(data)               ;
        if (temp == NULL) {
                printf("未找到该联系人!\n")  ;
        } else {
                printf("请问是否删除该联系人信息(Y/N)?")                       ;
                do {
                        ch = getchar()                                          ;
                } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n')      ;
                if(ch == 'Y' || ch == 'y') {
                        f = false                                               ;
                        if (temp == * data) {
                                * data = temp -> next                           ;
                                f = true                                        ;    
                        } else {
                                previous = * data                               ;
                                while (previous != NULL) {
                                        if (previous -> next == temp) {
                                                previous -> next = temp -> next ;
                                                f = true                        ;
                                                break                           ;
                                        }
                                }
                        }
                        if (f) {
                                free(temp)                                      ;
                                printf("成功删除该联系人!\n")                  ;
                        } else {
                                printf("抱歉,删除联系人失败!\n")              ;
                        }
                }
        }
}

void displayPerson(struct Cont **data)
{
        struct Cont * walk                   ;
        walk = * data                        ;
        if (walk != NULL) {
                while (walk != NULL) {
                        Print(walk)          ;
                        walk = walk -> next  ;
                }
        } else {
                printf("抱歉,尚无数据录入") ;
        }
}

int main(void)
{
        struct Cont *list = NULL , * p     ;
        char c[20]                         ;
        
        printf("=======================\n");
        printf("=   欢迎使用本程序!  =\n");
        printf("=   (指令功能如下) : =\n");
        printf("=  1.插入新的联系人; =\n");
        printf("=  2.查找已有联系人; =\n");
        printf("=  3.更改已有联系人; =\n");
        printf("=  4.删除已有联系人; =\n");
        printf("=  5.显示当前通讯录; =\n");
        printf("=  6.退出通讯录程序   =\n");
        printf("=======================\n");
        
        do {
                printf("请输入指令:")                         ;
                gets(c)                                        ;
                switch (c[0]) {
                        case '1' : addPerson(&list); break     ;
                        case '2' : SearchPerson(&list) ; break ;
                        case '3' : changePerson(&list); break  ;
                        case '4' : delPerson(&list); break     ;
                        case '5' : displayPerson(&list); break ;
                        case '6' : break                       ;
                        default  : break                       ;
                }
        } while (c[0] != '6')        ;
        printf("感谢使用本程序!\n") ;
        free(list)                   ;
        return 0                     ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-5 18:24:44 | 显示全部楼层
jackz007 发表于 2019-2-5 04:13
你的代码问题很多,几乎无法实现预设的所有功能,我已经帮你全面修改了一下,当然,可能还有未发现的错 ...

昂,多谢啦,在一点点改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 08:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表