473,388 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

array boundary access : is this valid?

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?

Oct 13 '07 #1
2 2530
kg******@gmail.com wrote:
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.
Oct 13 '07 #2
Gianni Mariani <gi*******@marian.wswrote in news:4710e2b7
@news.eftel.com.au:
kg******@gmail.com wrote:
>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 )

};
Or std::tr1::array<?

>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.
Oct 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is...
5
by: Michael | last post by:
Hi i recently came across some code that looked like this which I've never ever seen before! I appreicate it may be 'c' but is it valid in c++?? //main.c int main() { int a; int b;
0
by: Jim Andersen | last post by:
Hope this helps someone. SQL server 2000. I imported some text into a table. "insert into tbltst (field1, field2) SELECT field1, field2 FROM OpenDataSource('Microsoft.Jet.OLEDB.4.0','Data...
24
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to...
6
by: Abhi | last post by:
char *pc = "First" "Second"; In the compiler I checked it concates the string. I thought it would require usage of ## preprocessor directive. Thanks, -Abhishikt
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
5
by: awk | last post by:
Hi, is this valid C++ ? It compiles fine with msvc8 and intel 10, but not with gcc 4.1 and 4.2. template <int N> struct foo { int c; };
5
by: Stef Mientki | last post by:
hello, by accident I typed a double value test, and to my surprise it seems to work. Is this valid ? a = 2 b = 2 a == b == 2
1
by: p4willi | last post by:
Can I send a message in continuation with this one? I have a sub form with that creates an array in the following manner: Dim i As Integer Dim Arr1() As String Private Sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.