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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:34:39+00:00 2026-06-03T07:34:39+00:00

I’m having trouble knowing what I’m doing wrong. Huge& Huge::operator *=(const Huge& h) {

  • 0

I’m having trouble knowing what I’m doing wrong.

Huge& Huge::operator *=(const Huge& h) {
    Huge retHuge = *this * h;
    string out1 = retHuge.toString(); // for debugging purposes
    return *this = retHuge;
//  return retHuge;
}

The

Huge retHuge = *this * h;

works fine as I verify in the string out1. If I return retHuge and print the result in the calling routine, I can see that the original this remains unchanged. If I do a *this = retHuge and return *this, I get a SEGSEGV fault.

What is the proper way to call the destructor to clean up memory and to return the result after the multiply?

I would like to thank all of you for the answers. Crazy Eddie used what I had and extended it, which is exactly what I wanted to do. I changed the source to reflect his suggestion and the SEGSEGV fault comes up.

I take this to mean that my syntax must be basically correct but that I have a bug hidden in the rest of my code. This is valuable information and I need to go back and very carefully look to see if I can find something. I don’t want to be lazy and ask for help before I do my homework properly, so I’ll have a good look first.

My experience is mainly with Java which has a nice garbage collector for unreferenced objects. I’ll go through with the debugger and verify that my destructor is being called to free the memory.

That was a good tip to try to do the *= operator first and then build the = operator from it. Clearly I did things in the opposite direction. Still your answers seem to indicate that what I did should indeed work. Since it doesn’t work, I’ll go back and see if I can find anything. If, after I have done my homework, I still can’t find anything, I’ll continue to ask. In the meantime, thanks for all the help.

  • 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-03T07:34:41+00:00Added an answer on June 3, 2026 at 7:34 am

    Do not delete this; afterwards, *this is writing into unallocated memory.

    Instead, just say *this = retHuge (without delete this), then return *this.

    Handle deallocation in operator = instead.

    Here’s a complete-ish example, with an imaginary complicated payload of an int *:

    #include <iostream>
    
    class A {
    public:
        A(int _val): val(new int(_val))
        { }
    
        ~A() {
            delete this->val;
        }
    
        A &operator =(const A &r) {
            // Pretend this->val is complex and we need to reallocate the
            // entire thing.
            delete this->val;
    
            this->val = new int(*r.val);
    
            return *this;
        }
    
        A operator *(const A &r) const {
            return A(*this->val * *r.val);
        }
    
        A &operator *=(const A &r) {
            return *this = *this * r;
        }
    
        int *val;
    };
    
    int main() {
        A x(5), y(10);
        x *= y;
    
        std::cout << "x: " << *x.val << ", y: " << *y.val << std::endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.