simple problem with delete []

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael

    simple problem with delete []

    Hi All,

    I have the following:

    const int LENGTH = 5;

    void limitNameLength (string inText, char *&outText, int outLength){
    if(static_cast< int>(inText.len gth()) outLength){
    inText = inText.substr(0 , outLength);
    }
    strcpy(outText, inText.c_str()) ;
    if(static_cast< int>(strlen(out Text)) < outLength){
    for(int i = strlen(outText) ; i < outLength; i++){
    outText[i] = ' ';
    }
    }
    outText[outLength] = static_cast<cha r>(NULL);
    }

    int main(){
    char *temp;
    string name = "some long name";

    temp = new char[LENGTH];
    cout << "before : " << name << "\n";
    limitNameLength (name, temp, LENGTH);
    cout << "after : " << temp << "\n";

    delete [] temp;
    return 0;
    }

    if I comment out the call to limitNameLength the delete [] works ok. If I
    don't the delete [] never returns.....
    Can anyone tell me why? As far as I can see all I have done is pass the
    array to another function to manipulate it a bit then delete it. Why does
    delete not work?

    Thanks for your help

    Michael


  • kakaxyl2002@yahoo.com.cn

    #2
    Re: simple problem with delete []

    Hello:

    When I programed this one in MinGW stdio.It worked very well.

    And I didn't find any grammer error in yours.

    Comment

    • Ivan Vecerina

      #3
      Re: simple problem with delete []

      "michael" <spam@begone.ne twrote in message
      news:46455386$0 $9097$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...
      : Hi All,
      :
      : I have the following:
      :
      : const int LENGTH = 5;
      :
      : void limitNameLength (string inText, char *&outText, int outLength){
      You can have char* outText - no need to make it a reference
      since the function does not change the pointer address itself.
      : if(static_cast< int>(inText.len gth()) outLength){
      : inText = inText.substr(0 , outLength);
      : }
      : strcpy(outText, inText.c_str()) ;
      : if(static_cast< int>(strlen(out Text)) < outLength){
      : for(int i = strlen(outText) ; i < outLength; i++){
      : outText[i] = ' ';
      : }
      : }
      All of the previous can be simply written as:
      void limitNameLength ( string const& inText
      , char *outText, int const outLength)
      {
      strncpy( outText, inText.c_str(), outLength );
      //NB: if outLength<inTex t.size(), there will be no final '\0'

      : outText[outLength] = static_cast<cha r>(NULL);
      why not just: '\0' ?

      This effectively relies on outText having a length
      of outLength+1 !

      : }
      :
      : int main(){
      : char *temp;
      : string name = "some long name";
      :
      : temp = new char[LENGTH];
      : cout << "before : " << name << "\n";
      : limitNameLength (name, temp, LENGTH);
      : cout << "after : " << temp << "\n";
      :
      : delete [] temp;
      : return 0;
      : }
      :
      : if I comment out the call to limitNameLength the delete [] works ok.
      If I
      : don't the delete [] never returns.....
      : Can anyone tell me why? As far as I can see all I have done is pass
      the
      : array to another function to manipulate it a bit then delete it. Why
      does
      : delete not work?

      When you allocate an array of size LENGTH, the valid indices
      are 0 .. LENGTH-1. limitNameLength writes over outText[LENGTH].

      The buffer provided to limitNameLength needs to have 1 more character
      than the requested maximum length of the string.
      If outLength is to be the maximum buffer size, you could change
      the last line to:
      outText[outLength-1] = '\0';



      --
      http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
      Brainbench MVP for C++ <http://www.brainbench.com

      Comment

      • Paolo Maldini

        #4
        Re: simple problem with delete []

        i noticed one line in your code:
        >>strcpy(outTex t, inText.c_str()) ;
        the length of outText is 5, and also the length of inText is 5, overflow. no
        room to store '\0' in outText.
        so the calling sequence should be as below:
        int main() {
        temp = new char[LENGTH];
        ......
        limitNameLength (name, temp, LENGTH-1);
        ......
        delete temp;
        ......
        }


        "michael" <spam@begone.ne tдÈëÏûÏ¢ÐÂÎÅ:4 6455386$0$9097$ 5a62ac22@per-qv1-newsreader-01.iinet.net.au ...
        Hi All,
        >
        I have the following:
        >
        const int LENGTH = 5;
        >
        void limitNameLength (string inText, char *&outText, int outLength){
        if(static_cast< int>(inText.len gth()) outLength){
        inText = inText.substr(0 , outLength);
        }
        strcpy(outText, inText.c_str()) ;
        if(static_cast< int>(strlen(out Text)) < outLength){
        for(int i = strlen(outText) ; i < outLength; i++){
        outText[i] = ' ';
        }
        }
        outText[outLength] = static_cast<cha r>(NULL);
        }
        >
        int main(){
        char *temp;
        string name = "some long name";
        >
        temp = new char[LENGTH];
        cout << "before : " << name << "\n";
        limitNameLength (name, temp, LENGTH);
        cout << "after : " << temp << "\n";
        >
        delete [] temp;
        return 0;
        }
        >
        if I comment out the call to limitNameLength the delete [] works ok. If I
        don't the delete [] never returns.....
        Can anyone tell me why? As far as I can see all I have done is pass the
        array to another function to manipulate it a bit then delete it. Why does
        delete not work?
        >
        Thanks for your help
        >
        Michael
        >

        Comment

        • michael

          #5
          Re: simple problem with delete []


          "Ivan Vecerina" <_INVALID_use_w ebform_@ivan.ve cerina.comwrote in message
          news:a5055$4645 5eee$55da15e3$2 8099@news.hispe ed.ch...
          "michael" <spam@begone.ne twrote in message
          news:46455386$0 $9097$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...
          : Hi All,
          :
          : I have the following:
          :
          : const int LENGTH = 5;
          :
          : void limitNameLength (string inText, char *&outText, int outLength){
          You can have char* outText - no need to make it a reference
          since the function does not change the pointer address itself.
          : if(static_cast< int>(inText.len gth()) outLength){
          : inText = inText.substr(0 , outLength);
          : }
          : strcpy(outText, inText.c_str()) ;
          : if(static_cast< int>(strlen(out Text)) < outLength){
          : for(int i = strlen(outText) ; i < outLength; i++){
          : outText[i] = ' ';
          : }
          : }
          All of the previous can be simply written as:
          ummm... no it can't
          you will notice that I am padding the output string with spaces so it is
          always the same length. strncpy() will not do this for me.
          void limitNameLength ( string const& inText
          , char *outText, int const outLength)
          {
          strncpy( outText, inText.c_str(), outLength );
          //NB: if outLength<inTex t.size(), there will be no final '\0'
          >
          : outText[outLength] = static_cast<cha r>(NULL);
          why not just: '\0' ?
          >
          This effectively relies on outText having a length
          of outLength+1 !
          >
          : }
          :
          : int main(){
          : char *temp;
          : string name = "some long name";
          :
          : temp = new char[LENGTH];
          : cout << "before : " << name << "\n";
          : limitNameLength (name, temp, LENGTH);
          : cout << "after : " << temp << "\n";
          :
          : delete [] temp;
          : return 0;
          : }
          :
          : if I comment out the call to limitNameLength the delete [] works ok.
          If I
          : don't the delete [] never returns.....
          : Can anyone tell me why? As far as I can see all I have done is pass
          the
          : array to another function to manipulate it a bit then delete it. Why
          does
          : delete not work?
          >
          When you allocate an array of size LENGTH, the valid indices
          are 0 .. LENGTH-1. limitNameLength writes over outText[LENGTH].
          >
          yeah, thanks for that........cas e of looking but not seeing :-)
          The buffer provided to limitNameLength needs to have 1 more character
          than the requested maximum length of the string.
          If outLength is to be the maximum buffer size, you could change
          the last line to:
          outText[outLength-1] = '\0';
          >
          >
          >
          --
          http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
          Brainbench MVP for C++ <http://www.brainbench.com
          >

          Comment

          • Thomas J. Gritzan

            #6
            Re: simple problem with delete []

            Paolo Maldini wrote:
            i noticed one line in your code:
            >>strcpy(outTex t, inText.c_str()) ;
            the length of outText is 5, and also the length of inText is 5, overflow. no
            room to store '\0' in outText.
            so the calling sequence should be as below:
            int main() {
            char*
            temp = new char[LENGTH];
            ......
            limitNameLength (name, temp, LENGTH-1);
            ......
            delete temp;
            delete[] temp;
            ......
            }
            Also, please don't top-post. See my signature.

            --
            Thomas

            Comment

            Working...