Memory Management : character arrays and = operator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Memory Management : character arrays and = operator

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Memory Management : character arrays and = operator

    Memory Management : character arrays and = operator

    Q. In terms of Memory Management, What error would you have with the following code?

    Code:
    class String
    {
    public:
     String(const char right[]);
     String& operator= (const String& right);
     int length() const;
    private:
     char* buffer;
     int len;
    };
    
    int String::lenght() const {return len;}
    
    String::String(const char right[])
    {
      len = 0;
      while (right[len] != '\0')
        len++;
      buffer = new char[len+1];
      for (int i = 0; i < len; i++)
        buffer[i] = right[i];
      buffer[len] = '\0';
    }
    
    String& String::operator= (const String& right)
    {
      int n = right.length();
      for (int i = 0; i <= n; i++)
        buffer[i] = right.buffer[i];
      return *this;
    }
    Answer.
    I have no clue... Could you help me?
    The array size seems okay... new operator...
    is it because of the dangling pointer because there is no delete operator?

    Please let me know.
    Thanks,

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Memory Management : character arrays and = operator

    Your class performs manual memory management, but you neglected to define the destructor. You also forgot to define or disable the copy constructor, although you did define copy assignment operator.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Memory Management : character arrays and = operator

    Quote Originally Posted by weonthewave View Post
    Memory Management : character arrays and = operator

    Q. In terms of Memory Management, What error would you have with the following code?
    In addition to all of that was stated, what happens if new[] fails here?
    Code:
      buffer = new char[len+1];
    If new[] fails, you now have messed up your string contents (and is especially a problem if you coded an assignment operator that did this). This one error almost always gets overlooked by students, and even teachers. Now how do you fix it so that if new[] fails, your string is not corrupted, or that on construction, you don't give me a messed up string? That's your homework assignment from me to you.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 22nd, 2013 at 09:31 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Memory Management : character arrays and = operator

    Code:
    String& String::operator= (const String& right)
    {
      int n = right.length();
      for (int i = 0; i <= n; i++)
        buffer[i] = right.buffer[i];
      return *this;
    }
    And what if the destination buffer is smaller than the right.buffer?

    This assignment operator is wrong. You need to destroy the old string, size it properly, (or resize the existing string, or leave it alone if it is already large enough), and then copy the source string to the destination string.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 22nd, 2013 at 09:35 PM.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Memory Management : character arrays and = operator

    Quote Originally Posted by Paul McKenzie
    If new[] fails, you now have messed up your string contents.
    Actually, no: that is a constructor, so if new[] fails, the constructor fails. There will be no string contents to speak of.

    There does appear to be a potential buffer overflow problem with the copy assignment operator though.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Memory Management : character arrays and = operator

    Quote Originally Posted by laserlight View Post
    Actually, no: that is a constructor, so if new[] fails, the constructor fails. There will be no string contents to speak of.
    You're right, but you know that once the assignment operator is "corrected", you'll more than likely see the same error.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,382

    Re: Memory Management : character arrays and = operator

    This assignment operator is wrong. You need to destroy the old string, size it properly, (or resize the existing string, or leave it alone if it is already large enough), and then copy the source string to the destination string.
    You only destroy the old string and do the copy iff the destination of the copy is not the same as the source.

    Code:
    String st1;
    
       st1 = st1;
    needs to work!

    Look up in a good c++ book how to correctly code assignments.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,889

    Re: Memory Management : character arrays and = operator

    or you do what 90% of the apps out there do.
    if new/new[] fails, the exe aborts.
    :-p

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Memory Management : character arrays and = operator

    I think this thread basically lays out all of the mistakes that beginners make when creating a copy constructor and assignment op, especially for things like a string class.

    The problem is that there are many teachers who only cover some or even none of these errors, making the student believe they are writing good or acceptable class interfaces.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,382

    Re: Memory Management : character arrays and = operator

    The problem is that there are many teachers who only cover some or even none of these errors, making the student believe they are writing good or acceptable class interfaces.
    Unfortunately only too true! Some books don't even cover it correctly either.

    qui docet doctores?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,889

    Re: Memory Management : character arrays and = operator

    Quote Originally Posted by 2kaud View Post
    qui docet doctores?
    codeguru ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center