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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:16:12+00:00 2026-06-05T14:16:12+00:00

I have this class: class Base{ private: char *message; public: Base(string message`); ~Base(); };

  • 0

I have this class:

class Base{
     private:
        char *message;
     public:
        Base(string message`);
        ~Base();    
};

Edited: sorry, i forgot giving my constructor!
When i implement the destructor like below:

Base::Base(string message1){
     message = new char[message1.size() + 1]
     message[message1.size()] = '\0';
     memcpy(message, message1.c_str(), message.size());
}
Base::~Base(){
     delete message;  
}

sometime the system go wrong with stop working error, but if instead of delete message, i use message = NULL, everything will be alright! So, if I just declare message = NULL in my destructor, does my program get memory leak?

  • 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-05T14:16:14+00:00Added an answer on June 5, 2026 at 2:16 pm

    There’s absolutely no reason to use new here. The constructor takes a std::string and then manually copies its contents into a new dynamically allocated array of char. This is completely worthless. If instead, the constructor simply copied it into another std::string, the std::string copy constructor would do the same, but with lots of free benefits: exception-safety, no memory leaks, and proper copy semantics.

    class Base{
         private:
            string message;
         public:
            Base(string message);
            // maybe a virtual destructor is desirable if this is a polymorphic base class
            // virtual ~Base() {}
    };
    
    Base::Base(string message1) : message(message1) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class like this: public class BaseResponse { public string ErrorMessage
Suppose I have this code class Base{ public: int getVal(); private: int a, b;
I have a hierarchy that looks something like this: class Base { public: void
I have seen this question http://www.careercup.com/question?id=384062 class Base { public : virtual void method
I have a method in a base class class Base { private static string
I have this code in base class protected virtual bool HasAnyStuff<TObject>(TObject obj) where TObject:class
I have this relationship : class Organization < ActiveRecord::Base has_many :users end class User
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
I have a class that inherits from a base class (this contains a lot
I have this Project model: class Project < ActiveRecord::Base validates :status, :inclusion => {

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.