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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:02:18+00:00 2026-05-27T16:02:18+00:00

I am attempting to write my own string class (so I can learn more)

  • 0

I am attempting to write my own string class (so I can learn more) and while doing so I noticed I ran into an issue with how the char data is being deleted, when the program closes the destructor of string will be called and there will be a heap error on deleting the data

#include <string.h>

template<typename T>
class String
{
protected:
    T* mData;

public:
    String(const T* data);
    ~String();
};

template<typename T>
String<T>::String(const T* data)
{
    if(data != NULL)
    {
        mData = new T[strlen(data)];
        strcpy(mData, data);
    }
}

template<typename T>
String<T>::~String()
{
    if(mData != NULL)
    {
        delete [] mData;
        mData = 0;
    }
}

int main(void)
{
    String<char> Test("Test");

    return(0);
}
  • 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-27T16:02:19+00:00Added an answer on May 27, 2026 at 4:02 pm
     mData = new T[strlen(data)];
            strcpy(mData, data);
    

    You are allocating one item too few, strcpy copies the nul terminating byte as well as the string

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

Sidebar

Related Questions

I'm attempting to write my own Log4Net Glimpse plugin (so I can interface with
I am attempting to write my own button object in JavaScript that can be
Currently I'm attempting to write my own wxObject, and I would like for the
I've been playing around in depth with attempting to write my own version of
I'm currently attempting to write my own program that mirrors the pmap command, specifically
For the past few days I have been attempting to write my own shell
I am attempting to write an android game involving a class which extends SurfaceView
I'm attempting to write my own WYSWG text editor in Javascript, but I'm struggling
I've been attempting to write a Lisp macro that would perfom the equivalent of
I am attempting to write an application that uses libCurl to post soap requests

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.