Breakthrough
Breakthrough

Reputation: 2534

Creating an array of zero width and zero height?

I have an assignment from my programming class, which is very poorly worded... The following line really stumps me. We're creating a class called FloatArray, which contains an array (arr, which is just a pointer to a bunch of floats).

The default constructor FloatArray(); should create array of zero width and zero height.

I have no clue what my professor means by that... Does anyone else? We're required to provide another constructor, which indicates the height and width of the array. That's fine and dandy - just plug in the numbers, and allocate away... But what does he mean by "create array of zero width and zero height"?

Just so you know, the array is simply defined as:

float *arr;

Help me understand this madness! If it were up to me, I wouldn't even make a blank constructor for such a class...

Regardless - am I overlooking something here? Is this some kind of terminology I've never encountered before, or am I correct in stating that this is madness?

Should I just do the following in the default constuctor?

*arr = 0;

Because I can't do arr = new float[0], now can I :P (Or maybe I can - I just tried it (thanks Thomas!) and it compiles and runs... !?)

Does this mean that when I do arr = new float[0], arr points to some location (the beginning?) on the heap?

I know this question seems quite vague, but that's how the entire assignment is - very poorly worded. I just want to make sure it's not just me!

Upvotes: 2

Views: 464

Answers (5)

quamrana
quamrana

Reputation: 39404

You might want something like this:

#include <exception>

class FloatArray 
{
public:
    FloatArray():arr(new float[0]){}
    FloatArray(int width, int height)
    {
        arr=new float[height*width];
        mWidth=width;
        mHeight=height;
    }

    ~FloatArray(){ delete [] arr; }

    float operator()(int xindex, int yindex)
    {
        if (!isValid(xindex,yindex))
        {
            throw std::exception();  // Some suitable exception
        }
        return arr[yindex*mWidth+xindex];
    }

    bool isValid(int xindex, int yindex);

private:
    float* arr;
    int mWidth;
    int mHeight;
};

Points to note:
I've left isValid() without a definition as an exercise.
The dtor is fine with EDIT: delete [] arr; whichever ctor was called.
I've used operator() as the accessor.
You could throw something else apart from std::exception.
There are no setter methods - left as an exercise.
I've put in one deliberate error that I know of.

Upvotes: 0

larsmoa
larsmoa

Reputation: 12942

Doing *arr = 0 in the constructor would be very unwise. Since arr is uninitialized it might point to anything. Therefor *arr = 0 means "set some random memoryblock to 0 which probably will fail. On the other hand, doing arr = new float[0] is indeed a valid operation.

I would recommend using a "new float[0]" for 0-size arrays. This reduces some special handling of 0-size arrays, such as arrays explicitly created using new FloatArray(0) which should result in the exact same array as new FloatArray(). Note that a good alternative may be to have a default zero-value of the length argument in the "regular" constructor.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 993871

This exercise may be intended to highlight the difference between interface and representation. I don't know what the other methods in the FloatArray class are supposed to be, but I suspect there are methods to get and set elements in particular positions of the array.

With such a default constructor, the class should create an object that acts as if it has zero width and zero height. That is, if the caller then requests to set a particular element of the array, then the method should return whatever condition it normally would for "out of bounds". That is, by throwing an exception or an error return or whatever.

Internally, the class can represent this condition however it likes, even by allocating memory. Maybe it's sufficient to set arr = NULL, or maybe it has to point to non-null memory, but that's up to you as the implementor of the class. The point is the interface should act in a particular way as defined by the assignment question.

Upvotes: 6

Cristina
Cristina

Reputation: 2001

I think he means that the constructor should not initialize any memory space for the array.

Nevertheless ask your professor.

Upvotes: 4

jdelator
jdelator

Reputation: 4251

You should ask your professor. I doubt he will see this here. He might just mean to initialize it to null.

Upvotes: 1

Related Questions