dyn. vergrößern von char**-Arrays



  • Hallo.

    Wenn ich mit malloc() arbeite gibt es zum vergrößern die Funktion realloc()

    char **ppMsg = (char**) malloc (sizeof(char*));
    
    	for (int i=0; i<10; i++)
    	{
    		ppMsg = (char**) realloc (ppMsg, (i+1)*sizeof(char*));
    
    		ppMsg[i] = (char*) malloc (20*sizeof(char));
    
    		sprintf(ppMsg[i], "Hallo Nr. %d", i+1);
    	}
    

    Das funktioniert auch einwandfrei.
    Wenn ich das nun mit dem new operator machen will, überschreibt er mir jedesmal den Speicher. Mach ich was falsch oder geht das nicht?

    char **ppMsg = new char*;
    
    	for (int i=0; i<10; i++)
    	{
    		ppMsg = new char* [i+1];
    
    		ppMsg[i] = new char [20];
    
    		sprintf(ppMsg[i], "Hallo Nr. %d", i+1);
    	}
    

    mfg
    Horst2



  • Morgen,

    char **ppMsg = new char*[10]; //speicher fuer erste dimension
    
        for (size_t i = 0; i < 10; ++i) {
            ppMsg[i] = new char[10]; //zweite dimension
            sprintf(ppMsg[i], "Hallo Nr. %d", i+1);
        }
    
        //entsprechend auch loeschen
        for(size_t i = 0; i < 10; ++i)
            delete[] ppMsg[i];
        delete[] ppMsg;
    

    mfg
    v R



  • Morgen.
    Das ist mir klar. Ich möchte aber jedesmal wenn eine neue Meldung kommt den Speicher vergrößern. Deswegen das new in der for-Schleife.



  • wie wärs mit nem vector<char*>
    oder nem vector<string> ?



  • gehts nicht irgendwie mit new?



  • Wer benutzt denn noch C-Arrays in C++? 😮 Schon mal was von std::vector gehört? Devil81 ist der einzige real C++-Coder in diesem Thread! 😃



  • Horst2 schrieb:

    gehts nicht irgendwie mit new?

    Das geht nur, wenn du per new neuen Speicher reservierst, den alten Speicher-
    inhalt in den neuen Speicherbereich kopierst und den alten Speicher loeschst.

    Ein realloc-Pendant gibt es in C++ nicht. Aber Devil hat ja bereits Alternativen
    gepostet.

    mfg
    v R



  • Alles klar. Vielen Dank



  • @vR: Macht realloc nicht prinzipiell das gleiche?



  • Cocaine schrieb:

    @vR: Macht realloc nicht prinzipiell das gleiche?

    Ja. Damit ich das nicht unnoetiger weise neuschreiben muss (Ja ich bin faul :D):

    Manpage schrieb:

    The realloc() function changes the size of the previously allocated mem-
    ory referenced by ptr to size bytes. The contents of the memory are
    unchanged up to the lesser of the new and old sizes. If the new size is
    larger, the value of the newly allocated portion of the memory is unde-
    fined. If the requested memory cannot be allocated, NULL is returned and
    the memory referenced by ptr is valid and unchanged. If memory can be
    allocated, the memory referenced by ptr is freed and a pointer to the
    newly allocated memory is returned. Note that this may be different from
    the value passed as ptr. If ptr is NULL, the realloc() function behaves
    identically to malloc() for the specified size.

    mfg
    v R


Anmelden zum Antworten