array boundary access : is this valid?

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?
 
G

Gianni Mariani

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

Considered this ?

class LibArray : public std::vector<float>
{
public:
LibArray(): std::vector<float>() {}

LibArray(int n, float val): std::vector<float>( n )

};

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?

Yes, as long as you never actually try to read or write the value being
pointed to.
I compiles and runs in Visual Studio 2003.
Is accessing out of boundary array address portable?

I can't see how it can't be. Many stl implementations do exactly what
you propose above.
 
A

Andre Kostur

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

Considered this ?

class LibArray : public std::vector<float>
{
public:
LibArray(): std::vector<float>() {}

LibArray(int n, float val): std::vector<float>( n )

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

Yes, as long as you never actually try to read or write the value being
pointed to.


Are you sure? I thought you were allowed to form the address of the
one-past-the end element, but you're not allowed to dereference it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top