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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:07:07+00:00 2026-05-24T23:07:07+00:00

I have the following class (I’ve trimmed irrelevant stuff): class Example { private: char*

  • 0

I have the following class (I’ve trimmed irrelevant stuff):

class Example
{
    private:
        char* name;
        int value[4];
        int numVals;
    public:
        Example();
        Example(char name[], int numVals, int, int, int, int);
        ~Example();
};

And here is the initialization constructor:

Example::Example(char na[], int vals, int v1, int v2, int v3, int v4)
{
    name = new char[strlen(na)+1];
    strcpy(name, na);
    numVals = vals;
    value[0] = v1;
    value[1] = v2;
    value[2] = v3;
    value[3] = v4;
    // cout << name; // this DOES print out the correct text

}

In my main(), I have an array of Example class, Example myArray[numRecs]. I then have a loop that uses the initialization constructor to fill the array:

myArray[i] = Example(name, numVals, v[0], v[1], v[2], v[3]);

Everything works as expected, however the name does not retain the characters I put into it. I checked using cout what the value is when it is passed into the constructor, and it was correct! However when I use my Example::Print(), it spits out a random character (the same character for each instance of Example).

Here is the Example::Print()

void Example::Print()
{
    int total, avg;
    total = avg = 0;

    cout << left << setw(20) << name << '\t';

    for(int i=0; i<4; i++){
        if(i<numVals){
            cout << left << setw(8) << value[i];
            total += value[i];
        } else
            cout << left << setw(8) << " ";
    }
    avg = total/numVals;
    cout << left << setw(8) << total <<
        left << setw(8) << avg << endl;
}

Any ideas? Thanks!

Oh and also, this is for an assignment. We have been told to keep the name field as a char pointer, not a string. I am confused as to what I should be using for the init constructor (char* name or char name[] or.. is there a difference?)

EDIT: Here is the destructor and default constructor. I do not have a copy constructor yet as the assignment does not call for it and it is not used. I’ll go ahead and make one for completeness anyway.

Example::~Example()
{
    delete [] name;
}

Example::Example()
{
    numVals = 0;
    for(int i=0; i<4; i++)
        value[i] = -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-24T23:07:09+00:00Added an answer on May 24, 2026 at 11:07 pm

    You should run your program through a memory debugger to witness the nightmare you’ve created!

    You are using manual memory management in your class, yet you forgot to obey the Rule of Three: You didn’t implement the copy constructor, assignment operator and destructor! Thus the temporary does allocate memory, copies the pointer (shallowly), and then probably invalidates the memory when it goes out of scope.

    The immediate “fix my code” answer is that you have to implement a proper assignment operator and copy constructor to make a deep copy of the char array.

    The “this is C++” answer is not to use pointers, new and arrays, and instead std::string.

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

Sidebar

Related Questions

I have following class: public class PairOfDice { private Dice d1,d2; public int Value
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
Lets say i have following class public class abc { int id; string name;
I have the following class: private class Info{ public String A; public int B;
I have following class ( entity ) @Entity public class Magazine { private int
I have the following class public class Car { public Name {get; set;} }
I have following class: template <size_t size> class Araye{ public: Araye(int input[]){ for (int
I have the following class A. public class A { public string Name {
Have the following: class Hal { public int zip { get; set; } public
I have the following class: public class Movie { string Name get; set; string

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.