473,396 Members | 1,871 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,396 software developers and data experts.

vector subscript out of range

Hi all,

I started developing a little app on my Mac using XCode some month
ago. The app is running fine on my mac like a sharm. Now I am nearly
ready and yesterday I moved the whole source code to a Windows PC and
build it in MS VC++. Now I got a lot of "vector subscript out of
range" assertion errors and I don't know why because on my Mac it is
working without any error. What does the Mac with this problems and
what is the magic behind the errors that I get - because when I look
at the source I can't see any problems :(

mostly the problems happen at a place where I iterate through a vector
with vector.begin and vector.end - so I wonder why this error happens.

Thanks a lot for any tip or hint
Andy

May 11 '07 #1
6 11056
On May 11, 12:55 am, Andy <fru...@gmail.comwrote:
Hi all,

I started developing a little app on my Mac using XCode some month
ago. The app is running fine on my mac like a sharm. Now I am nearly
ready and yesterday I moved the whole source code to a Windows PC and
build it in MS VC++. Now I got a lot of "vector subscript out of
range" assertion errors and I don't know why because on my Mac it is
working without any error. What does the Mac with this problems and
what is the magic behind the errors that I get - because when I look
at the source I can't see any problems :(

mostly the problems happen at a place where I iterate through a vector
with vector.begin and vector.end - so I wonder why this error happens.

Thanks a lot for any tip or hint
Andy
And what would you have us do? guess?
Can't you simply write a simple test program that iterates through the
vector's elements and recreates the problem? And post it? Any chance
that you compiled the program on Windows in debug mode (assuming
assertions won't trigger in release mode on that MAC?) Did you know
that vector has the at() member function that throws an exception when
'subscript' is out of range in both debug and release mode? What
version of VC++ is involved?
Why is it that these questions need be asked at all?
May 11 '07 #2
perhaps I am not in that detail about my question - cuz I am new to c+
+ and also new to this group.

On the Mac the error does not appear - also when I compile it in
Release mode. Everything works fine

Yes, I compiled it on Windows in Debug Mode

I will take a look at the at() member

I also use sometimes things like that instead of an iterator
(for i=0; i < vector.size(); i++) { ... }
(for i=vector.size()-1; i>0 ;i--) { ... }

I am using MS Visual C++ Express Edition on Windows XP Pro


with vector.begin and vector.end - so I wonder why this error happens.
Thanks a lot for any tip or hint
Andy

And what would you have us do? guess?
Can't you simply write a simple test program that iterates through the
vector's elements and recreates the problem? And post it? Any chance
that you compiled the program on Windows in debug mode (assuming
assertions won't trigger in release mode on that MAC?) Did you know
that vector has the at() member function that throws an exception when
'subscript' is out of range in both debug and release mode? What
version of VC++ is involved?
Why is it that these questions need be asked at all?

May 11 '07 #3
On 11 Maj, 07:27, Andy <fru...@gmail.comwrote:
with vector.begin and vector.end - so I wonder why this error happens.
And what would you have us do? guess?
Can't you simply write a simple test program that iterates through the
vector's elements and recreates the problem? And post it? Any chance
that you compiled the program on Windows in debug mode (assuming
assertions won't trigger in release mode on that MAC?) Did you know
that vector has the at() member function that throws an exception when
'subscript' is out of range in both debug and release mode? What
version of VC++ is involved?
Why is it that these questions need be asked at all?

perhaps I am not in that detail about my question - cuz I am new to c+
+ and also new to this group.
Since you are new I'll forgive you this once, but in the future please
don't top-post, see http://www.parashift.com/c++-faq-lite/how-to-post.html
for more information.
On the Mac the error does not appear - also when I compile it in
Release mode. Everything works fine

Yes, I compiled it on Windows in Debug Mode

I will take a look at the at() member

I also use sometimes things like that instead of an iterator
(for i=0; i < vector.size(); i++) { ... }
(for i=vector.size()-1; i>0 ;i--) { ... }
The last one looks fishy, are you aware of the fact that it will not
process the first element in the vector? Use reverse_iterators
instead, then you don't have to worry about at what index to start and
end.
I am using MS Visual C++ Express Edition on Windows XP Pro
The standard library in VC++ 2005 comes with checked iterators, which
performs basic checks to see if iterators are out of range but also if
the [] operator is out of range, so if you get that kind of errors you
are probably doing something wrong and can count yourself lucky that
it has been working so far on the Mac. Try to use iterators as much as
possible and at() instead of [] and you should be able to find your
problem, another way might be to run the application in debug mode and
see where it crashes.

--
Erik Wikström

May 11 '07 #4
On May 11, 1:27 am, Andy <fru...@gmail.comwrote:

[rearranged inline]
>
And what would you have us do? guess?
Can't you simply write a simple test program that iterates through the
vector's elements and recreates the problem? And post it? Any chance
that you compiled the program on Windows in debug mode (assuming
assertions won't trigger in release mode on that MAC?) Did you know
that vector has the at() member function that throws an exception when
'subscript' is out of range in both debug and release mode? What
version of VC++ is involved?
Why is it that these questions need be asked at all?
perhaps I am not in that detail about my question - cuz I am new to c+
+ and also new to this group.

On the Mac the error does not appear - also when I compile it in
Release mode. Everything works fine

Yes, I compiled it on Windows in Debug Mode

I will take a look at the at() member

I also use sometimes things like that instead of an iterator
(for i=0; i < vector.size(); i++) { ... }
(for i=vector.size()-1; i>0 ;i--) { ... }
This is your problem.
(for i=vector.size()-1; i>0 ;i--) { ... }
That loop does not access vector[0] so you probably modified i in the
body somehow. If i is of type unsigned int and it is set to 0 in the
body, the statement i-- does *not* result in a negative number (there
are no negative numbers in an unsigned range). Scary, isn't it?
>
I am using MS Visual C++ Express Edition on Windows XP Pro
with vector.begin and vector.end - so I wonder why this error happens.
Thanks a lot for any tip or hint
Andy
Uncomment the obvious error to catch the cute exception. The
std::vector is not required to throw anything when its elements are
accessed using operator[], not unlike the primitive array. Thats why
at() is there for.

#include <iostream>
#include <vector>
#include <stdexcept>

int main()
{
std::vector< int v( 10 );
try
{
std::cout << "indexing up through vector:" << std::endl;
for ( size_t i = 0; i < v.size(); ++i )
{
std::cout << "v[" << i;
std::cout << "] = " << v.at( i );
std::cout << std::endl;
}
std::cout << "indexing down through vector:" << std::endl;
for ( size_t i = v.size(); i 0; --i )
{
std::cout << "v[" << i - 1;
std::cout << "] = " << v.at( i - 1 );
std::cout << std::endl;
}
// std::cout << v.at(v.size()); // throws range_error
}
catch ( const std::exception & r_e )
{
std::cout << "Error: " << r_e.what();
std::cout << std::endl;
}
}

/*
indexing up through vector:
v[0] = 0
v[1] = 0
v[2] = 0
v[3] = 0
v[4] = 0
v[5] = 0
v[6] = 0
v[7] = 0
v[8] = 0
v[9] = 0
indexing down through vector:
v[9] = 0
v[8] = 0
v[7] = 0
v[6] = 0
v[5] = 0
v[4] = 0
v[3] = 0
v[2] = 0
v[1] = 0
v[0] = 0
*/

moral of the story: use iterators instead.

May 11 '07 #5

thanks a lot I will take a deeper loop into
May 11 '07 #6
On May 11, 9:12 am, Salt_Peter <pj_h...@yahoo.comwrote:
On May 11, 1:27 am, Andy <fru...@gmail.comwrote:
[...]
On the Mac the error does not appear - also when I compile it in
Release mode. Everything works fine
How did you compile it on the Mac? I believe that Mac uses g++;
both g++ and VC++ will core dump on a bounds error, if compiled
with the usual debugging flags. (For g++, I use "-std=c++98
-ffor-scope -fno-gnu-keywords -foperator-names -pipe -Wall -W
-Wno-sign-compare -Wno-deprecated -Wno-non-virtual-dtor
-Wpointer-arith -Wno-unused -Wno-switch -ggdb3
-D_GLIBCXX_CONCEPT_CHECKS -D_GLIBCXX_DEBUG
-D_GLIBCXX_DEBUG_PEDANTIC -static-libgcc". And bounds errors
definitly do cause a core dump.)
Yes, I compiled it on Windows in Debug Mode
Which means? At least "/vmg /GR /EHs /D_CRT_SECURE_NO_DEPRECATE
/MTd /GS /Ge /w /D_DEBUG" are probably necessary (but some one
who is more familiar with VC++ should feel free to correct me
here).

[...]
The
std::vector is not required to throw anything when its elements are
accessed using operator[], not unlike the primitive array. Thats why
at() is there for.
You don't want an exception in case of an error; you want a core
dump. Which is what operator[] gives you, with both g++ and
VC++, at least if you compile with the right options. After
that, I'm not too sure how you procede under Windows, but under
Unix (and presumably, Mac), you can use gdb to immediately get a
stack walkback from the core; that should indicate exactly where
you've slipped up.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 11 '07 #7

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

Similar topics

20
by: andy.rich | last post by:
I am getting the following error and I do not know why. Can anyone help? -------------------------------------------------------- this is what appears on the screen...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: VB Programmer | last post by:
I'm acutally using VB6, not VB.NET, but I couldn't find the newsgroup for version 6.... I need help for something that should be simple. I keep getting a "subscript out of range" error and...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
2
by: flavourofbru | last post by:
Hi all, I am declaring 2 vectors as vector<int> abc; vector<int> xyz; i am passing the vector "xyz" as an argument to a function and the return value is stored in vector "abc". But it shows a...
4
by: Han | last post by:
when I exe my project in vs.net2005,I got the error following: Debug Assertion Failed! Program:........ File:c:\program files\microsoft visual studio 8\vc\include\vector Line:756 ...
6
by: brandon01 | last post by:
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx... Private Sub getBday() bDay = List1.List(nextBday) bDayArr = Split(bDay, " - ") Text1.Text =...
5
by: yogi_bear_79 | last post by:
Distant learning student. My lab is to write a function to perform the addition of large integers, with no limit to the number of digits. (Also have to do a subtraction, division, and...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.