0

If I declare an array on the heap, how can I get information about the array?

Here is my code:

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;
};

If I have the following:

Wheel *carWheels[4];
*carWheels = new Wheel[4];
cout << carWheels[0].getPressure();

How can I get call the .getPressure() method on any instance in the array when it is on the heap? Also, if I want to create an array of Wheel on the heap, yet use this constructor when creating the array on the heap:

Wheel(int s, int p)

How do I do this?

2
  • 1
    Don't use raw arrays, they are not first class citizens in C++. Use std::array or std::vector instead. You will save yourself a lot of trouble. Commented Aug 27, 2012 at 8:08
  • 1
    Note that you need to follow the Rule of Three for your Wheel class. Commented Aug 27, 2012 at 8:12

3 Answers 3

2
Wheel *carWheels[4];

is an array of pointers to Wheel, so you need to initialize it with new:

for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
  carWheels[i]=new Wheel(); // or any other c-tor like Wheel(int s, int p)

later you can access it like that:

carWheels[0]->getPressure();

size of array can be retrieved like above:

sizeof(carWheels)/sizeof(carWheels[0])

[edit - some more details]

If you want to stick to array you will need to pass its size on function call because arrays decays to pointers then. You might want to stay with following syntax:

void func (Wheel* (arr&)[4]){}

which I hope is correct, because I never use it, but better switch to std::vector.

Also with bare pointers in arrays you must remember to delete them at some point, also arrays does not protect you against exceptions - if any will happen you will stay with memory leaks.

Sign up to request clarification or add additional context in comments.

3 Comments

When passed to a functio, carWheels loses size info, so your last expression won't work. And if it's no passed, there's no point to it, since you already know the size.
Why is the '->' used instead of a '.'?
@Darryl because carWheels[0] is a pointer to Wheel. Its the same as if you have Wheel *myWheel = new Wheel();, then you can say myWheel->getPressuire(); or (*myWheel).getPressure();. The -> just improves readability.
0

Simple, replace

Wheel *carWheels[4];

with

std::vector<Wheel*> carWheels(4);
for ( int i = 0 ; i < 4 ; i++ )
   carWheels[i] = new Wheel(4);

You seem to be confusing () and [], I suggest you look into that.

You do know that ptrSize = new int(30); doesn't create an array, right?

Comments

0

Like C, you will need to lug the array's element count around with your allocation.

This information is actually stored by the implementation in some cases, but not in a way which is accessible to you.

In C++, we favor types such as std::vector and std::array.


Other notes:

ptrSize = new int(30); << creates one int with a value of 30

How do I do this? Wheel(int s, int p)

Typically, you would just use assignment if you have an existing element:

wheelsArray[0] = Wheel(1, 2);

because you will face difficulty creating an array with a non-default constructor.

and while we're at it:

std::vector<Wheel> wheels(4, Wheel(1, 2));

is all that is needed to create 4 Wheels if you use vector -- no new required. no delete required. plus, vector knows its size.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.