realloc的导致程序崩溃 [英] Realloc causing program to crash

查看:377
本文介绍了realloc的导致程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前试图写基于菜单的程序,要求用户输入学生的记录,经过最初的记录进入我希望用户拥有选择添加更多的记录插槽,但是当我尝试realloc的我二维指针数组我的程序崩溃,特别是后第二个函数被调用。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#定义长度为21个
无效printRecords为(int *的大小,字符** fistName,焦炭**姓氏,浮动*等级);
无效的addRecord(INT *的大小,字符**名字,焦炭**姓氏,浮动*等级);INT主要(无效){/ *最小测量检查* /
    INT大小= 0,*检查=(INT *)malloc的(的sizeof(INT));
    *检查= 1;
    而(检查){
        的printf(请说明你想进入(5分)记录数:);
        scanf函数(%d个,&安培;大小);
        如果(尺寸小于5)
            的printf(大小进入是低于最低\\ n);
        其他
            检查= 0; }
    免费(检查);                                        / *动态内存分配* /
    焦炭**的firstName =(字符**)的malloc(大小* sizeof的(字符*)),**的lastName =(字符**)的malloc(大小* sizeof的(字符*));
    浮动*等级=(浮点*)malloc的(大小*的sizeof(浮动));    INT I;
    对于(i = 0; I<大小;我++){
        的firstName [I] =(字符*)malloc的(长*的sizeof(字符));
        lastName的[I] =(字符*)malloc的(长*的sizeof(字符)); }    的printf(请学生输入记录(输入每条记录后,新行),\\ n使用以下格式名姓得分:\\ n);
    对于(I = 0; I&下;大小;(ⅰ)+)
        scanf函数(%s%S%F,&安培;的firstName [I] [0],&安培; lastName的[I] [0],&安培;等级[I]);    INT选项= 1;
    而(选择!= 0){/ *选项菜单* /
        的printf(打印记录(preSS 1)\\ n添加新的记录(preSS 2)\\ n删除记录(S)(preSS 3)\\ nSeach按姓氏(preSS 4)\\ nSort按分数(preSS 5)\\ nSort按姓氏(preSS 6)\\ n查找平均得分(preSS 7)\\ n退出程序(preSS 0)\\ n);
        scanf函数(%d个,&安培;可选);        开关(选配件){
        情况1:
            printRecords(安培;大小,名字,姓氏,等级);
            打破;
        案例2:
            的addRecord(安培;大小,名字,姓氏,等级);
            打破;
        案例3:
            打破;
        情况4:
            打破;
        情况5:
            打破;
        情况6:
            打破;
        案例7:
            打破;
        }
    }
    返回0;
}无效printRecords为(int *的大小,字符**名字,焦炭**姓氏,浮级[]){/ *选项1 * /
            INT I;
            对于(i = 0; I< *尺寸;我++)
                的printf(第一名称:%s,最后名称:%s,得分:%.2f \\ n,名字[I],姓氏[I],等级[I]);
    }无效的addRecord(INT *的大小,字符**名字,焦炭**姓氏,浮动*等级){/ *选项2 * /
    焦炭**一个;
    浮* C;
    (*大小)+ = 1;    A =(字符**)realloc的(名字,*尺寸*的sizeof(字符*)); / *错误* /
    如果(一个!= NULL)// realloc的成功
        的firstName =一;
    否则//有错误    A =(字符**)的realloc(姓氏,*尺寸*的sizeof(字符*));
    如果(一个!= NULL)// realloc的成功
        的lastName =一;
    其他; //有一个错误    C =(浮点*)的realloc(等级*尺寸*的sizeof(浮动*));
    等级= C;
    的firstName [*大小 - 1] =(字符*)malloc的(长*的sizeof(字符));
    lastName的[*大小 - 1] =(字符*)malloc的(长*的sizeof(字符));
    scanf函数(%s%S%F,&安培;的firstName [*尺寸] [0],&安培; lastName的[*尺寸] [0],&安培;等级[*尺寸]);
    }


解决方案

C是传值。

适用于的firstName 的lastName 变化的addRecord()丢失该函数返回后,由于这两个vairbales携带作为参数传递给的addRecord的调用()

的变量copeis

修改的定义的addRecord()

 无效的addRecord(INT *的大小,字符**名字,焦炭**姓氏,浮动*等级);

 无效的addRecord(INT *的大小,字符*** pfirstName,焦炭*** plastName,浮动*等级);

和里面的的addRecord()替换所有的firstName (* pfirstName)和所有的lastName (* plastName)

(Interessting不够,你做了正确的尺寸

呼叫的addRecord()是这样的:

 的addRecord(安培;大小,和放大器;名字,和放大器;姓氏,级);

Currently trying to write menu based program that asks user for student records, after initial records are entered I want user to have option to add more record slots but when I try to realloc my 2d pointer array my program crashes, specifically after the 2nd function is called.

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#define LENGTH 21
void printRecords(int* size, char **fistName, char **lastName, float *grade);
void addRecord(int* size, char** firstName, char** lastName, float *grade);

int  main(void) { /* Minimum measure check */
    int size = 0, *check = (int*)malloc(sizeof(int));
    *check = 1;
    while (check) {
        printf("Please indicate number of records you want to enter (min 5): ");
        scanf("%d", &size);
        if (size < 5)
            printf("Size entered was below the minimum.\n");
        else
            check = 0; }
    free(check);

                                        /* Dynamic Memory Allocation */
    char **firstName = (char**)malloc(size * sizeof(char*)), **lastName = (char**)malloc(size * sizeof(char*)); 
    float *grade = (float*)malloc(size * sizeof(float));

    int i;
    for (i = 0; i < size; i++) {
        firstName[i] = (char*)malloc(LENGTH *  sizeof(char));
        lastName[i] = (char*)malloc(LENGTH * sizeof(char)); }

    printf("Please input records of students (enter a new line after each record),\nwith following format first name last name score:\n");
    for (i = 0; i < size; (i)++)
        scanf("%s %s %f", &firstName[i][0], &lastName[i][0], &grade[i]);

    int option = 1;
    while (option != 0) { /* Option Menu */
        printf("Print records (press 1)\nAdd new record(press 2)\nDelete record(s)(press 3)\nSeach by last name(press 4)\nSort by score(press 5)\nSort by last name(press 6)\nFind the median score(press 7)\nExit the program(press 0)\n");
        scanf("%d", &option);

        switch (option) {
        case 1:
            printRecords(&size, firstName, lastName, grade);
            break;
        case 2:
            addRecord(&size, firstName, lastName, grade);
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        }
    }
    return 0;
}

void printRecords(int* size, char **firstName, char **lastName, float grade[]) { /* Option 1 */
            int i;
            for (i = 0; i < *size; i++)
                printf("First Name : %s, Last Name : %s, Score : %.2f\n", firstName[i], lastName[i], grade[i]); 
    }

void addRecord(int* size, char** firstName, char** lastName, float* grade) { /* Option 2 */
    char **a;
    float *c;
    (*size) += 1;

    a = (char **)realloc(firstName, *size * sizeof(char*)); /* Error */
    if (a != NULL) //realloc was successful
        firstName = a;
    else //there was an error

    a = (char **)realloc(lastName, *size * sizeof(char*));
    if (a != NULL) //realloc was successful
        lastName = a;
    else; //there was an error 

    c = (float *)realloc(grade, *size * sizeof(float*));
    grade = c;
    firstName[*size - 1] = (char*)malloc(LENGTH * sizeof(char));
    lastName[*size - 1] = (char*)malloc(LENGTH * sizeof(char));
    scanf("%s %s %f", &firstName[*size][0], &lastName[*size][0], &grade[*size]);
    }

解决方案

C is pass by value.

The changes applied to firstName and lastName in addREcord() are lost after the function returned, as those two vairbales carry copeis of the variables passed as argument to the call of addRecord().

Change the definition of addRecord() from

void addRecord(int* size, char** firstName, char** lastName, float* grade);

to be

void addRecord(int* size, char*** pfirstName, char*** plastName, float* grade);

and inside addRecord() replace all firstName with (*pfirstName) and all lastName with (*plastName)

(Interessting enough you did the correctly for size.)

Call addRecord() like this:

addRecord(&size, &firstName, &lastName, grade);

这篇关于realloc的导致程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆