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 8814493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:08:18+00:00 2026-06-14T04:08:18+00:00

I have this operator overloader. My program crashes at the creation of the new

  • 0

I have this operator overloader.
My program crashes at the creation of the new wchar_t array.

myObject &operator += (const myObject &s) {
    wchar_t *cat = wcscat(data, s.data);
    int len = wcslen(cat);
    wchar_t *test = new wchar_t[len + 1]; //this is killing!
    wcscpy(test, cat);

    delete data;
    data = test;

    return *this;
}

Does anybody know what’s happening?

EDIT complete class definition

class myObject
{
    private:
        wchar_t *data;
    public:
        myObject() { data = 0; }
        ~myObject() { delete data; }

        myObject &operator += (const myObject &s) {
            wchar_t *cat = wcscat(data, s.data);
            int len = wcslen(cat);
            wchar_t *test = new wchar_t[len + 1];
            wcscpy(test, cat);

            delete data;
            data = test;

            return *this;
        }
};
  • 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-14T04:08:20+00:00Added an answer on June 14, 2026 at 4:08 am

    This code contains, at least, two rather obvious problems:

    1. You allocate data apparently using new wchar_t[n] but you release it using delete p rather than using delete[] p.
    2. The likely cause of your problem is that you concatenate two strings into the memory of one string and then allocate enough memory to copy the data over.

    You probably want something more along the lines of this:

    myObject &operator += (const myObject &s) {
        size_t len = wcslen(this->data) + wcslen(s.data);
        std::unique_ptr<wchar_t[]> tmp(new wchar_t[len + 1]);
        wcscpy(tmp.get(), this->data);
        wcscat(tmp.get(), s.data);
        delete[] this->data;
        this->data = tmp.release();
        return *this;
    }
    

    Actually, I think you want to use std::wstring: this class already provides the logic, probably in a more efficient form anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I am very new to operator overloading and I have to execute this
Hi I have a code like this, I think both the friend overloaded operator
I have overloaded the + operator like this class sample { private : int
I'm trying to overload the '--' postfix operator. I have this code: class Counter
I have seen this symbol/operator in a block of code: a+=1; But I cannot
I have this; import operator cuts = { Emerald : (10,125), Oval : (20,150),
I have this code: #include <iostream> #include <functional> struct A { int operator()(int i)
I'm facing a problem in Xcode with the operator keyword. I have this in
I have done this operation millions of times, just using the + operator! I
I have something like this: struct D { virtual void operator() {...}; } struct

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.