473,698 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector error, how to debug

I have an integer variable and I am testing it as follows

#define NULL_VAL -1
...
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
...
}

The output is
32767aborted

The index i=0 and this is being assigned to, so somewhere it is getting
overwritten.
x is defined as a vector over a template

Vec<sx;

struct s{
int id;
...
}

Is there a way I can trace back and see where this value is coming from
?

Oct 1 '06 #1
18 2561
im*****@hotmail .co.uk wrote:
I have an integer variable and I am testing it as follows

#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}

The output is
32767aborted

The index i=0 and this is being assigned to, so somewhere it is
getting overwritten.
x is defined as a vector over a template

Vec<sx;
This vector contains no elements. To add elements, use 'push_back'.

Read FAQ 5.8, while you're at it.
>
struct s{
int id;
..
}

Is there a way I can trace back and see where this value is coming
from ?
The error is on line 42 of your program.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 1 '06 #2
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
im*****@hotmail .co.uk wrote:
I have an integer variable and I am testing it as follows

#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}
Replace your code above with:

assert( i >= 0 && i < x.size() );
if ( x.at(i).id != NULL_VAL ) {
std::cout << x[i].id << std::endl;
}

What does it do then?

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 1 '06 #3
im*****@hotmail .co.uk wrote:
I have an integer variable and I am testing it as follows

#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}

The output is
32767aborted

The index i=0 and this is being assigned to, so somewhere it is getting
overwritten.
x is defined as a vector over a template

Vec<sx;

struct s{
int id;
..
}

Is there a way I can trace back and see where this value is coming from?
The C++ language has no built feature for this. You can use a debugger: you
can then step through the program an watch the variable i. However,
debuggers are platform specific and specifics about particular debuggers
are therefore off-topic in this group. You may find better help in a forum
dedicated to your compiler or OS.
Best

Kai-Uwe Bux
Oct 1 '06 #4
Sorry, i is 0 and earlier in my code I set this to id to 0, I know this
because I'm printing it out there, so the only logical explanation is
that it is getting corrupted somewhere else. OK, if the dubugger is
off topic, I'll look elsewhere, thanks anyway.

Oct 1 '06 #5
In article <11************ *********@m7g20 00cwm.googlegro ups.com>,
im*****@hotmail .co.uk wrote:
Sorry, i is 0 and earlier in my code I set this to id to 0, I know this
because I'm printing it out there, so the only logical explanation is
that it is getting corrupted somewhere else. OK, if the dubugger is
off topic, I'll look elsewhere, thanks anyway.
What does x.size() return?

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 2 '06 #6
I am getting confused I should have pasted the actuall code. The
problem is the large vvalue is causing the abort none of the sizes goes
that high, I am looking to run a debugger to find where the index is
corrupted to this high value.

Oct 2 '06 #7

im*****@hotmail .co.uk wrote:
I have an integer variable and I am testing it as follows

#define NULL_VAL -1
..
if (x[i].id != NULL_VAL)
{
std::cout << x[i].id << std::endl;
..
}

The output is
32767aborted

The index i=0 and this is being assigned to, so somewhere it is getting
overwritten.
obviously, its being overwritten because its an index (or counter if
you prefer). That what indexes do.
x is defined as a vector over a template
no, x is a templated vector of type s elements.
>
Vec<sx;

struct s{
int id;
..
}

Is there a way I can trace back and see where this value is coming from
?
Why not show the code that is generating the (probably expected) output
you see?
32767 looks like the max numeric_limits for a short or 16 bit integer
on whatever platform/compiler you are running. Here is a test for you:

#include <iostream>
#include <ostream>
#include <limits>

int main()
{
int nlimit = std::numeric_li mits< int >::max();
std::cout << nlimit << std::endl;
}

What do you get?

Oct 2 '06 #8

<im*****@hotmai l.co.ukwrote in message
news:11******** ************@m7 3g2000cwd.googl egroups.com...
>I am getting confused I should have pasted the actuall code. The
problem is the large vvalue is causing the abort none of the sizes goes
that high, I am looking to run a debugger to find where the index is
corrupted to this high value.
I'm pretty sure Victor has identified your problem:

When you create a vector, *it is empty*, it contains
no elements. So any indexed access (e.g. [0]) will
produce undefined behavior (you're asking for the
value of something which does not exist.) First
put some elements in your vector, then you can
examine their values. But make sure the index you
use is at least one less than the value returned
by the vector's 'size()' member function (indices
start at zero).

And yes, the actual code is the only way we can
know for sure what's wrong.

-Mike
>

Oct 2 '06 #9

"Mike Wahler" <mk******@mkwah ler.netwrote in message
news:RO******** *********@newsr ead4.news.pas.e arthlink.net...
>
<im*****@hotmai l.co.ukwrote in message
news:11******** ************@m7 3g2000cwd.googl egroups.com...
>>I am getting confused I should have pasted the actuall code. The
problem is the large vvalue is causing the abort none of the sizes goes
that high, I am looking to run a debugger to find where the index is
corrupted to this high value.

I'm pretty sure Victor has identified your problem:

When you create a vector,
as in your example,
*it is empty*, it contains
no elements.
I needed to qualify my previous statement
because there are other ways to create a vector
which does contain elements (via contructors
other than default as you used).

-Mike
Oct 2 '06 #10

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

Similar topics

15
9688
by: David Jones | last post by:
Hi I have a list of values stored in a vector e.g. std::vector<int> x; x = 1; x = 4; x = 2; x = 4;
7
10625
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
4
1571
by: whocares | last post by:
hi everyone. i'm currently experiencing a strange problem under vc++ 2005 express. i hope someone has a hint for me, i'm kind of lost atm. i'm using a vectors of pointers in my code. using the release build i can add and remove elements aslong as i stay below a certain vector size (13 in this case, no joke). as soon as the vector tries to add element 14 i get a runtime error.
3
2231
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using namespace std; ...... vector <Vector> vectorlist;
6
2743
by: Rahul K | last post by:
Hi I am working on Visual Studio on Windows. I have a function which return the list of all the IP Addresses of a machine: vector<char *getAllLocalIPAddress() { char localHostName; struct hostent *hp; int i=0,rc;
1
2349
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and from what I've read insert() accepts references for almost all of its overloaded versions. If so, what could one do so those element's memory space doesn't wipe out? I suppose one should use the heap. In rest I hope the code speaks for itself, it's...
6
11079
by: Andy | last post by:
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...
4
7089
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 Expression:vector subscript out of range.
10
2195
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
8673
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
8601
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,...
1
8892
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7716
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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
4365
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...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.