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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:51:00+00:00 2026-05-29T10:51:00+00:00

For my home work I need to write String object in C++. One of

  • 0

For my home work I need to write String object in C++.

One of the methods is

void concatenate(String *s)

But when I check my object with Valgrind, there is a memory leak in my code. Here is method:

// add s's str to this _str
void String::concatenate(String *s)
{
  char * conc;
  int conc_size, i, j;

  conc_size = _len + s->_len;

  conc = new char[conc_size];  // line 39

  for (i = 0; i < _len; i++)
    conc[i] = _str[i];

  for (j = 0; i < conc_size || j < s->_len; i++, j++)
    conc[i] = s->_str[j];

  _str = conc;   // i'm assuming the problem is here
  _len = conc_size;
}

Here is Valgrind message:

==4706== 3 bytes in 1 blocks are definitely lost in loss record 2 of 9
==4706==    at 0x100024679: malloc (vg_replace_malloc.c:266)
==4706==    by 0x10007BF04: operator new(unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==4706==    by 0x10007BF96: operator new[](unsigned long) (in /usr/lib/libstdc++.6.0.9.dylib)
==4706==    by 0x100000E7E: String::concatenate(String*) (String.cpp:39)
==4706==    by 0x100001479: main (main.cpp:27)

Works with delete [] _str; and changed constructors:

// the empty string.
String::String()
{
    _len = 1;

    _str = new char[_len];
    assert(_str);
    _str[0] = '\0';
}

// store s string in this
String::String(char *s)
{
    _len = 0;

    int i = 0;
    while(s[i] != '\0')
    {
        _len++;
        i++;
    }

    _str = new char[_len];
    assert(_str);

    for (i = 0; s[i] != '\0' && i < _len; i++)
        _str[i] = s[i];

}
  • 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-29T10:51:01+00:00Added an answer on May 29, 2026 at 10:51 am

    You’re not dealocating the previous contents of your member _str. I’m assuming that’s also a char*. So when you’re telling _str to point elsewhere, the memory it previously pointed to will be unacessible.

    delete[] _str;
    _str = conc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a recursive function that utilizes just two string methods, .empty?
In my home and work pc's and in one friend of mine's it runs
I need a regular expression to validate string with one or more of these
I need to write method that will chek String str on other string, and
Consider the following string if I need to localize it: You need to write
There is a homework I should do and I need help. I should write
I understand that I need to figure out my own homework, but seeing that
I'm developing an application on two different machines (home and work) and while the
I've got eclipse workspaces for my full time job and my home test work
While using Vim (at home and at work), I often find myself doing similar

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.