How to find users with common interests?
Help to debug a vector
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > C/C++ Programming > Help to debug a vector 5 March 2005 22:30:40

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Help to debug a vector

Coosa C. 5 March 2005 22:30:40
 Hi,



I face problems debugging a vector in the C++ Library;

I put a simple example here to illustrate my target:

This is a simple program containing a vector and an array:



#include <iostream>

#include <vector>



using namespace std;



int main (void)

{

int arr [] = {1,5,8,3,6,2};

int size = sizeof(arr) / sizeof(int);

vector <int> vec (arr, arr+size);

cout << endl;

system ("pause");

return 0;

}



Using Visuall C++ under Visual Studio.NET 2003 I try to debug the vector and array using the command window:
arr

0x0012feb4

[0]: 1

[1]: 5

[2]: 8

[3]: 3

[4]: 6

[5]: 2

As your see here, by typing „arr” the debugger shows me the values of my array index by index

I can write also:
arr[2]

8

It shows the correct value for this array at index 2

Now when I try to debug my vector I see the following:
vec

{first=??? last=???}

std::_Vector_val<in­t,std::allocator<int­> >: {_Alval={...} }

_Myfirst: 0x00321180

_Mylast: 0x00321198

_Myend: 0x00321198
vec[0]

CXX0058: Error: Overloaded Operator not found

*Hint: The Vector Class has the [] operator!!
vec.front()

CXX0039: Error: Symbol is ambiguous
vec.back()

CXX0039: Error: Symbol is ambiguous
vec.size()

6

However, in the local and auto window i can see:

Name Value Type

- vec {first=??? last=???} std::vector<int,std­::allocator<int> >

+ std::_Vector_val<in­t,std::allocator<int­> > {_Alval={...} } std::_Vector_val<in­t,std::allocator<int­> >

- _Myfirst 0x00321180 int *

1 int

- _Mylast 0x00321198 int *

-33686019 int

- _Myend 0x00321198 int *

-33686019 int

So the value at the first index of the vector is shown, namely 1 that is correct, but I can’t see any values for the other elements of this vector



Can any one help me better debug a vector?

*Hint: I tried also Borland C++ Builder 6 and GDB Insight (with the command line) and all show almost the same like in Visual C++



Thanks in advance


Add comment
Brett W. McCoy 5 March 2005 19:58:01 permanent link ]
 
Coosa C. wrote:> using namespace std;>
int main (void)>
{>
int arr [] = {1,5,8,3,6,2};>
int size = sizeof(arr) / sizeof(int);>
vector <int> vec (arr, arr+size);>
cout << endl;>
system ("pause");>
return 0;>
}

I don't see that you've added the data to your vector yet. The
constructor you use doesn't look like you are using it correctly. I ran
your code, and there was nothing in the vec object (which is why you are
getting an error about the [] operator).

I tried this instead:

vector<int> vec(size);

for(int i = 0; i < size; i++) {
vec[i] = arr[i];
}

And was able to debug:

(gdb) print vec
$1 = {<std::_Vector_base­<int, std::allocator<int>­ >> =
{<std::_Vector_allo­c_base<int, std::allocator<int>­, true>> = {_M_start =
0x8049360,
_M_finish = 0x8049378,
_M_end_of_storage = 0x8049378}­, <No data fields>}, <No data fields>}
(gdb) print vec[0]
$2 = (int &) @0x8049360: 1
(gdb) print vec[5]
$3 = (int &) @0x8049374: 2

-- Brett

PS. Don't use system("pause") if you are debugging a console program and
need to keep the window open, use getchar() instead, it's more portable
and doesn't create a second process.


Add comment
Bernd Stramm 5 March 2005 20:04:10 permanent link ]
 


--- In c-prog-hHKSG33Tihhb­jbujkaE4pw@public.gm­ane.org, "Brett W. McCoy" <bmccoy@c...> wrote:> Coosa C. wrote:> > using namespace std;> >
int main (void)> >
{> >
int arr [] = {1,5,8,3,6,2};> >
int size = sizeof(arr) / sizeof(int);> >
vector <int> vec (arr, arr+size);> >
cout << endl;> >
system ("pause");> >
return 0;> >
}>
I don't see that you've added the data to your vector yet. The > constructor you use doesn't look like you are using it correctly. I
ran > your code, and there was nothing in the vec object (which is why you
are > getting an error about the [] operator).>
I tried this instead:>
vector<int> vec(size);>
for(int i = 0; i < size; i++) {> vec[i] = arr[i];> }>
And was able to debug:>
(gdb) print vec> $1 = {<std::_Vector_base­<int, std::allocator<int>­ >> = > {<std::_Vector_allo­c_base<int, std::allocator<int>­, true>> =
{_M_start = > 0x8049360,> _M_finish = 0x8049378,> _M_end_of_storage = 0x8049378}­, <No data fields>}, <No data
fields>}> (gdb) print vec[0]> $2 = (int &) @0x8049360: 1> (gdb) print vec[5]> $3 = (int &) @0x8049374: 2>
-- Brett>
PS. Don't use system("pause") if you are debugging a console program
and > need to keep the window open, use getchar() instead, it's more portable > and doesn't create a second process.

No, I think his code works as intended. I ran it on linux, adding
some pedestrian debugging statements. I get the same result as
Brett, gdb doesn't like debugging STL sometimes.

for completeness (not that it answers the original question, sorry):


12:00:09 bernd (1020) tmp $ mroe acc.cc

#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
int arr[] = {1,2,3,4,5,6};

int size = sizeof(arr)/sizeof(­int);

cout << " size " << size << " sizeof(arr) " << sizeof(arr) << endl;

int i;
vector<int> vec(arr,arr+size);

cout << "vec.size() " << vec.size() << endl;
for (i=0;i<size;i++) {
cout << "v[" << i << "] = " << vec[i] << endl;
}
}
12:00:13 bernd (1021) tmp $ gcc -o acc -g acc.cc -lstdc++
12:01:25 bernd (1022) tmp $ ./acc
size 6 sizeof(arr) 24
vec.size() 6
v[0] = 1
v[1] = 2
v[2] = 3
v[3] = 4
v[4] = 5
v[5] = 6






Add comment
Brett W. McCoy 5 March 2005 20:35:14 permanent link ]
 
Bernd Stramm wrote:
No, I think his code works as intended. I ran it on linux, adding> some pedestrian debugging statements. I get the same result as> Brett, gdb doesn't like debugging STL sometimes.>
for completeness (not that it answers the original question, sorry):

Strange... adding each data element individually works in the debugger
and adding them all at once doesn't?

Anyway, I think Bernd's suggestion is the better one anyway -- do your
debugging via the program first (via print statements, if possible) and
save the debugging via a debugger as a last resort (for tracing complex
statements and code logic).

-- Brett


Add comment
Victor A. Wagner Jr. 5 March 2005 21:03:57 permanent link ]
 
At Saturday 2005-03-05 09:58, you wrote:
Coosa C. wrote:> > using namespace std;> >
int main (void)> >
{> >
int arr [] = {1,5,8,3,6,2};> >
int size = sizeof(arr) / sizeof(int);

btw, you're slightly better off writing sizeof(arr)/sizeof(­*arr)
vector <int> vec (arr, arr+size);> >
cout << endl;> >
system ("pause");> >
return 0;> >
}>
I don't see that you've added the data to your vector yet. The>constructor you use doesn't look like you are using it correctly.

it looks like a perfectly good range constructor to me, I have NO idea why
your compiler wouldn't like it
I ran>your code, and there was nothing in the vec object (which is why you are>getting an error about the [] operator).>
I tried this instead:>
vector<int> vec(size);>
for(int i = 0; i < size; i++) {> vec[i] = arr[i];> }>
And was able to debug:>
(gdb) print vec>$1 = {<std::_Vector_base­<int, std::allocator<int>­ >> =>{<std::_Vector_al­loc_base<int, std::allocator<int>­, true>> = {_M_start =>0x8049360,> _M_finish = 0x8049378,> _M_end_of_storage = 0x8049378}­, <No data fields>}, <No data fields>}>(gdb) print vec[0]>$2 = (int &) @0x8049360: 1>(gdb) print vec[5]>$3 = (int &) @0x8049374: 2>
-- Brett>
PS. Don't use system("pause") if you are debugging a console program and>need to keep the window open, use getchar() instead, it's more portable>and doesn't create a second process.>
To unsubscribe, send a blank message to ><mailto:c-prog-uns­ubscribe-hHKSG33Tihh­bjbujkaE4pw@public.g­mane.org>.>Yahoo! Groups Links>

Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"



Add comment
Coosa C. 5 March 2005 21:42:38 permanent link ]
 Actually the vector used the parameter constructor in the statement:
vector <int> vec (arr, arr+size);
It first declared a vector of integers then initialized it to the content of the array.

Ofcourse the program is empty since i wrote this simple application for debugging purposes, but now it's ok, i'll add simple lines to display some thing and show that it has elements inside it:

add after the statemnet: vector <int> vec (arr, arr+size); the following lies:

cout << "Displaying the content of the vector" << endl;
vector <int>::iterator i;
for (i = vec.begin(); i != vec.end(); ++i)
cout << *i << " ";

Coosa C. wrote:> using namespace std;>
int main (void)>
{>
int arr [] = {1,5,8,3,6,2};>
int size = sizeof(arr) / sizeof(int);>
vector <int> vec (arr, arr+size);>
cout << endl;>
system ("pause");>
return 0;>
}

I don't see that you've added the data to your vector yet. The
constructor you use doesn't look like you are using it correctly. I ran
your code, and there was nothing in the vec object (which is why you are
getting an error about the [] operator).

I tried this instead:

vector<int> vec(size);

for(int i = 0; i < size; i++) {
vec[i] = arr[i];
}

And was able to debug:

(gdb) print vec
$1 = {<std::_Vector_base­<int, std::allocator<int>­ >> =
{<std::_Vector_allo­c_base<int, std::allocator<int>­, true>> = {_M_start =
0x8049360,
_M_finish = 0x8049378,
_M_end_of_storage = 0x8049378}­, <No data fields>}, <No data fields>}
(gdb) print vec[0]
$2 = (int &) @0x8049360: 1
(gdb) print vec[5]
$3 = (int &) @0x8049374: 2

-- Brett

PS. Don't use system("pause") if you are debugging a console program and
need to keep the window open, use getchar() instead, it's more portable
and doesn't create a second process.
Add comment
Coosa C. 5 March 2005 22:12:09 permanent link ]
 I actually tried the GDB Insight, Visual C++ and Borland C++ Builder; All show only the first element of the vector!
But maybe there is a tick

Again, .. the [] operator is fully supported in the STL vector class since random access is supported. Thus, they can be accessed via the [] operator like a normal array or via iterators as a part of the associative containers in sTL such as lists and deques.

So:

A basic stament to display the content of the vector is:

for (unsigned int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;


OR

for (vector <int>::iterator i = vec.begin(); i != vec.end(); ++i)
cout << *i << " ";
cout << endl;

That's not my intention to go into the details of vectors
My real code is huge and exceeds 6000 lines of code. Inside it i implemented vectors from vectors of primitive datatypes till vectors of vectors and vectors of user-defined datatypes such as classes and structs. I get after all some semantic errors and it's troublesome to write my own debugging methods due to the xomplex invokation of those vectors in many methods. That's why i need a good debuggger and my first choice was Visual C++ since it generates (according to my experience) in compare to gcc extra warnings such as the simple statement above for (unsigned int i = 0; i < vec.size(); i++) by writing it as (int i = 0; i < vec.size(); i++) gcc doesn't provide any warnings that the vector size returns an unsigned int but visual c++ does.

Any way, even Visual C++ doesn't provide me with good debugging for vectors and i wonder if there exists any better debugger that i should switch too.
Please suggest some debuggers that may help me trace vectors.

Thanks
--- In c-prog-hHKSG33Tihhb­jbujkaE4pw@public.gm­ane.org, "Brett W. McCoy" <bmccoy@c...> wrote:> Coosa C. wrote:> > using namespace std;> >
int main (void)> >
{> >
int arr [] = {1,5,8,3,6,2};> >
int size = sizeof(arr) / sizeof(int);> >
vector <int> vec (arr, arr+size);> >
cout << endl;> >
system ("pause");> >
return 0;> >
}>
I don't see that you've added the data to your vector yet. The> constructor you use doesn't look like you are using it correctly. I
your code, and there was nothing in the vec object (which is why you
getting an error about the [] operator).>
I tried this instead:>
vector<int> vec(size);>
for(int i = 0; i < size; i++) {> vec[i] = arr[i];> }>
And was able to debug:>
(gdb) print vec> $1 = {<std::_Vector_base­<int, std::allocator<int>­ >> => {<std::_Vector_allo­c_base<int, std::allocator<int>­, true>> =
{_M_start => 0x8049360,> _M_finish = 0x8049378,> _M_end_of_storage = 0x8049378}­, <No data fields>}, <No data
fields>}> (gdb) print vec[0]> $2 = (int &) @0x8049360: 1> (gdb) print vec[5]> $3 = (int &) @0x8049374: 2>
-- Brett>
PS. Don't use system("pause") if you are debugging a console program
need to keep the window open, use getchar() instead, it's more portable> and doesn't create a second process.

No, I think his code works as intended. I ran it on linux, adding
some pedestrian debugging statements. I get the same result as
Brett, gdb doesn't like debugging STL sometimes.

for completeness (not that it answers the original question, sorry):


12:00:09 bernd (1020) tmp $ mroe acc.cc

#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
int arr[] = {1,2,3,4,5,6};

int size = sizeof(arr)/sizeof(­int);

cout << " size " << size << " sizeof(arr) " << sizeof(arr) << endl;

int i;
vector<int> vec(arr,arr+size);

cout << "vec.size() " << vec.size() << endl;
for (i=0;i<size;i++) {
cout << "v[" << i << "] = " << vec[i] << endl;
}
}
12:00:13 bernd (1021) tmp $ gcc -o acc -g acc.cc -lstdc++
12:01:25 bernd (1022) tmp $ ./acc
size 6 sizeof(arr) 24
vec.size() 6
v[0] = 1
v[1] = 2
v[2] = 3
v[3] = 4
v[4] = 5
v[5] = 6
Add comment
Coosa C. 5 March 2005 22:21:28 permanent link ]
 
Add comment
Brett W. McCoy 5 March 2005 22:30:40 permanent link ]
 
Coosa C. wrote:
You wrote:> I tried this instead:>
vector<int> vec(size);>
-------------------­-->
Here you just declared a vector and reserved its size to equal size.> so you just created a vector and made it's size = 6.> However, the content inside is not defined and the compiler will > generate some numbers inside or maybe u will get some run time errors > when u try to display the such a content

Never mind, I was looking at your original constructor incorrectly. It
was the gdb results that were confusing me.

-- Brett


Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > C/C++ Programming > Help to debug a vector 5 March 2005 22:30:40

see also:
Enable PHP
User directories
Multiple authentications requirements.
пройди тесты:
see also:
Combating SpamTerrorism
Rubber brains
[hello to all]

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .