STLPort question

A

A.L

Does STLPort have a problem with the standard string and assignment?
I am sure I am reading something incorrectly, but I don't know what:

Here is a summary of a few functions

_Self& append(_ForwardIter __first,
_ForwardIter __last,
forward_iterator_tag)
....
/* Get the length of the data we are assigning into the string */
const size_type __old_size = size();
difference_type __n = 0;
distance(__first, __last, __n);

/* If there is there is not enough room for it, then allocate and
copy. Otherwise just copy *.

if (__old_size + __n > capacity()) {
//Allocate and copy
}
else
{
//Copy
}

Now, all of that is fine. If the string is totally empty, then
(begin() and end() are both NULL) then we should allocate and copy.
The only problem is that the capacity function looks like this:

size_type capacity() const
{ return (_M_end_of_storage._M_data - _M_start) - 1; }

So, if the data is 0, and the start is 0, then we subract one and
return it. Unfortunately, the returntype is unsigned, so this becomes
a very large number. This causes the comparision against copy to
succeed when it should fail.

What am I missing?
 
D

Dan Cernat

A.L said:
Does STLPort have a problem with the standard string and assignment?
I am sure I am reading something incorrectly, but I don't know what:

Here is a summary of a few functions

_Self& append(_ForwardIter __first,
_ForwardIter __last,
forward_iterator_tag)
...
/* Get the length of the data we are assigning into the string */
const size_type __old_size = size();
difference_type __n = 0;
distance(__first, __last, __n);

/* If there is there is not enough room for it, then allocate and
copy. Otherwise just copy *.

if (__old_size + __n > capacity()) {
//Allocate and copy
}
else
{
//Copy
}

Now, all of that is fine. If the string is totally empty, then
(begin() and end() are both NULL) then we should allocate and copy.
The only problem is that the capacity function looks like this:

If the string is empty, then begin() == end(). They are not null.
size_type capacity() const
{ return (_M_end_of_storage._M_data - _M_start) - 1; }

So, if the data is 0, and the start is 0, then we subract one and
return it. Unfortunately, the returntype is unsigned, so this becomes
a very large number. This causes the comparision against copy to
succeed when it should fail.
Did you debug and saw that _M_end_of_storage._M_data and _M_start are 0? Or
you just assumed?
What am I missing?

How about, what if _M_end_of_storage._M_data is 1 past the end of string?
Actually capacity tells you the _capacity_ not the _length_ of the string so
I think that even an empty string has a capacity > 0.

Hope it helped.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top