strcpy crashed ? o.O



  • Hi Leute,

    ich versuche folgendes und der code crashed einfach. Ich bin ratlos da ich es mir nicht erklären kann.
    Ich glaub dazu muss ich nicht viel sagen da der Code relativ eindeutig ist.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main() {
    
    	char *pStrDestinationDLC = NULL;
    	const char *strDLC = "AGP";
    	std::cout << strDLC << std::endl;
    	strcpy(pStrDestinationDLC, strDLC);
    
    	// your code goes here
    	return 0;
    }
    

  • Mod

    Ich bin ratlos da ich es mir nicht erklären kann.

    Falls das kein Getrolle sein soll: Es erzeugt undefiniertes Verhalten, strcpy mit einem Nullzeiger aufzurufen.

    Edit: Falls du auf ein Standardzitat bestehst:

    N1570 §7.1.4/1 schrieb:

    If an argument to a function has an invalid value (such as a [...] null pointer [...]) the behavior is undefined.

    Und hier ist die Referenz für strcpy, damit du auch weißt was es tut.



  • Warum benutzt du den alten C-Müll?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string StrDestinationDLC;
        const string trDLC = "AGP";
        std::cout << strDLC << std::endl;
        StrDestinationDLC = strDLC;
    }
    

Anmelden zum Antworten