Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di rambco
    Registrato dal
    Aug 2001
    Messaggi
    582

    [C++]Puntatori deallocazione

    è corretto scrivere cosi ?

    char *Buffer=new char[4];
    strcpy(Buffer,"abcde");
    ----.
    ----.
    Buffer= new char[5];
    strcpy(Buffer,"abcdee");
    ----.
    ----.
    delete Buffer;



    oppure devo mettere un (delete Buffer anche prima del second strcpy ?

  2. #2
    delete [] Buffer; // [] <-- perchè Buffer è un array


    credo (ma non sono sicuro)
    che con due new effettui due allocazioni distinte...
    la prima di 4 celle e la seconda (distinta) di 5 celle.
    Quindi (se la mia teoria è esatta) devi deallocare la variabile,
    poi successivamente allochi la nuova variabile.

    Io per rettificare la memoria allocata, utilizzo
    la funzione C, realloc()... avrei scritto nel seguente modo:

    char *s;
    s = new char[4];
    strcpy(s, "ABC");
    s = (char*) realloc(s, 5*sizeof(char));
    strcpy(s, "ABCD");
    delete [] s;
    ...Terrible warlords, good warlords, and an english song

  3. #3
    Utente di HTML.it L'avatar di rambco
    Registrato dal
    Aug 2001
    Messaggi
    582
    okey ho capito pero mi fa una cosa strana in questa funziona, gli puoi dare un'occhiata !

    codice:
    void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
          TCustomWinSocket *Socket)
    {
     std::string FTP_Command;
     Comando cmd[]={{"___"},{"220"},{"331"},{"230"}};
     char Command[3];
     int i;
     int FNC_N=0;
     char buf_retrived[4];
     int Size=ClientSocket1->Socket->ReceiveLength();
     char *Buffer = new char[Size];
     ClientSocket1->Socket->ReceiveBuf(Buffer,Size);
     Memo1->Lines->Add(Buffer);
    
     FTP_Command=Buffer;
     delete [] Buffer;
     FTP_Command=FTP_Command.substr(0,3);
     strcpy(buf_retrived,FTP_Command.c_str());
     for (i=0;i<sizeof(cmd);i++){
       if (!StrComp(cmd[i].CMD,buf_retrived)) {FNC_N=i;}
     }
    
          AnsiString Buf;
     switch(FNC_N){
       case 1:
         Buf="USER \r\n\r\n";
         Size=Buf.Length();
         Size++;
         Buffer= new char[Size];
         strcpy(Buffer,Buf.c_str());
         ClientSocket1->Socket->SendBuf(Buffer,Size-1);
       break;
       case 2:
         Buf="PASS \r\n\r\n";
         Size=Buf.Length();
         Buffer= new char[Size];
         strcpy(Buffer,Buf.c_str());
         ClientSocket1->Socket->SendBuf(Buffer,Size);
       break;
       case 3:
         Buf="PWD \r\n\r\n";
         Size=Buf.Length();
         Buffer= new char[Size];
         strcpy(Buffer,Buf.c_str());
         ClientSocket1->Socket->SendBuf(Buffer,Size);
       break;
     }
      delete [] Buffer;
    }

    Se lo lancio cosi

    Se lo lancio togliendo il primo delete




    come mai ?

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.