c - 运行时检查失败 #2 - 变量 'd' 周围的堆栈已损坏。 (Visual Studio下的C编程)

标签 c visual-studio visual-c++

你好我是新来的,用 C 编程。我不想问一些你可能认为简单的问题,但我已经问过我的同学甚至我的编程老师,看看他们是否能找到错误,但直到今天他们都找不到找不到它(他们)。

但首先让我描述一下我所知道的,它说:

"Run-Time Check Failure #2 - Stack variable "d" (and sometimes m and other y) was corrupted ".

我做了我的工作试图调试它但是问题总是显示在最后一个代码行(主体的),所以我找不到问题的确切位置,在这里我附上代码,我会如果您发现问题并向我解释我得到它的原因(以后不要重复同样的错误),我将非常高兴 =D。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Type Declaration */
#define true 1
#define false 0
typedef char boolean;

/* Functions declaration */

boolean test( short int d, short int m, long int y );
boolean nextday( short int d, short int m , long int y );

int main( void )
{
    /* Variables initialization */
    short int d = 0, m = 0; 
    long int y = 0;

    do {
        /* Data by user*/
        printf( "Ingrese el numero de año: " );
        scanf( "%ld", &y );
    }   while ( y < 0 );

    do {
        printf( "Ingrese el numero de mes: " );
        scanf( "%d", &m );  
    }   while ( m < 1 || m > 12 );

    do {
        printf( "Ingrese el numero de dia: " );
        scanf( "%d", &d );
    }   while ( d < 01 || ( test( d, m, y ) == false ) ); // If the data is wrong then re-type the data


    // If the nextday function return value is true then the next day is 01, if not just sum a day
    if ( nextday ( d, m, y ) == true ) { 
        d = 01;

        // If we start a new year the new month must be 01-01-01.
        if ( m == 12 ) {
            m = 01;
            y++;
        }

        // Just increase the month for any other month
        else {
            m++;
        }
    }

    else {
        d++;
    }

    printf( "Mañana será: %d-%d-%ld\n", d, m, y );

    return 0;
}

boolean test( short int d, short int m, long int y ){
    int max;

    switch(m) {
        case 1:       
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            max = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            max = 30;
            break;
        case 2:
            if ( y % 400 == 0 ){
                max = 29;
            }
            else if ( y % 100 == 0 ){
                max = 28;
            }
            else if ( y % 4 == 0 ){
                max = 29;
            }
            else {
                max = 28;
            }
            break;
    }
    if ( d <= max ){
        return true;
    }
    else {
        return false;
    }
}

boolean nextday( short int d, short int m, long int y ) {

    boolean x;

    // If it's 28-02 in a secular year * 4 then it's a leap-year. (so it has 29 days)
    if  ( m == 2 && d == 28 && y % 400 == 0 ) {
        x = false;
    }

    // If it is an end of century but it isn't 4 multiply then it only has 28 days.
    else if ( m == 2 && d == 28 && y % 100 == 0 ) { 
        x = true;
    }

    // If it just a leap year it has 29 days.
    else if ( m == 2 && d == 28 && y % 4 == 0 ) { 
        x = false;
    }

    //If it's the last day of February and it's a leap year.
    else if ( m == 2 && d == 29 && y % 4 == 0 ){
        x = true;
    }

    // If we are in the end of the month.
    else if ( ( d == 30 && ( m == 4 || m == 6 || m == 9 || m == 11 ) ) || 
                d == 31 ) {  
        x = true;
    }

    // Then if it is another day just sum a day
    else {
        x = false;
    }

    return x;
}

最佳答案

你需要使用:

scanf( "%hd", &m );
scanf( "%hd", &d );

因为它们是短整数。

使用“%d”,您基本上是在 small int 存储空间中加载一个 int 大小的变量。

int 通常是 4 个字节,而 small int 是 2 个字节。

关于c - 运行时检查失败 #2 - 变量 'd' 周围的堆栈已损坏。 (Visual Studio下的C编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12537371/

相关文章:

c# - Visual Studio 如何在本地运行 Web 服务?

c# - 与 C#/NET 中的控制台应用程序相关的问题

c++ - 警告 C4800 : 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)

c - "if"的 gnu C 条件

无法将 ‘char (*)[1024]’ 转换为 ‘char*’ 作为返回

c - 我正在寻找如何对我的 8 拼图进行两次(BFS、DFS)搜索。我已经快完成了

c++ - 新的,Microsoft 除外

c - 汇编语言8085程序计算前20个偶数之和(8位)

visual-studio - Visual Studio 2010 + .Net Framework 1.1 + 单击一次部署

c++ - Windows 密码过滤器 DLL 未加载