473,811 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array boundary access : is this valid?

A library which i use, has an array class
class LibArray {
public:
LibArray():_arr ay(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(pt r){}
float *_MyPtr
};
..
class const_iterator {
const_iterator( const float *ptr):_MyPtr(pt r){}
const float * _MyPtr;
};
iterator begin() {
return iterator(& _MyBase._array[0]);
}

iterator end() {
return iterator( &_MyBase._ar ray[_len]);
}

LibArray _MyBase;
};

Please consider the following code,

MyWrapper wrArray(10, 67.0f);
MyWrapper::iter ator 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 2554
kg******@gmail. com wrote:
A library which i use, has an array class
class LibArray {
public:
LibArray():_arr ay(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<flo at>
{
public:
LibArray(): std::vector<flo at>() {}

LibArray(int n, float val): std::vector<flo at>( 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(pt r){}
float *_MyPtr
};
.
class const_iterator {
const_iterator( const float *ptr):_MyPtr(pt r){}
const float * _MyPtr;
};
iterator begin() {
return iterator(& _MyBase._array[0]);
}

iterator end() {
return iterator( &_MyBase._ar ray[_len]);
}

LibArray _MyBase;
};

Please consider the following code,

MyWrapper wrArray(10, 67.0f);
MyWrapper::iter ator 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*******@mari an.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():_arr ay(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<flo at>
{
public:
LibArray(): std::vector<flo at>() {}

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

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

>MyWrapper wrArray(10, 67.0f);
MyWrapper::ite rator 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
1644
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 supported across all 3 compilers ? I want a template to take a parameter and make it it's friend. <code>
5
397
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
8306
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 Source=c:\temp;Extended properties=Text')...tstfile#txt" It worked great. As long as I was logged in as user sa. When I finished testing, I began to receive the error:
24
1801
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 void* though the reverse is true.But they said this is valid. I just can't agree with them with out a valid explaination.Can any C standard experts clarify me this?
6
1203
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
2850
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 of x is: %d\n", x);
5
1283
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
1201
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
1389
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 cbo_hdr_memid_Change() i = i + 1 ReDim Preserve Arr1(1 To i) Arr1(i) = Me.cbo_hdr_memid.Text
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.