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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:23:07+00:00 2026-06-04T07:23:07+00:00

I’m again doing a task for school and I’m implementing it slowly, I don’t

  • 0

I’m again doing a task for school and I’m implementing it slowly, I don’t know why my park_car function is not working, I just wanted to make a test and the program crashes … here is my code.

PS: I can’t change the ***p2parkboxes because it is given in the starter file like most other variables. I just want to see the first element of Floor 0 as : HH-AB 1234. Your help is most appreciated.
PS2: I can’t use the std::string as well it isn’t allowed for the task.

#include <iostream>
#include <cstring>
using namespace std;

#define EMPTY "----------"
class Parkbox{
    char *license_plate; // car's license plate
    public:
    Parkbox(char *s = EMPTY); // CTOR
    ~Parkbox(); // DTOR
    char *get_plate(){return license_plate;}
};
class ParkingGarage{
    Parkbox ***p2parkboxes;
    //int dimensions_of_parkhouse[3]; // better with rows,columns,floors
    int rows,columns,floors; // dimensions of park house
    int total_num_of_cars_currently_parked;
    int next_free_parking_position[3];
    // PRIVATE MEMBER FUNCTION
    void find_next_free_parking_position();
    public:
    ParkingGarage(int row, int col, int flr);// CTOR,[rows][columns][floors]
    ~ParkingGarage(); // DTOR
    bool park_car(char*); // park car with license plate
    bool fetch_car(char*); // fetch car with license plate
    void show(); // show content of garage floor
    // by floor
};

Parkbox::Parkbox(char *s ) { // CTOR
    license_plate = new char[strlen(s)+1];
    strcpy(license_plate, s);
    //cout << "ParkBox CTOR" << endl;
}
Parkbox::~Parkbox() { // DTOR
    delete [] license_plate;
    //cout << "ParkBox DTOR" << endl;
}

ParkingGarage::ParkingGarage(int row, int col, int flr){
    rows = row; columns = col; floors = flr;
    p2parkboxes = new Parkbox**[row];
    for (int i = 0; i < row; ++i) {
        p2parkboxes[i] = new Parkbox*[col];

        for (int j = 0; j < col; ++j)
            p2parkboxes[i][j] = new Parkbox[flr];
    }

}

ParkingGarage::~ParkingGarage(){

    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < columns; ++j)
            delete [] p2parkboxes[i][j];

        delete [] p2parkboxes[i];
    }
    delete [] p2parkboxes;
}

void ParkingGarage::show(){
    int i,j,k;
    for (i = 0 ; i < floors; i++){
        cout << "Floor" << i << endl;
        for (j=0;j<rows;j++){
            for (k=0;k<columns;k++){
                cout << p2parkboxes[j][k][i].get_plate() << "  ";
            }
            cout << endl;
        }
    }
}

bool ParkingGarage::park_car(char*s){

    p2parkboxes[0][0][0] = Parkbox(s); //test
    //p2parkboxes[0][0][0] = s; //test

    return true;
}


int main(void) {
    // a parking garage with 2 rows, 3 columns and 4 floors
    ParkingGarage pg1(2, 3, 4);
    pg1.park_car("HH-AB 1234");
    /*pg1.park_car("HH-CD 5678");
      pg1.park_car("HH-EF 1010");
      pg1.park_car("HH-GH 1235");
      pg1.park_car("HH-IJ 5676");
      pg1.park_car("HH-LM 1017");
      pg1.park_car("HH-MN 1111"); */
    pg1.show();
    /*pg1.fetch_car("HH-CD 5678");
      pg1.show();
      pg1.fetch_car("HH-IJ 5676");
      pg1.show();
      pg1.park_car("HH-SK 1087");
      pg1.show();
      pg1.park_car("SE-AB 1000");
      pg1.show();
      pg1.park_car("PI-XY 9999");
      pg1.show(); */
    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-06-04T07:23:08+00:00Added an answer on June 4, 2026 at 7:23 am

    You did not declare the copy constructor for the Parkbox class. So, the line

    p2parboxes[0][0][0] = Parkbox(s)
    

    creates something (instance of Parkbox with a char* pointer) on the stack (and deletes it almost immediately). To correct this you might define the

    Parkbox& operator = Parkbox(const Parkbox& other)
    {
        license_plate = new char[strlen(other.get_plate())+1];
        strcpy(license_plate, other.get_plate());
        return *this;
    }
    

    Let’s see the workflow for the

    p2parboxes[0][0][0] = Parkbox(s)
    

    line.

    1. First, the constructor is called and an instance of Parkbox is created on stack (we will call this tmp_Parkbox).
    2. Inside this constructor the license_plate is allocated and let’s say it points to 0xDEADBEEF location.
    3. The copying happens (this is obvious because this is the thing that is written in code) and the p2parboxes[0][0][0] now contains the exact copy of tmp_Parkbox.
    4. The scope for tmp_Parkbox now ends and the destructor for tmp_Parkbox is called, where the tmp_Parkbox.license_plate (0xDEADBEEF ptr) is deallocated.
    5. p2parboxes[0][0][0] still contains a “valid” instance of Parkbox and the p2parboxes[0][0][0].license_plate is still 0xDEADBEEF which leads to the undefined behaviour, if any allocation occurs before you call the

      cout << p2parboxes[0][0][0].license_plate;

    Bottom line: there is nothing wrong with the line itself, the problem is hidden within the implementation details of the ‘=’ operator.

    At this point it is really better for you to use the std::string for strings and not the razor-sharp, tricky and explicit C-style direct memory management mixed with the implicit C++ copy/construction semantics. The code would also be better if you use the std::vector for dynamic arrays.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.