what's the difference between "capacity" and "size" functions ofstring class?

Discussion in 'C++' started by Luna Moon, Oct 23, 2008.

  1. Luna Moon

    Luna Moon Guest

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    string cc(31, 'c');

    string bb=cc.assign(3, 'dd');

    cout << bb.capacity() << endl;

    cout << bb.size() << endl;

    getchar();

    }



    And "bb.capacity()" returns "15" on my computer, which is wierd, where
    did it come from?
     
    Luna Moon, Oct 23, 2008
    #1
    1. Advertising

  2. Luna Moon

    Ian Collins Guest

    Re: what's the difference between "capacity" and "size" functionsof string class?

    Luna Moon wrote:
    > #include "stdafx.h"


    Why?

    > #include <iostream>
    > #include <string>
    > using namespace std;
    > int main()
    > {
    > string cc(31, 'c');
    >
    > string bb=cc.assign(3, 'dd');
    >

    'dd' isn't a character constant.

    > cout << bb.capacity() << endl;
    >
    > cout << bb.size() << endl;
    >
    > getchar();
    >
    > }
    >
    >
    >
    > And "bb.capacity()" returns "15" on my computer, which is wierd, where
    > did it come from?
    >

    Capacity is the size of the string's buffer, size is the number of
    characters in the buffer.

    15 does appear to be incorrect.

    --
    Ian Collins
     
    Ian Collins, Oct 23, 2008
    #2
    1. Advertising

  3. Luna Moon

    Barry Guest

    On Oct 23, 8:30 am, Luna Moon <> wrote:
    > #include "stdafx.h"
    > #include <iostream>
    > #include <string>
    > using namespace std;
    > int main()
    > {
    >   string cc(31, 'c');
    >
    >   string bb=cc.assign(3, 'dd');
    >
    >   cout << bb.capacity() << endl;
    >
    >   cout << bb.size() << endl;
    >
    >   getchar();
    >
    > }
    >
    > And "bb.capacity()" returns "15" on my computer, which is wierd, where
    > did it come from?


    string::capacity >= string::size(), while the latter one indicates
    the
    data size. (capacity - size) indicates that you can push_back such
    amount
    of charT without allocating the new space.

    you can call "string::reserve" to affect the "capacity"
    while you can call "string::resize" to affect the "size"

    --
    Best Regards
    Barry
     
    Barry, Oct 23, 2008
    #3
  4. Luna Moon

    James Kanze Guest

    On Oct 23, 2:47 am, Ian Collins <> wrote:
    > Luna Moon wrote:
    > > #include "stdafx.h"


    > Why?


    > > #include <iostream>
    > > #include <string>
    > > using namespace std;
    > > int main()
    > > {
    > > string cc(31, 'c');


    > > string bb=cc.assign(3, 'dd');


    > 'dd' isn't a character constant.


    Yes it is:). "A character literal is one or more characters
    enclosed in single quotes[...]" (first sentence of section
    2.1.3.2). But it probably won't do what he wants: "An ordinary
    character literal that contains more than one c-char is a
    multicharacter literal. A multicharacter literal has type int
    and implementation-defined value." (A c-char is "any member of
    the source character set except the single-quote ', backslash \,
    or new-line character", an escape sequence or a universal
    character name. Which, if I'm reading things write, means that
    something like '\u20A0' only contains one c-char, and so should
    have type char---even if the encoding is UTF-8:).

    As a general rule, character constants work well for single
    characters in the basic execution set, but I'd avoid them for
    anything else.

    > > cout << bb.capacity() << endl;
    > > cout << bb.size() << endl;
    > > getchar();
    > > }


    > > And "bb.capacity()" returns "15" on my computer, which is
    > > wierd, where did it come from?


    > Capacity is the size of the string's buffer, size is the
    > number of characters in the buffer.


    > 15 does appear to be incorrect.


    Why? The only requirement is that capacity() >= size(). The
    post condition of the copy constructor called to initialize bb
    is that bb == str, where str is the other string. The ==
    operator ignores capacity, and it is usual that the copy
    constructor set the capacity to no more than is needed, modulo
    any internal constraints. The g++ implementation of
    std::string, for example, should more or less imposes a capacity
    which is a multiple of sizeof( size_t ), because of alignment
    issues (it doesn't---the actual implementation seems to ignore
    all alignment issues), and implementations which use the small
    string optimization impose a minimum capacity corresponding to
    the size of the small string. Implementations like g++ which
    use reference counting will, of course, also "copy" the
    capacity. Thus, while all the standard requires in the above is
    that capacity() >= 3 (which is the size); with g++, I'd expect
    at least 31, because no copy is actually being done. With VC++,
    15 (the size of its small string optimization), etc. With Sun
    CC (which also uses reference counting), I get 31 with exact
    program above, but if I insert a "char& r = cc[ 0 ];" (which
    inhibits sharing) before the initialization of bb, I get a
    capacity of 3.

    All perfectly conform.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Oct 23, 2008
    #4
  5. Luna Moon

    Luna Moon Guest

    On Oct 22, 7:01 pm, Barry <> wrote:
    > On Oct 23, 8:30 am, Luna Moon <> wrote:
    >
    >
    >
    >
    >
    > > #include "stdafx.h"
    > > #include <iostream>
    > > #include <string>
    > > using namespace std;
    > > int main()
    > > {
    > >   string cc(31, 'c');

    >
    > >   string bb=cc.assign(3, 'dd');

    >
    > >   cout << bb.capacity() << endl;

    >
    > >   cout << bb.size() << endl;

    >
    > >   getchar();

    >
    > > }

    >
    > > And "bb.capacity()" returns "15" on my computer, which is wierd, where
    > > did it come from?

    >
    > string::capacity >= string::size(), while the latter one indicates
    > the
    > data size. (capacity - size) indicates that you can push_back such
    > amount
    > of charT without allocating the new space.
    >
    > you can call "string::reserve" to affect the "capacity"
    > while you can call "string::resize" to affect the "size"
    >
    > --
    > Best Regards
    > Barry- Hide quoted text -
    >
    > - Show quoted text -


    What's the difference between "reserve" and "resize" then? I got more
    confused now...

    To me, they seem to be the same...

    Also, in string class, there is a member called "max_size", what's
    that? How is that different from "capacity" and "size"?
     
    Luna Moon, Oct 23, 2008
    #5
  6. Luna Moon

    Ian Collins Guest

    Re: what's the difference between "capacity" and "size" functionsof string class?

    Luna Moon wrote:
    >>
    >> --
    >> Best Regards
    >> Barry- Hide quoted text -
    >>
    >> - Show quoted text -

    >

    Please stop quoting signatures and google crap (the stuff above this line).

    > What's the difference between "reserve" and "resize" then? I got more
    > confused now...
    >
    > To me, they seem to be the same...
    >
    > Also, in string class, there is a member called "max_size", what's
    > that? How is that different from "capacity" and "size"?


    This was done to death a few days ago in the thread "reserve of vector".

    --
    Ian Collins
     
    Ian Collins, Oct 23, 2008
    #6
  7. Luna Moon

    Old Wolf Guest

    On Oct 23, 1:30 pm, Luna Moon <> wrote:
    >   cout << bb.capacity() << endl;
    >   cout << bb.size() << endl;
    >
    > And "bb.capacity()" returns "15" on my computer, which is wierd, where
    > did it come from?


    'size' is how many characters are in the string.
    'capacity' is how much memory is allocated. (some
    of it might not yet be in use).

    For example if 'capacity' is 15 and 'size' is 6
    then you can add up to 9 more characters to the
    string, and the string object is guaranteed to
    not request any more memory.

    'reserve' increases the capacity, and 'resize'
    changes the size. You would use capacity and
    reserve if you were interested in controlling
    when the string allocated memory.
     
    Old Wolf, Oct 24, 2008
    #7
    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. jakk
    Replies:
    4
    Views:
    12,393
  2. Timo Nentwig

    List.size() != List.capacity();

    Timo Nentwig, Apr 12, 2004, in forum: Java
    Replies:
    11
    Views:
    3,645
    Larry Coon
    Apr 13, 2004
  3. Gernot Frisch

    serialize ofstring to stream

    Gernot Frisch, Dec 6, 2004, in forum: C++
    Replies:
    3
    Views:
    386
    Konstantin Litvinenko
    Dec 6, 2004
  4. Replies:
    8
    Views:
    403
  5. puzzlecracker
    Replies:
    4
    Views:
    349
    Hendrik Schober
    Oct 14, 2008
Loading...

Share This Page