Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:58:42+00:00 2026-06-11T12:58:42+00:00

So I wrote these two versions of the overloaded assignment operator for an ADT

  • 0

So I wrote these two versions of the overloaded assignment operator for an ADT I wrote in class. When I use them in conjunction with my overloaded ostream << operator for that same ADT I see different results. Why? Is it because I deallocated the memory to the other buffer that I am receiving this issue?

void Text::operator= (const Text &other) { 
if (this != &other) {
    delete [] buffer;
    bufferSize = other.bufferSize;
    buffer = new char[bufferSize + 1];
    strcpy(buffer, other.buffer);
    }
}

 void Text::operator= (const Text &other) { 
if (this != &other) {
    delete [] buffer;
    bufferSize = other.bufferSize;
    buffer = new char[bufferSize + 1];
    for (int i = 0; i < bufferSize; i++) {
         buffer[i] = other.buffer[i];
     }
}

Here is my overloaded ostream <<,

ostream & operator << (ostream &output, const Text &outputText) {
output << outputText.buffer;
return output;
}

The discrepancy occurs like this:

The first one outputs: Hey Jude

The second one outputs: Hey Jude(random garbage)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:58:43+00:00Added an answer on June 11, 2026 at 12:58 pm

    The second code snippet does not append a terminating null terminator, hence the garbage (strcpy() copies the terminating null). You need to explicitly add the null terminator after the for loop:

    buffer[bufferSize] = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the question: write a method that swaps two variables. These two variables
I wrote two methods to check there performance public class Test1 { private String
I realise that the two Java versions are substantially different but the library I
I have a function that I use on two different machine one a Mac
I am appending two files in my .htaccess file and write these lines: php_value
Let's say there are two python scripts that want to write data to the
Is there a better to write the following? I have two services that I
I wrote these lines in My Application start event: var mongo = new Mongo();
I wrote these lines in my code: CFUUIDRef identifier = CFUUIDCreate(NULL); NSString *identifierString =
In serialization mechanism,we are wrote the object into stream using objectinputstream and object outputstream.These

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.