简体   繁体   English

Printf - 访问冲突读取位置 - C ++

[英]Printf - access violation reading location - C++

0xC0000005: Access violation reading location 0xcccccccc. 0xC0000005:访问冲突读取位置0xcccccccc。

printf is throwing this exception. printf抛出此异常。

I don't know why this is happening... There are values in those string variables. 我不知道为什么会发生这种情况......这些字符串变量中有值。 Am I using printf wrong? 我使用printf错了吗?

Help! 救命! (Please see the switch case) (请看开关盒)

string header;
string body;
string key;

if (!contactList.isEmpty()) {

    cout << "Enter contact's name: ";
    getline(cin, key);
    Contact * tempContact = contactList.get(key);
    if (tempContact != NULL) {
        string name = tempContact->getName();
        string number = tempContact->getNumber();
        string email = tempContact->getEmail();
        string address = tempContact->getAddress();

        //I've just put this here just to test if the variables are being initialized
        cout << name + " " + number + " " + email + " " + address << endl;

        switch (type) {
            case 1:
                printf("%-15s %-10s %-15s %-15s\n", "Name", "Number", "Email", "Address");
                printf("%-15s %-10s %-15s %-15s\n", name, number, email, address);
                break;
            case 2:
                printf("%-15s %-10s\n", "Name", "Number");
                printf("%-15s %-10s\n", name, number);
                break;
            case 3:
                printf("%-15s %-15s\n", "Name", "Email");
                printf("%-15s %-15s\n", name, email);
                break;
            case 4:
                printf("%-15s %-15s\n", "Name", "Address");
                printf("%-15s %-15s\n", name, address);
                break;
            default:
                printf("%-15s %-10s %-15s %-15s\n", "Name", "Number", "Email", "Address");
                printf("%-15s %-10s %-15s %-15s\n", name, number, email, address);
        }

    } else {
        cout << "\"" + key + "\" not found.\n" << endl;
        wait();
    }

} else {        
    cout << "Contact list is empty.\n" << endl;
    wait();
}

The first printf is printing fine but the second one will throw the exception, seemingly regardless of how I pass the string value in. 第一个printf打印正常,但第二个将抛出异常,看起来无论我如何传递字符串值。

printf's "%s" expects a char* as an argument, not a std::string . printf的“%s”期望char*作为参数,而不是std::string So printf will interpret your string objects as pointers and try to access the memory location given by the object's first sizeof(char*) bytes, which leads to an access violation because those bytes aren't really a pointer. 因此printf会将您的字符串对象解释为指针,并尝试访问该对象的第一个sizeof(char*)字节给出的内存位置,这会导致访问冲突,因为这些字节实际上不是指针。

Either use the strings' c_str method to get char* s or don't use printf. 要么使用字符串的c_str方法来获取char* s,要么不使用printf。

A C++ string isn't what printf expects for the %s specifier - it wants a null terminated character array. C ++ string不是printf%s说明符所期望%s - 它想要一个空终止的字符数组。

You need to either use iostream for the output ( cout << ... ) or convert the string to a character array, with c_str() for example. 您需要使用iostream作为输出( cout << ... )或将字符串转换为字符数组,例如c_str()

sepp2k给出了正确的答案,但我要补充一点:如果你打开完全警告(推荐),编译器会警告你:

a.cc:8: warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘int’

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM