【问题标题】:Return Value 3221225477?返回值 3221225477?
【发布时间】:2019-12-16 19:43:07
【问题描述】:

我对编码很陌生,所以请放轻松

这是我的代码

/* Open File*/
FILE *openFile(char filename[], char filetype[]);
/* Read Number Of Nurses */
int readNurses(void);
/* Title */
void title(void);
/* Login Screen*/
int loginScreen();
/* Checks if memory allocation was correct */
void mallocCheck(char **malloc1, char **malloc2);
/* Asks for login details */
int askLogin(void);
/* Retrieve Login Details */
void getDetails(char **properID, char **properPASS, int nurses);
/* Test Details Input To Details */
int testDetails(void);
/* Encryption Of Password */
void encryptPass(void);
/* Main Menu */
void mainMenu(void);
/* Encrypt Patient's Details */
void encrtpyPatient(void);
/* Enter Patients Details */
void enterPatients(void);
/* Create Patients File */
void createPatient(void);
/* Clear Screen Smooth */
void clearScreen(void); 

int main(void)
{
    int nurses;
    int correct;
    char enterID[ID_LENGTH];
    nurses = readNurses();
    correct = loginScreen(enterID, nurses);
    title();
    return 0;
}

void title(void)
{
    printf("\n\n\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =  Welcome to Action On Weight  =");
    Sleep(SLEEP_TIME);
    printf("\n            =                               =");
    Sleep(SLEEP_TIME);
    printf("\n            =================================");
    Sleep(SLEEP_TIME);
    printf("\n                    *****************");
    printf("\n               ******               ******");
    printf("\n           ****                           ****");
    printf("\n        ****                                 ***");
    printf("\n      ***                                       ***");
    printf("\n     **           ***               ***           **");
    printf("\n   **           *******           *******           **");
    printf("\n   **           *******           *******           **");
    Sleep(SLEEP_TIME);
    printf("\n   **           *******           *******           **");
    printf("\n   **             ***               ***             **");
    printf("\n   **                                               **");
    printf("\n   **      *                                 *      **");
    printf("\n   **     **                                 **     **");
    printf("\n   **  ***                                     ***  **");
    printf("\n   **     *                                  *      **");
    printf("\n   **      ***                           ***        **");
    Sleep(SLEEP_TIME);
    printf("\n   ***      ****                       ****       ***");
    printf("\n     **         ******             ******         **");
    printf("\n      ***            ***************            ***");
    printf("\n        ****                                 ****");
    printf("\n           ****                           ****");
    Sleep(SLEEP_TIME); 
    printf("\n               ******               ******");
    printf("\n                    *****************");
    Sleep(SLEEP_TIME);
    Sleep(SLEEP_TIME);
}


int readNurses(void)
{
    FILE *fin;
    int nurses;
    fin = openFile("Nurses.txt", "r");
    while(!feof(fin))
    {
        fscanf(fin, "%*s");
        nurses++;
    }
    fclose(fin);
    printf("%d", nurses);
    return nurses;
}

FILE *openFile(char filename[], char filetype[])
{
    FILE *ptr = fopen(filename, filetype);
    if(!ptr)
    {
        printf("\n\n\n        Error 1. File couldn't be opened.");
              (" Please contact us at email@example.com");
              exit(1);
    }
    return ptr;
}

int loginScreen(char enterID[], int nurses)
{
    int correctDetails;
    int loop;
    char *properID;
    char *properPASS;
    properID = (char*) malloc(ID_LENGTH * nurses * 60);
    properPASS = (char*) malloc(PASS_LENGTH * nurses *60);
    correctDetails = 0;
    mallocCheck(&properID, &properPASS);
    getDetails(&properID, &properPASS, nurses);
    loop = nurses - 1;
    printf("das");
    while(nurses > 0)
    {
        printf("%s : %s", &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    /* do
    {
        printf("\n\n\n              ================================");
        printf("\n              =                              =");
        printf("\n              =         Login Screen         =");
        printf("\n              =                              =");
        printf("\n              ================================");
    }while(correctDetails = 0); */
    while(loop > 0)
    {
        free(properID - loop);
        free(properPASS - loop);        
        loop--;
    }
    return correctDetails;
}

void mallocCheck(char **malloc1, char **malloc2)
{
    if(malloc1 == NULL ||malloc2 == NULL)
    {
        printf("\n\n\n        Error 2. Assignment of ");
        printf(" memory wasn't succcesful.");
        printf(" Please contact us at email@example.com");
        Sleep(SLEEP_TIME);
        Sleep(SLEEP_TIME);
        exit(0);
    }
}

void getDetails(char **properID, char **properPASS, int nurses)
{
    FILE *ptr;
    ptr = openFile("Nurses.txt", "r");
    while(!feof(ptr))
    {
        fscanf(ptr, "%[^.]%*c%s\n", 
        &properID[nurses], &properPASS[nurses]);
        nurses--;
    }
    fclose(ptr);
}

问题出在函数 loginScreen() 中,函数 getDetails() 开始运行,但是一旦它完成 3221225477,谁能指出为什么会发生这种情况?看起来我正在正确分配内存和一切。我试图找出为什么会发生这种情况,但我找不到任何东西。

【问题讨论】:

  • 也许用 0 初始化护士。另外,你要数什么?
  • 这很奇怪,因为该函数中的拼写错误while(correctDetails = 0)
  • 您的指针使用在某些地方不正确。 getDetails(&properID, &properPASS, nurses); 您需要传递包含已分配内存地址的指针。相反,您将指针传递给指针。也就是应该是getDetails(properID, properPASS, nurses);
  • Oh mybad:你评论了那部分,但我也注意到int loginScreen() 是一个缺少完整声明的原型。请发布显示问题的Minimal Reproducible Example。这还应该显示您正在使用的#include 文件,以及诸如ID_LENGTH 之类的缺失值。我还建议您使用tour 并阅读How do I ask a good question?

标签: c memory memory-management c99


【解决方案1】:

3221225477 是十六进制的 0xC0000005,即NTSTATUS 代码 STATUS_ACCESS_VIOLATION。你的程序以某种方式破坏了它的工作记忆。改为在 Linux 上编译程序;然后你可以在valgrind 下运行它,并被告知问题出在哪里。

(valgrind 是一个非常有价值的调试工具,我建议所有 C 程序员保留 Linux 安装,即使他们不需要它用于其他任何事情,只是这样他们就可以使用 valgrind。不幸的是,它从未被移植到 Windows给你。如果你现在不想花几个小时重新配置你的计算机以实现永久双引导,你可以通过从Ubuntu liveCD 引导来临时访问 Linux。)

没有理由期望您将 3221225477 识别为 Windows NT 低级错误代码。在您的 IDE 上提交错误报告;它应该为你解码这些。

【讨论】:

  • “在 Linux 上编译你的程序……” 这是一个艰巨的任务!安装或实时启动不同的操作系统只是为了使用一个工具对我来说太夸张了。
  • @machine_1 valgrind 就是这么好。我什至不是在开玩笑。
【解决方案2】:

如果您查看signature of fscanf() function,它会说

scanf() 系列函数根据如下所述的格式扫描输入。此格式可能包含转换说明符;此类转换的结果(如果有)通过指针参数存储

你的getDetails() 函数需要三个参数;第一个是指向字符指针的指针,又名 **char*char[],最后一个是 int .

void getDetails(char **properID, char **properPASS, int nurses)

在您的fscanf() 调用中,您传递了properIDproperPASS 参数的地址,即使这些参数本身就是指针。如果您更新您的fscanf() 调用如下所示,您的代码应该可以正常工作。

fscanf(ptr, "%[^.]%*c%s\n", properID[nurses], properPASS[nurses]);

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多