tags:

views:

293

answers:

3

0xC0000005: Access violation reading location 0xcccccccc.

printf is throwing this exception.

I don't know why this is happening... There are values in those string variables. Am I using printf wrong?

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.

+4  A: 

A C++ string isn't what printf expects for the %s specifier - it wants a null terminated character array.

You need to either use iostream for the output (cout << ...) or convert the string to a character array, with c_str() for example.

calmh
+4  A: 

printf's "%s" expects a char* as an argument, not a 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.

Either use the strings' cstr method to get char*s or don't use printf.

sepp2k
I would put the emphasis on don't use printf exactly this kind of typesafety issues
Grizzly
+4  A: 

sepp2k gave the correct answer, but I'll add one minor point: if you turn on full warnings (which is recommended), the compiler will warn you:

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