STLPort question

Discussion in 'C++' started by A.L, Jul 24, 2003.

  1. A.L

    A.L Guest

    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?
     
    A.L, Jul 24, 2003
    #1
    1. Advertising

  2. A.L

    Dan Cernat Guest

    "A.L" <> wrote in message
    news:...
    > 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
     
    Dan Cernat, Jul 24, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    1
    Views:
    626
    Christoph Rabel
    Aug 13, 2003
  2. Icosahedron
    Replies:
    4
    Views:
    336
    tom_usenet
    Aug 18, 2003
  3. John Graat
    Replies:
    0
    Views:
    593
    John Graat
    Aug 26, 2004
  4. pervinder
    Replies:
    1
    Views:
    1,364
    David Lindauer
    Sep 24, 2004
  5. Replies:
    2
    Views:
    430
Loading...

Share This Page