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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:54:12+00:00 2026-05-22T01:54:12+00:00

I am unable to find out the bug in the below code which i

  • 0

I am unable to find out the bug in the below code which i had written[Not for any purpose though].


#include < iostream >
#include < cstdlib >

using namespace std;


class Base{
public:
        Base(){cout << "Base class constructor" << endl;}
        void funv() {};
        ~Base(){cout << "Base class destructor" << endl;} ;
};

class Derived:public Base{
public:
        char *ch;
        Derived():ch(new char[6]()){}
        ~Derived(){
                cout << "before" << endl;
                delete [] ch;
                ch = NULL;
                cout << "after" << endl;
        }
};

int main(){

        Derived * ptr = new Derived;

        //memcpy(ptr -> ch,"ar\0",4); // Works when class Derived is derved from base and also when not derived from base

        ptr -> ch = const_cast < char* >("ar0");  // Works only when class Derived is not derived from class Base

        cout << ptr -> ch[1] << endl;

        ptr -> funv();

        delete ptr;

        return 0;
}


I have commented on the suspected lines of the code.

I am using sun Studio 12.

  • 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-22T01:54:12+00:00Added an answer on May 22, 2026 at 1:54 am

    This is an Undefined behavior. It will cause problem in any case, whether you derive it or not.
    When you assign a const char* to a char* like below:

    ptr -> ch = const_cast < char* >("ar0");
    

    That means, you are assigning a character string which is defined in non-heap segment (mostly in data segment). One should only delete memory which was allocated on heap segment.

    Also, above assignment statement is executed, it will leak memory pointed by ch earlier. One way to avoid such problem is to declare the variable as,

    private: char* const ch;
    

    As soon as you try assigning something to ch, it will give compilation error. So that it will make you write wrapper to assign ch and there you can take care of deallocation.

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

Sidebar

Related Questions

I have been unable to find any decent documentation on this function. The code
I've been unable to find any info on this, which seems like it could
I have this code but I am unable to find out how it is
I am new to SQL, and could not find out how to make below
I have been unable to find any documentation on the .build method in Rails
I have been unable to find any documentation on properly closing database connections in
This is a simple question yet I was unable to find any information at
I'm trying to find a bug in a web application, which is causing Chrome
I am unable to find out more about this default parameter that I ran
Question is really simple, I am unable to find out what is wrong with

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.