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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:28:33+00:00 2026-05-24T03:28:33+00:00

The problem this: at the end of this function, the members of the element

  • 0

The problem this: at the end of this function, the members of the element at “tasks[taskCount]” like name, due date, etc, are indeed what was passed into this function, but after returning to this function’s caller, all those values become garbage, except taskcount, which is not dynamic. This function is defined within the scope of class, “Tasklist”

void addTask(char name[],char course[],char dueDate[]){
    taskCount++;
    Task task(taskCount, name, course, dueDate);
    tasks[taskCount] = task;
}

Here is the brief definition for class “Task”:

class Task
{
private:
    int number;
    char* name;
    char* dueDate;
    char* course;
public:
    Task(){
        name = new char[TEXT_SIZE + 1];
        dueDate = new char[TEXT_SIZE + 1];
        course = new char[TEXT_SIZE + 1];
        saveText = new char[(TEXT_SIZE * 3) + 1];
    };

    Task(int num, char n[], char c[], char d[]){
        number = num;
        name = new char[strlen(n) + 1];
        dueDate = new char[strlen(d) + 1];
        course = new char[strlen(c) + 1];

        strcpy(name, n);
        strcpy(dueDate, d);
        strcpy(course, c);
    };

    ~Task(){
        delete [] name;
        delete [] dueDate;
        delete [] course;
        delete [] saveText;
    }
};

I’m pretty sure what is happening is that this function is disposing of its locally declared variable “task” after returning to the caller, which invokes task’s destructor
thereby deallocating the memory that the “tasks” array was referencing for each of it’s element’s members (name, due, course).

So, how do I prevent this from happening?

So by the advice of the many helpful people on this site, I now have this in my Task class definition:

Task(const Task& t){
    name = new char[TEXT_SIZE + 1];
    dueDate = new char[TEXT_SIZE + 1];
    course = new char[TEXT_SIZE + 1];
    saveText = new char[(TEXT_SIZE * 3) + 1];

    number = t.number;
    strcpy(name, t.name);
    strcpy(dueDate, t.dueDate);
    strcpy(course, t.course);
    strcpy(saveText, t.saveText);
}

So this should account for one of the rule of three, right?

  • 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-24T03:28:34+00:00Added an answer on May 24, 2026 at 3:28 am

    You should use std::string instead of char * and let C++ handle allocation for you. No need to call operator new[], strcpy(), or operator delete[] when these operations have a better interface in the form of std::string.

    If you cannot use std::string, then you need to implement a copy constructor for Task that takes a const Task& as its only argument, and an assignment operator that does roughly the same thing. This constructor will then be implicitly used by your code when copying a Task object into an array or other place:

    Task::Task(const Task& t) {
        ...
    }
    
    Task& Task::operator =(const Task& t) {
        if (this != &t) {
            ...
        }
        return *this;
    }
    

    (These two members tend to have very similar implementations. Factoring out the common code is an exercise for the reader.)

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

Sidebar

Related Questions

Say I have some code like this function Chart(start, end, controller, method, chart) {
Strange problem happening where the end of these 2 blocks of code (this part:
This is a problem I come across every so often; I always end up
In the end, I have decided that this isn't a problem that I particularly
In the end this is gonna be a simple responsive slider. problems being the
Problem This question actually came up at work today. We are planning an experiment
Problem : This is a probably a very basic question, but how do I
I'm running in to the same problem this individual has . Specifically, I'm trying
I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern
i am a beginner and i have a problem : this code doesnt compile

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.