Archived
This forum has been archived. Please start a new discussion on GitHub.
ByteSeq Problem
Can someone tell me what I'm doing wrong here. Here's the code. The GDB output is below too.
Ice::ByteSeq bytes(10,0);
int sz = 10;
cout << "Preparing ByteSeq of size " << sz << endl;
for (int i = 0; i < 10; i++) {
bytes.push_back(1);
cout << "byte-in:" << i << endl;
}
rfs->write(bytes, 0, bytes.size());
cout << "bytes created." << endl;
</code>
I don't think there are any values in the ByteSeq.
Breakpoint 1, main (argc=1, argv=0xbffff54c) at SDK.cpp:477
477 rfs->write(bytes, 0, bytes.size());
(gdb) print bytes
$1 = {
<_Vector_base<Ice::Byte,std::allocator<Ice::Byte> >> = {
_M_impl = {
<allocator<Ice::Byte>> = {
<new_allocator<Ice::Byte>> = {<No data fields>}, <No data fields>},
members of _Vector_impl:
_M_start = 0xf417420 "",
_M_finish = 0xf417434 "????",
_M_end_of_storage = 0xf417434 "????"
}
}, <No data fields>}
(gdb)
Thanks
David
Ice::ByteSeq bytes(10,0);
int sz = 10;
cout << "Preparing ByteSeq of size " << sz << endl;
for (int i = 0; i < 10; i++) {
bytes.push_back(1);
cout << "byte-in:" << i << endl;
}
rfs->write(bytes, 0, bytes.size());
cout << "bytes created." << endl;
</code>
I don't think there are any values in the ByteSeq.
Breakpoint 1, main (argc=1, argv=0xbffff54c) at SDK.cpp:477
477 rfs->write(bytes, 0, bytes.size());
(gdb) print bytes
$1 = {
<_Vector_base<Ice::Byte,std::allocator<Ice::Byte> >> = {
_M_impl = {
<allocator<Ice::Byte>> = {
<new_allocator<Ice::Byte>> = {<No data fields>}, <No data fields>},
members of _Vector_impl:
_M_start = 0xf417420 "",
_M_finish = 0xf417434 "????",
_M_end_of_storage = 0xf417434 "????"
}
}, <No data fields>}
(gdb)
Thanks
David
0
Comments
-
Ice::ByteSeq is a typedef for vector<Ice::Byte>, and Ice::Byte is a typedef for unsigned char.
Your constructor call creates a vector with 10 elements, each element set to zero:Ice::ByteSeq bytes(10, 0);
Then, in the for loop, you push another 10 elements onto the vector, each element set to one, so the vector now contains 20 elements, 10 zeros followed by 10 ones.
Your gdb output doesn't really show that the vector is empty. The members of _Vector_Base appear to have sensible values.
How about trying something like the following?Ice::ByteSeq bytes(10,0); for (int i = 0; i < 10; i++) { bytes.push_back(i); } for (int i = 0; i < 10; i++) { assert(bytes[i] == 0); } for (int i = 10; i < 20; i++) { assert(bytes[i] == i - 10); }
That will quickly establish whether the vector is working or not.
Cheers,
Michi.0 -
Why do you think that the sequence is empty? The code first creates a vector with 10 bytes, all initialized to 0. Then it adds 10 more elements with the value 1. For more information about how to use vectors, please consult one of the many STL books. You can also find information about STL vectors here:
http://www.sgi.com/tech/stl/Vector.html
Analyzing an STL vector with a debugger is not easy. To do so, you need to have knowledge about how your particular STL library implements vectors, and how your debugger shows you this information.0 -
Thanks, that makes more sense to me now
Michi:
thanks, I've been using Java for too long. If I used e.g.
ByteSeq bseq;
then the push_back(i) method would start from [0].
David0