鱼C论坛

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

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

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

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

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

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

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

  4. struct Cont
  5. {
  6.         char name[20];
  7.         char phone[12];
  8.         char company[40];
  9.         struct Cont *next;
  10. };

  11. void addPerson(struct Cont **);
  12. struct Cont *findPerson(struct Cont **);
  13. void changePerson(struct Cont **);
  14. void delPerson(struct Cont **);
  15. void displayPerson(struct Cont **);
  16. void Input(struct Cont *);
  17. void Print(struct Cont *);

  18. void Input(struct Cont *news)
  19. {
  20.         printf("请输入姓名:");
  21.         scanf("%s", news->name);
  22.         printf("请输入电话:");
  23.         scanf("%s", news->phone);
  24.         printf("请输入公司:");
  25.         scanf("%s", news->company);
  26.         getchar();
  27. }

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

  34. void addPerson(struct Cont **data)
  35. {
  36.         struct Cont *person, *temp;
  37.         static struct Cont *tail;

  38.         person = (struct Cont *)malloc(sizeof(struct Cont));
  39.         if (person == NULL);
  40.         {
  41.                 printf("内存分配失败!\n");
  42.                 exit(1);
  43.         }

  44.         Input(person);

  45.         while (1)
  46.         {
  47.                 if (*data == NULL)
  48.                 {
  49.                         *data = person;
  50.                         person->next = NULL;
  51.                 }
  52.                 else
  53.                 {
  54.                         tail->next = person;
  55.                         person->next = NULL;
  56.                         break;
  57.                 }
  58.         }
  59. }

  60. struct Cont *findPerson(struct Cont **data)
  61. {
  62.         char input[20];
  63.         struct Cont *search;
  64.        
  65.         search = *data;
  66.         scanf("%s", input);
  67.         getchar();
  68.         while (search != NULL)
  69.         {
  70.                 if (!strcmp(search->name, input))
  71.                 {
  72.                         break;
  73.                 }
  74.                 else
  75.                 {
  76.                         search = search->next;
  77.                 }
  78.         }
  79.        
  80.         if (search == NULL)
  81.         {
  82.                 printf("抱歉,未找到该联系人!\n");
  83.                 return search;
  84.         }
  85.        
  86.         Print(search);
  87.        
  88.         return search;
  89. }

  90. void changePerson(struct Cont **data)
  91. {
  92.         struct Cont *search;
  93.         char ch;

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

  96.         if (search == NULL)
  97.         {
  98.                 printf("请问是否添加新的联系人(Y/N)?");
  99.                 do
  100.                 {
  101.                         scanf("%c", &ch);
  102.                         getchar();
  103.                 } while (ch != 'Y' && ch != 'N');
  104.         }
  105.         else
  106.         {
  107.                 printf("请问是否更改该联系人信息(Y/N)?");
  108.                 do
  109.                 {
  110.                         scanf("%c", &ch);
  111.                         getchar();
  112.                 } while (ch != 'Y' && ch != 'N');
  113.         }
  114.        
  115.         if (ch == 'N')
  116.         {
  117.                 return;
  118.         }
  119.         addPerson(&search);
  120. }

  121. void delPerson(struct Cont **data)
  122. {
  123.         struct Cont *previous, *temp;
  124.         char ch;
  125.        
  126.         printf("请输入要删除的联系人:");
  127.         previous = findPerson(data);
  128.        
  129.         if (previous == NULL)
  130.         {
  131.                 printf("未找到该联系人!\n");
  132.                 return;
  133.         }
  134.        
  135.         printf("请问是否删除该联系人信息(Y/N)?");
  136.         do
  137.         {
  138.                 scanf("%c", &ch);
  139.                 getchar();
  140.         } while (ch != 'Y' && ch != 'N');
  141.        
  142.         if (ch == 'N')
  143.         {
  144.                 return;
  145.         }

  146.         temp = previous;
  147.         previous = previous->next;

  148.         free(temp);
  149. }

  150. void displayPerson(struct Cont **data)
  151. {
  152.         while (*data != NULL)
  153.         {
  154.                 Print(*data);
  155.                 *data = (*data)->next;
  156.         }
  157. }

  158. int main(void)
  159. {
  160.         struct Cont *list = NULL;
  161.         int index;
  162.        
  163.         printf("=======================\n");
  164.         printf("=   欢迎使用本程序!  =\n");
  165.         printf("=   (指令功能如下) : =\n");
  166.         printf("=  1.插入新的联系人; =\n");
  167.         printf("=  2.查找已有联系人; =\n");
  168.         printf("=  3.更改已有联系人; =\n");
  169.         printf("=  4.删除已有联系人; =\n");
  170.         printf("=  5.显示当前通讯录; =\n");
  171.         printf("=  6.退出通讯录程序   =\n");
  172.         printf("=======================\n");
  173.        
  174.         do
  175.         {
  176.                 printf("请输入指令:");
  177.                 scanf("%d", &index);
  178.                 getchar();
  179.                 switch (index)
  180.                 {
  181.                         case 1 : addPerson(&list); break;
  182.                         case 2 : printf("请输入要查找的联系人:");findPerson(&list); break;
  183.                         case 3 : changePerson(&list); break;
  184.                         case 4 : delPerson(&list); break;
  185.                         case 5 : displayPerson(&list); break;
  186.                         case 6 : break;
  187.                         default : break;
  188.                 }
  189.         } while (index != 6);
  190.        
  191.         printf("感谢使用本程序!\n");
  192.        
  193.         free(list);
  194.        
  195.         return 0;
  196. }
复制代码




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

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

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

    由于改动的地方较多,就不一一叙述,楼主就参照自己的代码参考吧。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Cont
  5. {
  6.         char name[20];
  7.         char phone[12];
  8.         char company[40];
  9.         struct Cont *next;
  10. };

  11. void addPerson(struct Cont **);
  12. struct Cont *findPerson(struct Cont **);
  13. void changePerson(struct Cont **);
  14. void delPerson(struct Cont **);
  15. void displayPerson(struct Cont **);
  16. void Input(struct Cont *);
  17. void Print(struct Cont *);

  18. void Input(struct Cont * news)
  19. {
  20.         printf("请输入姓名:")     ;
  21.         scanf("%s", news->name)    ;
  22.         printf("请输入电话:")     ;
  23.         scanf("%s", news->phone)   ;
  24.         printf("请输入公司:")     ;
  25.         scanf("%s", news->company) ;
  26. }

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

  33. void addPerson(struct Cont **data)
  34. {
  35.         struct Cont * person , * tail , * temp                   ;

  36.         person = (struct Cont *)malloc(sizeof(struct Cont))      ;
  37.         if (person == NULL) {
  38.                 printf("内存分配失败!\n")                       ;
  39.                 exit(1)                                          ;
  40.         }
  41.         Input(person)                                            ;
  42.         person -> next = NULL                                    ;
  43.         if (* data == NULL) {
  44.                 * data = person                                  ;
  45.         } else {
  46.                 tail = * data                                    ;
  47.                 while (tail -> next != NULL) tail = tail -> next ;
  48.                 tail -> next = person                            ;
  49.         }
  50. }

  51. struct Cont * findPerson(struct Cont **data)
  52. {
  53.         char input[20]                                      ;
  54.         struct Cont * search                                ;
  55.         
  56.         search = * data                                     ;
  57.         printf("请输入姓名:")                              ;
  58.         scanf("%s" , input)                                 ;
  59.         while (search != NULL) {
  60.                 if (! strcmp(search -> name , input)) break ;
  61.                 search = search -> next                     ;
  62.         }
  63.         return search                                       ;                                       
  64. }

  65. struct Cont * SearchPerson(struct Cont **data)
  66. {
  67.         struct Cont * search                                ;
  68.         search = findPerson(data)                           ;
  69.         if(search == NULL) printf("抱歉,联系人未找到\n")   ;
  70.         else Print(search)                                  ;
  71.         return search                                       ;
  72. }

  73. void changePerson(struct Cont **data)
  74. {
  75.         struct Cont * search                                               ;
  76.         char ch                                                            ;  
  77.         search = findPerson(data)                                          ;
  78.         if (search == NULL) {
  79.                 printf("抱歉,没有找到该联系人信息\n")                     ;
  80.                 printf("请问是否添加新的联系人(Y/N)?")                    ;
  81.                 do {
  82.                         ch = getchar()                                     ;
  83.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
  84.                 if (ch == 'Y' || ch == 'y') {
  85.                         addPerson(data)                                    ;
  86.                         printf("成功添加联系人信息\n")                     ;
  87.                 }
  88.         } else {
  89.                 printf("请问是否更改该联系人信息(Y/N)?")                  ;
  90.                 do {
  91.                         ch = getchar()                                     ;
  92.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
  93.                 if (ch == 'Y' || ch == 'y') {
  94.                         Input(search)                                      ;        
  95.                         printf("成功修改联系人信息\n")                     ;
  96.                 }
  97.         }
  98. }

  99. void delPerson(struct Cont **data)
  100. {
  101.         struct Cont * previous , * temp       ;
  102.         char ch                               ;
  103.         bool f                                ;
  104.         
  105.         temp = findPerson(data)               ;
  106.         if (temp == NULL) {
  107.                 printf("未找到该联系人!\n")  ;
  108.         } else {
  109.                 printf("请问是否删除该联系人信息(Y/N)?")                       ;
  110.                 do {
  111.                         ch = getchar()                                          ;
  112.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n')      ;
  113.                 if(ch == 'Y' || ch == 'y') {
  114.                         f = false                                               ;
  115.                         if (temp == * data) {
  116.                                 * data = temp -> next                           ;
  117.                                 f = true                                        ;   
  118.                         } else {
  119.                                 previous = * data                               ;
  120.                                 while (previous != NULL) {
  121.                                         if (previous -> next == temp) {
  122.                                                 previous -> next = temp -> next ;
  123.                                                 f = true                        ;
  124.                                                 break                           ;
  125.                                         }
  126.                                 }
  127.                         }
  128.                         if (f) {
  129.                                 free(temp)                                      ;
  130.                                 printf("成功删除该联系人!\n")                  ;
  131.                         } else {
  132.                                 printf("抱歉,删除联系人失败!\n")              ;
  133.                         }
  134.                 }
  135.         }
  136. }

  137. void displayPerson(struct Cont **data)
  138. {
  139.         struct Cont * walk                   ;
  140.         walk = * data                        ;
  141.         if (walk != NULL) {
  142.                 while (walk != NULL) {
  143.                         Print(walk)          ;
  144.                         walk = walk -> next  ;
  145.                 }
  146.         } else {
  147.                 printf("抱歉,尚无数据录入") ;
  148.         }
  149. }

  150. int main(void)
  151. {
  152.         struct Cont *list = NULL , * p     ;
  153.         char c[20]                         ;
  154.         
  155.         printf("=======================\n");
  156.         printf("=   欢迎使用本程序!  =\n");
  157.         printf("=   (指令功能如下) : =\n");
  158.         printf("=  1.插入新的联系人; =\n");
  159.         printf("=  2.查找已有联系人; =\n");
  160.         printf("=  3.更改已有联系人; =\n");
  161.         printf("=  4.删除已有联系人; =\n");
  162.         printf("=  5.显示当前通讯录; =\n");
  163.         printf("=  6.退出通讯录程序   =\n");
  164.         printf("=======================\n");
  165.         
  166.         do {
  167.                 printf("请输入指令:")                         ;
  168.                 gets(c)                                        ;
  169.                 switch (c[0]) {
  170.                         case '1' : addPerson(&list); break     ;
  171.                         case '2' : SearchPerson(&list) ; break ;
  172.                         case '3' : changePerson(&list); break  ;
  173.                         case '4' : delPerson(&list); break     ;
  174.                         case '5' : displayPerson(&list); break ;
  175.                         case '6' : break                       ;
  176.                         default  : break                       ;
  177.                 }
  178.         } while (c[0] != '6')        ;
  179.         printf("感谢使用本程序!\n") ;
  180.         free(list)                   ;
  181.         return 0                     ;
  182. }
复制代码
屏幕截图(2).png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

使用道具 举报

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

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

    由于改动的地方较多,就不一一叙述,楼主就参照自己的代码参考吧。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Cont
  5. {
  6.         char name[20];
  7.         char phone[12];
  8.         char company[40];
  9.         struct Cont *next;
  10. };

  11. void addPerson(struct Cont **);
  12. struct Cont *findPerson(struct Cont **);
  13. void changePerson(struct Cont **);
  14. void delPerson(struct Cont **);
  15. void displayPerson(struct Cont **);
  16. void Input(struct Cont *);
  17. void Print(struct Cont *);

  18. void Input(struct Cont * news)
  19. {
  20.         printf("请输入姓名:")     ;
  21.         scanf("%s", news->name)    ;
  22.         printf("请输入电话:")     ;
  23.         scanf("%s", news->phone)   ;
  24.         printf("请输入公司:")     ;
  25.         scanf("%s", news->company) ;
  26. }

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

  33. void addPerson(struct Cont **data)
  34. {
  35.         struct Cont * person , * tail , * temp                   ;

  36.         person = (struct Cont *)malloc(sizeof(struct Cont))      ;
  37.         if (person == NULL) {
  38.                 printf("内存分配失败!\n")                       ;
  39.                 exit(1)                                          ;
  40.         }
  41.         Input(person)                                            ;
  42.         person -> next = NULL                                    ;
  43.         if (* data == NULL) {
  44.                 * data = person                                  ;
  45.         } else {
  46.                 tail = * data                                    ;
  47.                 while (tail -> next != NULL) tail = tail -> next ;
  48.                 tail -> next = person                            ;
  49.         }
  50. }

  51. struct Cont * findPerson(struct Cont **data)
  52. {
  53.         char input[20]                                      ;
  54.         struct Cont * search                                ;
  55.         
  56.         search = * data                                     ;
  57.         printf("请输入姓名:")                              ;
  58.         scanf("%s" , input)                                 ;
  59.         while (search != NULL) {
  60.                 if (! strcmp(search -> name , input)) break ;
  61.                 search = search -> next                     ;
  62.         }
  63.         return search                                       ;                                       
  64. }

  65. struct Cont * SearchPerson(struct Cont **data)
  66. {
  67.         struct Cont * search                                ;
  68.         search = findPerson(data)                           ;
  69.         if(search == NULL) printf("抱歉,联系人未找到\n")   ;
  70.         else Print(search)                                  ;
  71.         return search                                       ;
  72. }

  73. void changePerson(struct Cont **data)
  74. {
  75.         struct Cont * search                                               ;
  76.         char ch                                                            ;  
  77.         search = findPerson(data)                                          ;
  78.         if (search == NULL) {
  79.                 printf("抱歉,没有找到该联系人信息\n")                     ;
  80.                 printf("请问是否添加新的联系人(Y/N)?")                    ;
  81.                 do {
  82.                         ch = getchar()                                     ;
  83.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
  84.                 if (ch == 'Y' || ch == 'y') {
  85.                         addPerson(data)                                    ;
  86.                         printf("成功添加联系人信息\n")                     ;
  87.                 }
  88.         } else {
  89.                 printf("请问是否更改该联系人信息(Y/N)?")                  ;
  90.                 do {
  91.                         ch = getchar()                                     ;
  92.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') ;
  93.                 if (ch == 'Y' || ch == 'y') {
  94.                         Input(search)                                      ;        
  95.                         printf("成功修改联系人信息\n")                     ;
  96.                 }
  97.         }
  98. }

  99. void delPerson(struct Cont **data)
  100. {
  101.         struct Cont * previous , * temp       ;
  102.         char ch                               ;
  103.         bool f                                ;
  104.         
  105.         temp = findPerson(data)               ;
  106.         if (temp == NULL) {
  107.                 printf("未找到该联系人!\n")  ;
  108.         } else {
  109.                 printf("请问是否删除该联系人信息(Y/N)?")                       ;
  110.                 do {
  111.                         ch = getchar()                                          ;
  112.                 } while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n')      ;
  113.                 if(ch == 'Y' || ch == 'y') {
  114.                         f = false                                               ;
  115.                         if (temp == * data) {
  116.                                 * data = temp -> next                           ;
  117.                                 f = true                                        ;   
  118.                         } else {
  119.                                 previous = * data                               ;
  120.                                 while (previous != NULL) {
  121.                                         if (previous -> next == temp) {
  122.                                                 previous -> next = temp -> next ;
  123.                                                 f = true                        ;
  124.                                                 break                           ;
  125.                                         }
  126.                                 }
  127.                         }
  128.                         if (f) {
  129.                                 free(temp)                                      ;
  130.                                 printf("成功删除该联系人!\n")                  ;
  131.                         } else {
  132.                                 printf("抱歉,删除联系人失败!\n")              ;
  133.                         }
  134.                 }
  135.         }
  136. }

  137. void displayPerson(struct Cont **data)
  138. {
  139.         struct Cont * walk                   ;
  140.         walk = * data                        ;
  141.         if (walk != NULL) {
  142.                 while (walk != NULL) {
  143.                         Print(walk)          ;
  144.                         walk = walk -> next  ;
  145.                 }
  146.         } else {
  147.                 printf("抱歉,尚无数据录入") ;
  148.         }
  149. }

  150. int main(void)
  151. {
  152.         struct Cont *list = NULL , * p     ;
  153.         char c[20]                         ;
  154.         
  155.         printf("=======================\n");
  156.         printf("=   欢迎使用本程序!  =\n");
  157.         printf("=   (指令功能如下) : =\n");
  158.         printf("=  1.插入新的联系人; =\n");
  159.         printf("=  2.查找已有联系人; =\n");
  160.         printf("=  3.更改已有联系人; =\n");
  161.         printf("=  4.删除已有联系人; =\n");
  162.         printf("=  5.显示当前通讯录; =\n");
  163.         printf("=  6.退出通讯录程序   =\n");
  164.         printf("=======================\n");
  165.         
  166.         do {
  167.                 printf("请输入指令:")                         ;
  168.                 gets(c)                                        ;
  169.                 switch (c[0]) {
  170.                         case '1' : addPerson(&list); break     ;
  171.                         case '2' : SearchPerson(&list) ; break ;
  172.                         case '3' : changePerson(&list); break  ;
  173.                         case '4' : delPerson(&list); break     ;
  174.                         case '5' : displayPerson(&list); break ;
  175.                         case '6' : break                       ;
  176.                         default  : break                       ;
  177.                 }
  178.         } while (c[0] != '6')        ;
  179.         printf("感谢使用本程序!\n") ;
  180.         free(list)                   ;
  181.         return 0                     ;
  182. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> 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-5-13 20:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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