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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:39:24+00:00 2026-05-30T00:39:24+00:00

I have implemented a class string, similar to std::string one. I have a problem

  • 0

I have implemented a class string, similar to std::string one.
I have a problem when the destructor is called: the field length has the length of the characters allocated in field.
This is the class:

class indexException:public std::exception
{
    public:
    virtual const char* what()
    {
        return "Index is either too long, or negative";
    }
};

class string
{
    public:
    static const unsigned int length_max=100;
    string(const char* field=NULL)
    {
        if(field!=NULL)
        {
            const unsigned int length=strlen(field);
            this->field=new char[length+1];
            this->length=length;
            for(unsigned int i=0;i<=length;i++)
                this->field[i]=field[i];
        }
        else
        {
            this->field=NULL;
            length=0;
        }
    }
    string(string& str)
    {
        string(str.field);
    }
    ~string()
    {
        if(length>0)
            delete field;
    }
    char& operator[] (int i) const throw()
    {
        try
        {
            if(i<0 || i>=(int)length)
                throw indexException();
        }
        catch(indexException& e)
        {
            std::cerr << e.what() << std::endl;
        }
        return field[i];
    }
    string& operator=(const char* field)
    {
        const unsigned int length=strlen(field);
        if(this->length>0)
            delete this->field;
        this->field=new char[length];
        this->length=length;
        for(unsigned int i=0;i<length;i++)
            this->field[i]=field[i];
        return *this;
    }
    string& operator= (const string& str)
    {
        if(this!=&str)
            *this=str.field;
        return *this;
    }
    operator char* ()
    {
        return field;
    }
    friend std::ostream& operator<< (std::ostream& out, string& str);
    friend std::istream& operator>> (std::istream& in, string& str);
    public:
    unsigned int length;
    char* field;
};

std::ostream& operator<<(std::ostream& out, string& str)
{
    out << str.field;
    return out;
}

std::istream& operator>> (std::istream& in, string& str)
{
    char temp[string::length_max];
    in >> temp;
    str=temp;
    return in;
}

If I use the assignment operator, this doesn’t cause a segmentation fault.
But it undirectly cause it.
I explain how:

int main(int argc,char** argv)
{
    string str="hi";
    string str2=str;
    return 0;
}

Putting a breakpoint into the assignment operator overloading, I realized that the assigment operator doesn’t cause segmentation fault.
The problem is after, when exiting from main.
If I remove the destructor I don’t get this segmentation fault, but I would know why I get this problem.

Edit: I have understood where’s the problem.
I followed your suggestions but it still goes to segmentation fault.
But now it doesn’t crash anymore on the destructor method, but on the assignment operator overloading:

    string& operator=(const char* field)
    {
        unsigned int length=0;
        if(field!=NULL)
            length=strlen(field);
        else
            field="";
        if(this->length>0)
            delete[] this->field;
        this->field=new char[length+1];
        this->length=length;
        strcpy(this->field,field);
        return *this;
    }

The problem is when I delete this->field, the debugger stops there.
An example of segmentation fault:

string str("hi");
string str2=str;

This causes segmentation fault.I suppone it’s because str2 is not initialized, and length has an undefined value.
If I instead do this:

string str("hi");
string str2;
str2=str;

There isn’t any segmentation fault.Why?
I thought that calling :

string str2;

Was also calling the constructor, or is that the “=” operator has the precedence?
How to solve this?

PS: I also changed other things,like the copy constructor.
Full code is here:
http://pastebin.com/ubRgaVr8

Solved: I changed the copy constructor as suggested in the accepted reply:

    string(const string& str)
    {
        length=str.length;
        field=new char[str.length+1];
        memcpy(field,str.field,length+1);
    }
  • 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-30T00:39:26+00:00Added an answer on May 30, 2026 at 12:39 am

    EDIT: Scrapped my previous answer, as it was incorrect.

    The problem appears to be the copy constructor, you are passing the field from the source instance as though it is merely another null terminated char*, but it isn’t.

    You don’t copy the null character at the end during the char* assignment invoked by the previous statement, you use an internal length field instead, and copy only that many bytes.

    so your copy constructor should be:

    string(string& str)
    {
       length = str.length;
       field = new char[length];
       memcpy(field, str.field, length);
    }
    

    or, if you want to preserve compatibility with null terminated functions, and you have ensured that the null is kept for all other assignments/constructors, etc:

    string(string& str)
    {
       length = str.length;
       field = new char[length + 1];
       memcpy(field, str.field, length + 1);
    }
    

    In fact, the mixing null terminated, and specified length strings so much throughout your class appears to be confusing you.

    I would create an internal, private, single disposal method, and an array of methods to set various source types, and have the constructors, assignment operators, and destructors use those instead.

    That way you only have a single places where any given operation occurs, rather than juggling many minor variations on the same functionality. For example:

    private:
        void internalSet(const char *source) {
            if (source == NULL) {
                length = 0;
                field = NULL;
            }else{
                length = strlen(source);
                field = new char[length];
                memcpy(field, source, length);
            }
        }
    
        void internalSet(const string &source) {
            length = source.length;
            if (length > 0) {
                field = new char[length];
                memcpy(field, source.field, length);
            }else{
                field = NULL;
            }
        }
    
        void internalDispose() {
            delete[] field;
        }
    
    public:
        string() : field(NULL), length(0) {}
    
        string(const string& source) { internalSet(source); }
        string(const char *source) { internalSet(source); }
    
        ~string() { internalDispose(); }
    
        string& operator=(const char *source) {
            internalDispose();
            internalSet(source);
            return *this;
        }
    
        string& operator=(const string &source) {
            internalDispose();
            internalSet(source);
            return *this;
        }
    
        void clear() {
            internalDispose();
            length = 0;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a sort of Repository class and it has has GetByID ,
I have the following tricky problem: I have implemented a (rather complicated) class which
We have a class hierarchy similar to this one: public class TestDereference { private
I have implemented VirtualPathProvider class so I can keep all my views in the
I have a .NET 3.5 web application for which I have implemented a class
I have implemented a LoginAccess class that prompts the user to enter their active
I have implemented a ToString() override method for my class in my Webservice and
I have implemented a Splash Screen according to WiredPrairie unmanaged c++ splasher class. But
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I have the following setup: Class A property x as string property y as

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.