K
kgeorge2
A library which i use, has an array class
class LibArray {
public:
LibArray():_array(NULL),_len(0);
LibArray(int n, float val):_array( new float[n]),_len(n){}
/**
more complete definition omitted
*/
int _len;
float *_array;
};
Now, i need to wrap this class, so that it has the exact member
functions of an stl container class.
This will enable me to use the wrapped class in stl algorithms.
class MyWrapper {
//stl container like spec
.....
class iterator {
iterator ( float *ptr):_MyPtr(ptr){}
float *_MyPtr
};
..
class const_iterator {
const_iterator(const float *ptr):_MyPtr(ptr){}
const float * _MyPtr;
};
iterator begin() {
return iterator(& _MyBase._array[0]);
}
iterator end() {
return iterator( &_MyBase._array[_len]);
}
LibArray _MyBase;
};
Please consider the following code,
MyWrapper wrArray(10, 67.0f);
MyWrapper::iterator eit = wrArray.end();
eit now references an address that is out of the array boundary.
*eit now references a value in that out of boundary address.
Are these valid?
I compiles and runs in Visual Studio 2003.
Is accessing out of boundary array address portable?
-thanks in advance?
class LibArray {
public:
LibArray():_array(NULL),_len(0);
LibArray(int n, float val):_array( new float[n]),_len(n){}
/**
more complete definition omitted
*/
int _len;
float *_array;
};
Now, i need to wrap this class, so that it has the exact member
functions of an stl container class.
This will enable me to use the wrapped class in stl algorithms.
class MyWrapper {
//stl container like spec
.....
class iterator {
iterator ( float *ptr):_MyPtr(ptr){}
float *_MyPtr
};
..
class const_iterator {
const_iterator(const float *ptr):_MyPtr(ptr){}
const float * _MyPtr;
};
iterator begin() {
return iterator(& _MyBase._array[0]);
}
iterator end() {
return iterator( &_MyBase._array[_len]);
}
LibArray _MyBase;
};
Please consider the following code,
MyWrapper wrArray(10, 67.0f);
MyWrapper::iterator eit = wrArray.end();
eit now references an address that is out of the array boundary.
*eit now references a value in that out of boundary address.
Are these valid?
I compiles and runs in Visual Studio 2003.
Is accessing out of boundary array address portable?
-thanks in advance?