【问题标题】:Program crashing near the end of compiling程序在编译快结束时崩溃
【发布时间】:2018-03-13 03:55:26
【问题描述】:

我有一个程序用于获取输入并将其存储到结构中。这些结构用于联系详细信息、姓名、地址和电话号码。我的程序运行良好,我可以将所有信息输入到程序中,但是当我尝试printf 时,结果程序在中途崩溃。我认为它们可能是内存或损坏或其他问题。我认为它可能与损坏有关的原因是,如果我切断我的一些程序并编译它,而不是程序崩溃,我会得到一个“运行时检查失败#2 变量 'optionAddress' 周围的堆栈已损坏”错误.这是我的程序

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "contacts.h"
int main(void)
{
    // Declare variables here:

    struct Name names;
    char optionName;
    struct Address addresses;
    char optionAddress;
    struct Numbers number;
    char optionCell;
    char optionHome;
    char optionBusiness;

    // Display the title

    printf("Contact Management System\n");
    printf("-------------------------\n");

    // Contact Name Input:

    printf("Please enter the contact's first name: ");
    scanf("%s", &names.firstName);
    printf("Do you want to enter a middle initial(s)? (y or n): ");
    scanf("%s", &optionName);
    while (optionName == 'y' || optionName == 'Y') {
        printf("Please enter the contact's middle initial(s): ");
        scanf("%s", &names.middleInitial);
        break;
    }
    printf("Please enter the contact's last name: ");
    scanf("%s", &names.lastName);

    // Contact Address Input:

    printf("Please enter the contact's street number: ");
    scanf("%s", &addresses.streetNumber);
    printf("Please enter the contact's street name: ");
    scanf("%s", &addresses.street);
    printf("Do you want to enter an apartment number? (y or n): ");
    scanf("%s", &optionAddress);
    while (optionAddress == 'y' || optionAddress == 'Y') {
        printf("Please enter the contact's apartment number: ");
        scanf(" %c", &addresses.apartmentNumber);
        break;
    }
    printf("Please enter the contact's postal code: ");
    scanf("%s", &addresses.postalCode);
    printf("Please enter the contact's city: ");
    scanf("%s", &addresses.city);

    // Contact Numbers Input:

    printf("Do you want to enter a cell phone number? (y or no): ");
    scanf("%s", &optionCell);
    while (optionCell == 'y' || optionCell == 'Y') {
        printf("Please enter the contact's cell phone number: ");
        scanf(" %c", number.cell);
        break;
    }
    printf("Do you want to enter a home phone number? (y or n): ");
    scanf("%s", &optionHome);
    while (optionHome == 'y' || optionHome == 'Y') {
        printf("Please enter the contact's home phone number: ");
        scanf(" %c", &number.home);
        break;
    }
    printf("Do you want to enter a business phone number? (y or n): ");
    scanf("%s", &optionBusiness);
    while (optionBusiness == 'y' || optionBusiness == 'Y') {
        printf("Please enter the contact's business phone number: ");
        scanf(" %c", number.business);
        break;
    }

    // Display Contact Summary Details

    printf("Contact Details\n");
    printf("---------------\n");
    printf("Name Details\n");
    printf("First name: ");
    printf("%s", names.firstName);
    printf("\nMiddle initials(s): ");
    printf("%s", names.middleInitial);
    printf("\nLast name: ");
    printf("%s", names.lastName);
    printf("\n\nAddress Details\n");
    printf("Street number: ");
    printf("%s", addresses.streetNumber);
    printf("\nStreet name: ");
    printf("%s", addresses.street);
    printf("\nApartment: ");
    printf("%s", addresses.apartmentNumber);
    printf("\nPostal code: ");
    printf("%s", addresses.postalCode);
    printf("\nCity: ");
    printf("%s", addresses.city);
    printf("\n\nPhone Numbers: ");
    printf("\nCell phone number: ");
    printf("%s", number.cell);
    printf("\nHome phone number: ");
    printf("%s", number.home);
    printf("\nBusiness phone number: ");
    printf("%s", number.business);

    // Display Completion Message

    printf("\n\nStructure test for Name, Address, and Numbers Done!");

    return 0;
}

以及头文件中的结构体:

// Structure type Name declaration
struct Name {
    char firstName[31];
    char middleInitial[7];
    char lastName[36];
};

// Structure type Address declaration
// Place your code here...

 struct Address {
    char streetNumber;
    char street[41];
    char apartmentNumber;
    char postalCode[8];
    char city[41];
 };

 // Structure type Numbers declaration
 // Place your code here...

 struct Numbers {
    char cell[21];
    char home[21];
    char business[21];   
 };

我的程序达到打印出"Street number: " 的程度,然后停止工作。这是一个显示的 Windows 错误窗口,而不是编译器窗口。

【问题讨论】:

  • StreetNumber 是 char,但您输入为字符串:scanf("%s", &amp;addresses.streetNumber);
  • 编译时,始终启用警告,然后修复这些警告。 (对于gcc, at a minimum use: -Wall -Wextra -Wconversion -pedantic -std=gnu11`)这会导致编译器输出10个警告,主要是关于调用scanf()的格式字符串,其中输入/格式说明符与数据不匹配设置。
  • 在调用任何scanf() 系列函数时 1) 始终检查返回值以确保操作成功。 2) 使用%s 和/或%[...] 说明符时,始终包含一个最大字符修饰符,该修饰符比输入缓冲区的长度小1,因为这些说明符始终将NUL 字节附加到输入。这也避免了缓冲区溢出及其导致的未定义行为和可能的段错误事件的任何可能性。
  • 关于:scanf(" %c", &amp;addresses.apartmentNumber); 我的公寓号码是764,它不适合单个字符。事实上,几乎所有公寓号码的长度都是多个字符,可以是:22A,所以不是严格的数字。
  • 关于:char postalCode[8]; 对于许多邮政编码来说,这还不够长。例如在美国,邮政编码由一个 5 位数字、一个空格或破折号、一个 4 位数字组成。一个 8 字符的字段只够容纳 5 位数字、NUL 终止符和另外 2 个字符。

标签: c struct scanf


【解决方案1】:

我的程序会打印出“街道号码:”然后 停止工作。

看看:

 struct Address {
    char streetNumber;    // declared as character!
    char street[41];
    char apartmentNumber; // character
    char postalCode[8];
    char city[41];
 };

char optionName;
char optionAddress;
char optionCell;
char optionHome;
char optionBusiness;

稍后您尝试读取字符的值:

scanf("%s", &optionName);
scanf("%s", &optionAddress);
scanf("%s", &optionCell);
scanf("%s", &optionHome);
scanf("%s", &optionBusiness);

scanf("%s", &addresses.streetNumber); // <-------------- string read

使用字符串格式%s。这会调用 UB,因为您正在读取至少 2 字节。 "y" 是一个带有字符串空终止符'\0' 的字符串。那个额外的字节会覆盖内存位置。 将变量声明更改为字符串或将读取格式更改为字符读取,就像您在此处所做的那样:

  scanf(" %c", &addresses.apartmentNumber);

【讨论】:

  • 只要所有街道号码都在-128和127之间,他应该没问题:)
【解决方案2】:
scanf("%s", &optionName);

这会引发未定义的行为,这可能会导致运行时错误,因为 optionName 是 char 一个字节,而带有 %s 的 scanf() 将尝试写入 '\0',这肯定会超出界限

【讨论】:

  • 我尝试改用 optionName[2],但是当我这样做时,它会使选项的 while 循环停止工作,并且程序最后仍然崩溃。我应该以其他方式更改 optionName 吗?
  • 您可以将optionName保留为char,并将scanf更改为scanf(" %c", &optionName);以便它处理先前 scanf 中的空白
猜你喜欢
  • 2011-01-04
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 2021-08-22
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多