Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C++] Deallocazione variabile

    Ragazzi, in questo pezzo del mio codice ho deallocato l'array chBuf nel seguente modo utilizzando free(chBuf)
    codice:
    char tmp1[]="cd VocaleConsole2.0\ncd Exe\ncd Profiles\ncd ";
                char tmp2[]="\ncd test\nHCopy -A -D -T 1 -C wav_config -S codetrain.scp\njulian -input rawfile -filelist wavlst -smpFreq 48000  -C ../../../SrcScripts/create_model_scripts/julian.jconf  > juliusOutput\n./ProcessJuliusOutput.pl juliusOutput juliusProcessed\nHResults -I testref.mlf tiedlist juliusProcessed \n";
                length = sizeof(tmp1)+sizeof(tmp2)+sizeof(nome_cognome);
    
                char *chBuf=(char *) malloc(length);
       
                strcpy (chBuf,tmp1);
                strcat (chBuf,nome_cognome);
                strcat (chBuf, tmp2);
                bSuccess = WriteFile(Child_IN_Wr, chBuf,length, &dwWritten, NULL);
                free (chBuf);
    è corretto?!
    Un saluto
    Giovanni

  2. #2
    Utente di HTML.it
    Registrato dal
    Mar 2006
    Messaggi
    93
    Ciao,

    non vorrei dire cavolate ma la funzione sizeof ti restituisce la dimensione in byte della strnga... incluso un byte per il carattere terminatore ('\0') di ogni stringa...

    quindi se usi sizeof per ogni stringa in length per determinare quanta memoria allocare otteni una valore maggiore del necessario, forse sarebbe meglio usare strlen() per length e allocare length +1 con malloc

  3. #3
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381

    Re: [C++] Deallocazione variabile

    Originariamente inviato da Giovanni Pr88
    Ragazzi, in questo pezzo del mio codice ho deallocato l'array chBuf nel seguente modo utilizzando free(chBuf)
    Visto che programmi in C++ non è più comodo usare una std::string?

    codice:
    std::string tmp1="cd VocaleConsole2.0\ncd Exe\ncd Profiles\ncd ";
    std::string tmp2 = "\ncd test\nHCopy -A -D -T 1 -C wav_config -S codetrain.scp\njulian -input rawfile -filelist wavlst -smpFreq 48000  -C ../../../SrcScripts/create_model_scripts/julian.jconf  > juliusOutput\n./ProcessJuliusOutput.pl juliusOutput juliusProcessed\nHResults -I testref.mlf tiedlist juliusProcessed \n";
    
    tmp1+= std::string(nome_cognome) + tmp2;
    bSuccess = WriteFile(Child_IN_Wr, tmp1.c_str(),tmp1.size(), &dwWritten, NULL);
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  4. #4

    Re: Re: [C++] Deallocazione variabile

    Più chiaro di così:
    codice:
    #include <stdio.h>
    #include <string.h>
    
    int main (void){
    	const char aString1 [] = "123456"; 
    	const char * aString2 = "123456";
    	
    	printf ("sizeof (aString1) = %ld;\n", sizeof (aString1));
    	printf ("sizeof (aString2) = %ld;\n", sizeof (aString2));
    	printf ("strlen (aString1) = %ld;\n", strlen (aString1));
    	printf ("strlen (aString2) = %ld;\n", strlen (aString2));
    	return 0;
    }
    che sul mio mac a 64 bit produce:
    codice:
    $ gcc -Wall -ansi -pedantic -Wextra -Wconversion main.c 
    $ ./a.out 
    sizeof (aString1) = 7;
    sizeof (aString2) = 8;
    strlen (aString1) = 6;
    strlen (aString2) = 6;
    importante: sizeof lavora a tempo compilazione, strlen in esecuzione.

    ps. ok scusate l'ho fatto in C. In C++ poco cambia, e come ti suggerisce shodan usa le std::string che fai prima e meglio ;-)

  5. #5

    Re: [C++] Deallocazione variabile

    Originariamente inviato da Giovanni Pr88
    Ragazzi, in questo pezzo del mio codice ho deallocato l'array chBuf nel seguente modo utilizzando free(chBuf)
    codice:
    char tmp1[]="cd VocaleConsole2.0\ncd Exe\ncd Profiles\ncd ";
                char tmp2[]="\ncd test\nHCopy -A -D -T 1 -C wav_config -S codetrain.scp\njulian -input rawfile -filelist wavlst -smpFreq 48000  -C ../../../SrcScripts/create_model_scripts/julian.jconf  > juliusOutput\n./ProcessJuliusOutput.pl juliusOutput juliusProcessed\nHResults -I testref.mlf tiedlist juliusProcessed \n";
                length = sizeof(tmp1)+sizeof(tmp2)+sizeof(nome_cognome);
    
                char *chBuf=(char *) malloc(length);
       
                strcpy (chBuf,tmp1);
                strcat (chBuf,nome_cognome);
                strcat (chBuf, tmp2);
                bSuccess = WriteFile(Child_IN_Wr, chBuf,length, &dwWritten, NULL);
                free (chBuf);
    è corretto?!
    Un saluto
    Giovanni
    no non è corretto, perché nel caso la WriteFile lanci un'eccezione, chBuf non viene deallocata. Cmq in C++ è meglio usare new/delete, piuttosto che malloc/free.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.