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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:38:36+00:00 2026-06-07T15:38:36+00:00

The problem is in my assignment operator, I’ve forgotten to deallocate and reallocate memory

  • 0

The problem is in my assignment operator, I’ve forgotten to deallocate and reallocate memory for my pointer in my string class. I must have accidentally treated it like a copy constructor; This has been a great lesson in why memory management is very important. Thanks everyone for your help.

ive implemented my own string class and this seems to be the last function on the call stack before it breaks.

String::~String(){
    delete [] rep;
    len=0;
}

Can someone help me understand what the problem is?

Here is the function which calls it

template <class T> 
void SList<T>::RemoveAfter(typename SList<T>::Iterator i){        
    assert(i.nodePointer !=0 && i.nodePointer->next!=0);
    Node *save = i.nodePointer -> next;
    i.nodePointer->next = i.nodePointer->next->next;
    delete save;

}

If there is any more info you need to help me figure out why this occurs let me know.

By the way, if I use a type int, I dont have this problem, so I know the issue has to be with my string class…right?

More info as requested:

struct Node{ // Node: Stores the next Node and the data.
    T data;
    Node *next;
    Node() {next =0;}
    Node(const T& a, Node *p = 0){data=a;next=p;}

};

the error:

Windows has triggered a breakpoint in Algorithms.exe.
This may be due to a corruption of the heap, which indicates a bug in Algorithms.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Algorithms.exe has focus.
The output window may have more diagnostic information.

Example functionality that breaks:

    String item1("Example"), item2("Example");
    SList<String> list1;        
    list1.AddFirst(item2);
    list1.AddFirst(item1);
    list1.AddLast("List Class");
    list1.AddLast("Functionality");

    SList<String>::Iterator i1;

    i1 = list1.Begin();     
    i1++;
    i1++;
    list1.RemoveAfter(i1);

Example that works

    SList<int> list1;       
    list1.AddFirst(1);
    list1.AddFirst(2);
    list1.AddLast(3);
    list1.AddLast(4);

    SList<int>::Iterator i1;

    i1 = list1.Begin();     
    i1++;
    i1++;
    list1.RemoveAfter(i1);


    system("pause");

More information:

//Default Constructor
String::String(){
rep = new char[1];
rep[0] = '\0';
len = 0;
}  

//Constructor - Converts char* to String object
String::String(const char *s){
len=0;
const char *temp = s;
while(*temp){
    ++len;
    ++temp;
}//Sets len of rep to the length of s
rep = new char[len + 1];
for(int i=0; i<=len; ++i)
    rep[i]=s[i];
}

//Copy Constructor
String::String(const String &obj){
len=0;
char *temp = obj.rep;

while (*temp){
    ++len;
    ++temp;
}//Sets len of rep to length of obj.rep
rep = new char[len + 1];
for (int i = 0; i<=len; ++i)
    rep[i] = obj.rep[i];
}

//Assignment operator
const String& String::operator=(const String &rhs){
if (this != &rhs){
    len=0;
    char *temp = rhs.rep;
    while(*temp){
        ++len;
        ++temp;
    }//Sets len of this to length of rhs.rep
    for(int i = 0; i<=len;++i)
        rep[i]=rhs.rep[i];
}
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-07T15:38:39+00:00Added an answer on June 7, 2026 at 3:38 pm

    In your assignment operator you are potentially writing outside the bounds of the allocated area pointed to by char* rep, which will probably corrupt the stack (ie. cause undefined-behavior).

    When you later try to deallocate this segment of memory a corrupt stack might cause things to go banans, and it’s a very plausible this is the reason for the error message you are receiving when running your application.

    As stated by the message itself "this may be due to a corruption of the heap".


    How do I fix my problem?

    Inside your assignment operator you will need to do three things:

    1. deallocate the previous allocated memory in this (unless this->len and rhs.len have the same value, if so; skip step 2. as well)
    2. allocate the same amount of memory as rhs has for rhs.rep (ie. rhs.len bytes)
    3. update this->len to represent the length of the new content
    4. copy the bytes stored in the memory pointed to by rhs.rep to the memory segment pointed to by this->rep.

    Currently you are only doing step 3 & 4.

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

Sidebar

Related Questions

I have implemented a class string, similar to std::string one. I have a problem
I have a problem concerning nested templates and the overriding of the assignment operator.
I have a container class that is templatized. I am overloading the assignment operator
I have some problem understanding the compound assignment operator and the assignment operator in
In my assignment i have a problem with reading a file. See the following
I have assignment to work on producer and consumer problem by use thread and
This is a class assignment that must be done using a dynamically created array
Can someone justify the need of privatizing the assignment operator in a Singleton class
I am new to boost spirit and I have the following problem: #include <string>
I've been trying to figure out this problem. I have an assignment to make

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.