Help with pointers!

Hi, I am altering a vector class to take in custom index ranges, like [-5, 5] and [10, 20]. My teacher wants us to have a "myZero" variable that points to where the 0th element is or would be. But if I do this

 
   customvector <int> example(1, 5);


where the parameters are the index boundaries, how can I have a pointer pointing to the 0th element if the space was not allocated? I am just conceptually lost on how to do this. Thanks.
You're probably supposed to have it point to the first element of the allocated array.
Hard to say without seeing the full task description.

It's possible to do arbitary pointer arithmetic, e.g.: int* myZero=new int[5]-1;
While this compiles, it's undefined behavior and therefore incorrect, so I hope that's not what your teacher was expecting.
@Athar

That is not what he wants us to do and its hard to explain what he wants us to do because I am lost myself. Here is a picture he gave us

http://i.imgur.com/IKvvP.png

he has myZero outside of the range of the vector.
Ah. Yes, you can reach that by pointer arithmetic: int* myZero=myList-myLower;
However, doing that is not safe - it's okay to have a pointer exactly one element past the end of an array (so to -4 in this case), but everything beyond that is not allowed.
But isn't he asking me to do what you are saying is unsafe? Haha
Yes. Your teacher is most likely not aware that this has undefined behavior.
I see something different here.

given:
1
2
3
4
5
6
7
const int myLower = -10, myUpper = -5;
const int size = 1 + myUpper - myLower;

int* myList = new int[size];
// so that the elements myList[0] through myList[myUpper - myLower] are valid

int* myZero = myList - myLower;

Now we have:
(myZero + myLower) == (myList + 0) is true.
and
(myZero + myUpper) = (myList + myUpper - myLower) = (myList + size - 1) = OK

I think the point is this:
Although myZero points to a location in memory which has not been allocated for our use, this memory location is still a valid point to calculate an offset from.

If we do not refer to elements outside the range of
myZero[myLower] through myZero[myUpper]

same as we would not refer to elements outside the range of
myList[0] through myList[size-1]

Then all will be well.
Ok, I see that myZero will be myList - myLower. How can I use myZero when overloading the [] operator? My teacher says we can't just calculate the myZero index to the myList index using an integer everytime we use the subscript operator and that we have to use myZero.

This is what he gave us

1
2
3
4
5
   Item & operator [] (int index)
   {
      checkIndex(index);
      return myList[index];
   }


This works for a regular vector. I want to return the correct index in myList by finding where it is in myZero.
Last edited on
Of course, why not:
1
2
3
4
5
Item & operator [] (int index)// pass index in range myLower <= index <= myUpper
   {
      checkIndex(index);// constrain index to the range myLower <= index <= myUpper
      return myZero[index];
   }

Where
1
2
int* myList = new int[size];
int* myZero = myList - myLower;

has already been declared, assigned, etc...

Maybe some test code would help.
Here I read and write using both myList and myZero:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
    const int myLower = -10, myUpper = -5;
    const int size = 1 + myUpper - myLower;// = 1 - 5 + 10 = 6

    int* myList = new int[size];// valid from myList[0] through myList[size-1]
    for(int i = 0; i < size; ++i) myList[i] = 0;// init these values

    int* myZero = myList - myLower;// valid from myZero[myLower] through myZero[myLower+size-1]
                                    // note: myLower + size -1 = myUpper

    // test it!
    myZero[-10] = 1;
    myZero[-9]  = 2;
    myZero[-8]  = 3;
    myZero[-7]  = 4;
    myZero[-6]  = 5;
    myZero[-5]  = 6;

    // prove these values were written to valid memory
    for(int i = 0; i < size; ++i)
        cout << myList[i] << " ";
    cout << endl;

    // write these values again using offsets from myZero
    for(int i = myLower; i <= myUpper; ++i)
        cout << myZero[i] << " ";
    cout << endl;

    return 0;
}

Output:

1 2 3 4 5 6
1 2 3 4 5 6
Last edited on
That code helped me understand more than my teacher...Thank you so much! I was just confused as to how myZero and myList compared to each other but I get it now. Thanks again
Topic archived. No new replies allowed.