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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:38:05+00:00 2026-05-27T06:38:05+00:00

UPDATE: Allocated memory for str1’s new data. Still memory error. I’m trying to rewrite

  • 0

UPDATE: Allocated memory for str1’s new data. Still memory error.

I’m trying to rewrite the += method for a string class I created.

Class mystring{

public:
    friend void operator+=(mystring& str1, const mystring& str2){
        mystring temp;

        delete[] temp.data;
        temp.length = str1.length + str2.length;
        temp.data = new char[temp.length + 1];

        strcpy(temp.data, str1.data);
        strcat(temp.data, str2.data);

        delete[] str1.data;
        str1.length = temp.length;

        strcpy(str1.data, temp.data);

    }

private:
    char *data;
    int length;

}

Then in the main class:

mystring str1("hi");
mystring str2("matt");

str1 += str2;
cout << str1 << endl;

This function is working as it should be, but I am getting memory errors all over when I run valgrind. I am unable to figure out why that is. If anyone could give me any tips that would be awesome.

Thanks

  • 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-27T06:38:06+00:00Added an answer on May 27, 2026 at 6:38 am

    Firstly, you meant not:

     strcat(str1.data, str1.data);
    

    but:

     strcat(str1.data, str2.data);
    

    Secondly, where do you expect str2.data to go? It is a memory scribble and hence the valgrind errors. Surprised it does not just crash.

    You need to reallocate enough storage for the combined length, copy over both original strings and free str1.data before re-assigning it to the new storage.

    Based on the updated post:

    friend void operator+=(mystring& str1, const mystring& str2)
        {
            // Not using a temp mystring here, as the temp never really maintains its state as a mystring
            // I am assuming length is the length of the string, not the storage. Not the best design if you consider resizing the the string to less than the storage
            int newStringLength = str1.length + str2.length;
            char* newStorage = new char[newStringLength +  1];
    
            strcpy(newStorage, str1.data);
            // strcat has to scan from the start of the string; we do not need to.
            strcpy(newStorage + str1.length, str2.data);
    
            delete[] str1.data;
    
            str1.length = newStringLength ;
            str1.data = newStorage;
    
             // Haven't though about the case where str2 is an alias for str1.
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to allocate memory using sun.misc.Unsafe.allocateMemory() and access it in native code.
is it possible to learn the allocated memory range of an OpenGL context? Supposedly
This question concerns memory in Android. My method: I have two activites, A and
We allocate memory in C using malloc and in C++ using new. I know
I am trying to find a way to allocate memory dynamically for memory mapped
I allocated some memory with anonymous mmap: buff->addr = mmap(NULL, length, PROT_NONE, MAP_PRIVATE |
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to

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.