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

  • Home
  • SEARCH
  • 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 6560539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:28:23+00:00 2026-05-25T13:28:23+00:00

This is a naive example I have coded to study C++ operator overloading. When

  • 0

This is a naive example I have coded to study C++ operator overloading. When I execute it, code hangs at the statement c = a + b; and control never reaches c.display();

As part of debugging if I put a cout << ptr << '\n'; in the assignment operator overloaded function it does print out HelloWorld, so string doesn’t seem to be malformed.

Why does it hang then? What am I missing ??

class mystring
{
    char *ptr;

    public:

   mystring(char *str = "")
   {
      ptr = new char[strlen(str) + 1];
      strcpy(ptr,str);

   } 

   mystring operator +(mystring s)
   {
      char *str = new char[strlen(ptr) + strlen(s.ptr) + 1];//where should this memory be freed
      strcpy(str,ptr);
      strcat(str,s.ptr);
      return mystring(str);
   }

   void operator =(mystring s)
   {
       strcpy(ptr,s.ptr);

      //cout << ptr << '\n'; \\Debug - this prints out HelloWorld but still hangs 
   }

   void display()
   {
       cout << ptr << '\n';
   }

   ~mystring()
   {
      delete [] ptr; 
   }
};

int main()
{
   mystring a="Hello",b="World",c;

  c = a + b;

  c.display();

  getchar();

}

EDIT: Compiler: MS-Visual C++ 2010 Express / Windows.

  • 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-05-25T13:28:24+00:00Added an answer on May 25, 2026 at 1:28 pm

    I think that what you are getting is a memory error. This line:

    c = a + b;
    

    does the following:

    c.constructor()
    c.operator=(a.operator+(b));
    

    and your operator= fails to allocate memory

    void operator =(mystring s)
    {
      // ptr is allocated enough memory for "", i.e. one byte
      strcpy(ptr,s.ptr); // copying more than one byte into one byte array
      //cout << ptr << '\n'; // this works, but you've trashed memory with the strcpy
    } // stack might be corrupted here, depends where this is, so execution can end up anywhere
    

    What you need is:

    void operator = (mystring &s) // reference!
    {
      delete [] ptr;
      ptr = new char [strlen (s.ptr + 1)];
      strcpy (ptr, s.ptr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this naive regex "<([\s]|[^<])+?>" (excluding the quotation marks). It seems so straightforward
This question might be naive as I'm new to ColdFusion programming. I have a
I am trying to do naive bayes classification in R. I have seen this
I have a data structure that represents C# code like this: class Namespace: string
You can perform hot deploy when you have native Java code on Jetty. This
Perhaps this is a naive question. In my understanding, ASP.NET MVC cannot work with
I realize this perhaps a naive question but still I cant figure out how
this may be a naive question, but, as asked in the object, what is
This seems like perhaps a naive question, but I got into a discussion with
I'm new to C# and .Net in general so this may be a naive

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.