-4

I am writing some code to implement a deep copy of an object.

Here is my code:

//---------------------------------------------------------------------------

#pragma hdrstop

#include <tchar.h>
#include <string>
#include <iostream>
#include <sstream>
#include <conio.h>

using namespace std;

//---------------------------------------------------------------------------

class Wheel
{
public:
    Wheel() : pressure(32)
    {
        ptrSize = new int(30);
    }
    Wheel(int s, int p) : pressure(p)
    {
        ptrSize = new int(s);
    }
    ~Wheel()
    {
        delete ptrSize;
    }
    void pump(int amount)
    {
        pressure += amount;
    }
    int getSize()
    {
        return *ptrSize;
    }
    int getPressure()
    {
        return pressure;
    }
private:
    int *ptrSize;
    int pressure;
};

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        *carWheels = new Wheel[4];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    RacingCar(RacingCar &oldObject)
    {
        for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
        {
            Wheel oldObjectWheel = oldObject.getWheel(i);
            carWheels[i]=new Wheel(oldObjectWheel.getSize(),oldObjectWheel.getPressure());
        }
    }
    void Accelerate()
    {
        speed = speed + 10;
    }
    Wheel getWheel(int id)
    {
        return *carWheels[id];
    }
    void printDetails()
    {
        cout << carWheels[0];
        cout << carWheels[1];
        cout << carWheels[2];
        cout << carWheels[3];
    }
private:
    int speed;
    Wheel *carWheels[4];
};

#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{

RacingCar testCar;
testCar.printDetails();

RacingCar newCar = testCar;
newCar.printDetails();

getch();
return 0;
}
//---------------------------------------------------------------------------

For some reason, my C++ builder crashes after compiling this code. Is there anything above that is not correct that would cause this to crash. There is no compile error, the program just crashes.

Darryl Janecek
  • 399
  • 4
  • 9
  • 25
  • 1
    Use a debugger, downvoted for lack of research effort until you do this. – djechlin Aug 29 '12 at 14:26
  • 1
    Why are you making an array of 4 pointers in the class, allocating 4 new Wheels for the first one in your constructor, and then iterating over the initial 4? Just use `std::array` if you always want four wheels. No need for dynamic allocation anywhere. – chris Aug 29 '12 at 14:28
  • You have no copy c-tor and assignment operator for Wheel class, that's incorrect in most cases, if you use pointers. Why not use std::vector, if you write on C++? – ForEveR Aug 29 '12 at 14:31
  • `ptrSize` looks like a prime candidate to be just a plain old `int` as well. – chris Aug 29 '12 at 14:33

2 Answers2

2

The problem is:

Wheel *carWheels[4];

and

*carWheels = new Wheel[4];

this only allocates 4 Wheels for carWheels[0]. Along with

return *carWheels[id];

If id is not 0, this will lead to undefined behavior because, as previously stated, only the first element is a valid pointer.

Besides this, the code is horrible. Avoid raw pointers. There are much better alternatives in C++. Use std::vector or std::array where you'd use a C-array, and smart pointers where you'd use raw ones.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Generally in my experience, if my compiler/tool crashes, I'm probably doing something so wrong that it never even occurred to the compiler writers to check for it.

The best way to track down such things is to comment out code until it works again, then slowly bring stuff back in until you find the offending part.

As a design note, I'd say that if it were me, I'd implement a copy constructor for Wheel as well, rather than having to write a complex deep copy constructor for classes like RacingCar that use it.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 1
    I'd say at least finding out where it is by using the debugger is much faster. The day I made the transition from one to the other was a good day. – chris Aug 29 '12 at 14:31
  • 1
    @chris - How's he supposed to do that, if the compiler is crashing? Debug the compiler? – T.E.D. Aug 29 '12 at 14:35
  • 1
    Oh, I didn't notice the part about the *compiler* crashing. I highly doubt that's what's actually meant, though. I suspect it's just the program crashing because of the wrong pointer usage. – chris Aug 29 '12 at 15:05
  • @chris - I don't think you read the last paragraph (after the code, so easy to miss) in the OQ. He makes it pretty clear that its his builder tool crashing, not his actual program. – T.E.D. Aug 29 '12 at 15:27
  • I read it, but it seems more likely that it's a case of miswording it than that crashing rather than the program being compiled. – chris Aug 29 '12 at 15:36
  • Perhaps. But what if he actually worded it just like he intended? It seems rather sad to me that a bunch of people would immediately assume he meant something completely different than what he said, then promptly downvote the guy into oblivion for not bothering to run a debugger (which he in fact could not run, and said so clearly right in his question). – T.E.D. Aug 29 '12 at 15:57
  • The claim is that the builder crashes *after* compiling. I doubt it crashes during linking, so they probably mean that they run the code from an IDE and then it falls over. – juanchopanza Aug 29 '12 at 16:08
  • @T.E.D., I agree downvoting off of an assumption is wrong. As for the assumption, juanchopanza explained my reasoning pretty well. – chris Aug 29 '12 at 16:15
  • @TED: 'I'd implement a copy constructor for Wheel'. So basically what I have for the RacingCar Class, but for the wheel. Can you provide me with a little help for this? – Darryl Janecek Aug 30 '12 at 04:30