Creating an array of zero width and zero height?

I have an assignment from my programming class which is very poorly formulated ... The next line brings me down a lot. We create a FloatArray class that contains an array ( arr

which is just a pointer to the group floats

).

Default constructor FloatArray (); should create an array of zero width and zero height.

I don't know what my professor means ... Anyone else? We have to provide another constructor that specifies the height and width of the array. That's fine and dandy - just insert numbers and highlight ... But what does he mean by "create an array of zero width and zero height"?

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

float *arr;

      

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

Regardless - have I forgotten something? Is this some kind of terminology that I've never come across before, or am I correct in stating that this is crazy?

Should I just be doing the following in a standard constuctor?

*arr = 0;

      

Because I can't do arr = new float[0]

, now I can: 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]

, it arr

points to some place (start?) In the heap?

I know this question seems rather vague, but like the whole assignment - very poorly formulated. I just want to make sure it's not just me!

+2


source to share


5 answers


Execution *arr = 0

in the constructor would be very unwise. Since it is arr

uninitialized, it can point to anything. So it *arr = 0

means "set some random block of memory to 0, which is likely to work. On the other hand, doing is arr = new float[0]

really a valid operation ."



I would recommend using "new float [0]" for size 0 arrays. This reduces some special handling of size 0 arrays, such as arrays explicitly created with new FloatArray(0)

, which should result in the same array as new FloatArray()

. Note that it might be a good alternative to have a default of zero for the length argument in the "regular" constructor.

+2


source


This exercise may be aimed at highlighting the difference between interface and presentation. I don't know what the other methods in the class should be FloatArray

, but I suspect there are methods to get and set elements at specific positions in the array.

With such a default constructor, the class must create an object that acts as if it has zero width and zero height. That is, if the caller asks for a specific element of the array, then the method should return whatever condition would normally be out of bounds. That is, throwing an exception or returning an error or whatever.



Internally, the class can represent this condition, but it likes it, even by allocating memory. Perhaps this is enough to set arr = NULL

, or perhaps it should point to non-zero memory, but that is up to you as the class designer. The point is that the interface should act in a specific way, as determined by asking the question.

+6


source


I think it means that the constructor doesn't have to initialize any memory space for the array.

However, ask your professor.

+4


source


You should ask your professor. I doubt he will see it here. It can just mean to initialize it to zero.

+1


source


You may need 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;
};

      

Note to Note:
I have left isValid()

undefined as an exercise.
Dtor is fine with EDIT: delete [] arr;

whichever ctor is called.
I used it operator()

as an accessor. You can throw something else besides std :: exception.
No setter methods - left as an exercise.
I have asked one deliberate error that I am aware of.

0


source







All Articles