C++ Access Violation Exception Occurred

Asked 2 years ago, Updated 2 years ago, 53 views

#include <iostream>
#include <stdlib.h>
#include <ctime>

class Date
{
private:
    time_t t = time(NULL);
    struct tm* ptime = localtime(&t);

    int year = ptime->tm_year + 1900;
    int month = ptime->tm_mon + 1;
    int day = ptime->tm_mday;
public:
    int getyear() {
        return year;
    }
    int getmonth() {
        return month;
    }
    int getday() {
        return day;
    }
};

int main(void) {
    Date currtime;
    char* pc1 = new char[11];
    if (pc1 == NULL) {
        printf ("Dynamic Memory Allocation Failed".\n");
        exit(1);
    }
    char* pc2 = new char[5];
    if (pc2 == NULL) {
        printf ("Dynamic Memory Allocation Failed".\n");
        exit(1);
    }
    int num, byear, bmonth, bday;
    int curryear = currtime.getyear();
    int currmonth = currtime.getmonth();
    int currday = currtime.getday();

    printf("Enter your department name, class number, name, and date of birth in order >");
    scanf_s("%s %d %s %d %d %d", pc1, 11, &num, pc2, 5, &byear, &bmonth, &bday);
    printf("%s%d%s, %d, %d, %d, %d, %d, %d, %d, %d, current age is %d."), *pc1, num, *pc2, year, bmonth, bday, curry, curr month, currrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

    delete(pc1); delete(pc2);

    return 0;
}

Continue to run the above program in the scanf_s line of the main function

0x7AD01F4C (ucrtbased.dll) has an unprocessed exception (from Project2.exe). 
0xC0000005: An access violation occurred while reading the 0xFFFFFCD location.

The exception occurs. Even if you use scanf instead of scanf_s, you must specify the memory size when you receive a string with scanf_sYou have also specified a memory size, but the exception continues. I've deactivated SDL tests, and I've tried everything. I can't figure it out. Visual Studio is in use.

c++ exception

2022-09-20 17:46

2 Answers

There are two major problems.

First of all, there is too little space to enter the name of the department. You need to increase it enough.

char* pc1 = new char[11];
char* pc2 = new char[5];

The above code should be modified, for example, as shown below. If you do not have enough space to receive the name, you may have problems when a string longer than the space is entered.

char* pc1 = new char[20];
char* pc2 = new char[20];

The second problem is that when printing a string with printf, the address value should be put where it matches %s, but in the question, the actual value through the address is not put in the address like *pc1.

printf("%s%d%s, %d%d%s, %d%d birthdate%d%d%d birthdate%d%d%d, %d%d birthdate%d%d%d"), *pc1, num, *pc2, year, bday, currrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

Please correct the code above as below.

printf("%s%d%s, %d%d%s, %d%d birthdate%d%d%d birthdate%d%d%d, %d%d birthdate%d%d"), pc1, num, pc2, year, month, bday, currrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

Please refer to the full code and results below.

#include <iostream>
#include <stdlib.h>
#include <ctime>

class Date
{
private:
    time_t t = time(NULL);
    struct tm* ptime = localtime(&t);

    int year = ptime->tm_year + 1900;
    int month = ptime->tm_mon + 1;
    int day = ptime->tm_mday;
public:
    int getyear() {
        return year;
    }
    int getmonth() {
        return month;
    }
    int getday() {
        return day;
    }
};

int main(void) {
    Date currtime;
    char* pc1 = new char[20];
    if (pc1 == NULL) {
        printf ("Dynamic Memory Allocation Failed".\n");
        exit(1);
    }
    char* pc2 = new char[20];
    if (pc2 == NULL) {
        printf ("Dynamic Memory Allocation Failed".\n");
        exit(1);
    }
    int num, byear, bmonth, bday;
    int curryear = currtime.getyear();
    int currmonth = currtime.getmonth();
    int currday = currtime.getday();

    printf("Enter your department name, class number, name, and date of birth in order >");
    scanf_s("%s %d %s %d %d %d", pc1, 20, &num, pc2, 20, &byear, &bmonth, &bday);
    printf("%s%d%s, %d, %d, %d, %d, %d, %d, %d, %d, current age is %d."), pc1, num, pc2, bye, bmonth, bday, curry, curr month, currrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

    delete(pc1); delete(pc2);

    return 0;
}


2022-09-20 17:46

VS2015 compiles and executes the code of the questioner, and the same error occurs. If you run in debug mode and look at the stack when an exception occurs:

  ucrtbased strnlen _ _, dll common c < 0, unsigned char > c++. 72 to (const unsigned char * * * * * * * const string, const unsigned int maximum the count)
    Ucrtbased strnlen _ _, dll common simd < 0, 1, unsigned char > c++. 130 to (const unsigned char * * * * * * * const string, const unsigned int maximum the count)
    Ucrtbased dll! _ common strnlen < 0, unsigned char > to (const unsigned char * * * * * * * const string, const unsigned int maximum the count), c++.
    Ucrtbased, dll strnlen c++. 201 to (const char string and unsigned int maximum the count)
        ucrtbased.dll!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s_compute_narrow_string_length(const int maximum_length, char __formal) 줄 2281  C++

    ucrtbased.dll!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::format_validation_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s() 줄 2268 C++
    Ucrtbased dll! __ crt stdio _ _ :: output output processor < char, __ crt stdio output _ _ _ :: stream output adapter < char >, __ crt stdio base _ _ :: standard output < char, __ crt stdio output _ _ _ :: stream output adapter < char > > > case _ _ :: state type c++ 2012 for ().
    Ucrtbased dll! __ crt stdio _ _ :: output output processor < char, __ crt stdio output _ _ _ :: stream output adapter < char >, __ crt stdio base _ _ :: standard output < char, __ crt stdio output _ _ _ :: stream output adapter < char > > 1657 to :: process () > c++.
    Lambda. :: __ 2 :: < l dll, the vfprintf common ucrtbased > c++. 46 () to
    ucrtbased.dll!__crt_seh_guarded_call<int>::operator()<void <람다>(void),int <람다>(void) &,void <람다>(void) >(__acrt_lock_stream_and_call::__l2::void <람다>(void) && setup, common_vfprintf::__l2::int <람다>(void) & action, __acrt_lock_stream_and_call::__l2::void <람다>(void) && cleanu204 c++ for p).
    Ucrtbased dll! __ _ acrt lock and _ _ _ stream call < int < the lambda > (void) > (* _ iobuf const stream, common vfprintf 2 :: :: __ l int < the lambda > (void) & & action) to 256 c++
    Ucrtbased dll! _ common vfprintf < the __ crt stdio base, _ _ :: standard output char > c++. 34 to (const unsigned const * __ _ iobuf and 64 options int stream, const char * * * * * * * const format, __ crt pointers * _ _ locale const locale, char const arglist)
    Ucrtbased dll! __ stdio common _ vfprintf c++. 58 to (unsigned locale _ _ stream, __ __ crt and format char * * __ _ iobuf and 64 options int locale, char * * pointers arglist)
>   ConsoleApplication5.exe!_vfprintf_l(_iobuf * const _Stream, const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) 줄 639 C++
    5, exe printf consoleapplication 954 to (const const format, ... * char) c++.

The exception is from the printf side. Run in debug mode for a more accurate cause.


2022-09-20 17:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.