Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: STL objects...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 24 Topic 45764 of 47035
Post > Topic >>

Re: STL objects and binary compatibility

by Triple-DES <DenPlettfrie@[EMAIL PROTECTED] > May 12, 2008 at 10:36 PM

On 13 Mai, 01:35, Paavo Helde <nob...@[EMAIL PROTECTED]
> wrote:
> Maybe I should have been more precise. The alarms are not "false" in the
> sense that the code has UB by the standard. On the other hand, the code
> appears to have defined meaning and guaranteed behavior by the same
> implementation which raises the alarms. Example 1:
>
> #include <vector>
> #include <iostream>
> #include <ostream>
>
> double summate(const double* from, const double* to) {
> =A0 =A0 =A0 =A0 double sum=3D0.0;
> =A0 =A0 =A0 =A0 for(const double* p=3Dfrom; p!=3Dto; ++p) {
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 sum +=3D *p;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 return sum;
>
> }
>
> int main() {
> =A0 =A0 =A0 =A0 std::vector<double> v;
> =A0 =A0 =A0 =A0 v.push_back(3.1415926);
> =A0 =A0 =A0 =A0 v.push_back(2.7182818);
> =A0 =A0 =A0 =A0 size_t n=3Dv.size();
> =A0 =A0 =A0 =A0 std::cout << summate(&v[0], &v[n]) << "\n";
>
> }
>
> In real life, summate() is some legacy function accepting double*
> pointers, which has to be interfaced with a new std::vector array used
> for better memory management. The expression &v[n] is formally UB. VC++
> on XP crashes the application with the message "test.exe has encountered
> a problem and needs to close. =A0We are sorry for the inconvenience.".
If
> _SECURE_SCL is defined to 0, the program runs nicely and produces the
> expected results.

I think the problem is not only formal in this case, because the
expression v[n], or v.operator[](n) dereferences an invalid pointer.
Okay, the program may happen to work, but the checked iterator alarm/
crash is justified by the fact that it exposes a potential problem
that could easily be avoided by using (&v[0] + n) instead of &v[n].

--
DP
 




 24 Posts in Topic:
STL objects and binary compatibility
osama178@[EMAIL PROTECTED  2008-05-05 20:40:22 
Re: STL objects and binary compatibility
Gianni Mariani <gi4nos  2008-05-06 05:56:23 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-06 02:52:52 
Re: STL objects and binary compatibility
osama178@[EMAIL PROTECTED  2008-05-06 10:29:14 
Re: STL objects and binary compatibility
Gianni Mariani <gi4nos  2008-05-06 19:10:54 
Re: STL objects and binary compatibility
Ian Collins <ian-news@  2008-05-07 07:34:57 
Re: STL objects and binary compatibility
Joe Greer <jgreer@[EMA  2008-05-07 15:12:14 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-07 02:08:24 
Re: STL objects and binary compatibility
Paavo Helde <nobody@[E  2008-05-08 16:08:06 
Re: STL objects and binary compatibility
osama178@[EMAIL PROTECTED  2008-05-07 14:23:58 
Re: STL objects and binary compatibility
Joe Greer <jgreer@[EMA  2008-05-08 17:02:15 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-09 05:20:59 
Re: STL objects and binary compatibility
Paavo Helde <nobody@[E  2008-05-12 01:15:39 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-12 02:57:18 
Re: STL objects and binary compatibility
Paavo Helde <nobody@[E  2008-05-12 18:35:10 
Re: STL objects and binary compatibility
Triple-DES <DenPlettfr  2008-05-12 22:36:30 
Re: STL objects and binary compatibility
Paavo Helde <nobody@[E  2008-05-13 03:10:55 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-13 03:20:37 
Re: STL objects and binary compatibility
Paavo Helde <nobody@[E  2008-05-13 12:27:43 
Re: STL objects and binary compatibility
"kwikius" <a  2008-05-13 20:19:35 
Re: STL objects and binary compatibility
ajalkane <ajalkane@[EM  2008-05-13 21:57:04 
Re: STL objects and binary compatibility
Ian Collins <ian-news@  2008-05-14 17:34:59 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-14 02:04:10 
Re: STL objects and binary compatibility
James Kanze <james.kan  2008-05-14 02:13:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:41:18 CDT 2008.