list container

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerry

    #1

    list container

    i have a problem when i read c++ primer ,i don't know what the follow
    codes meaing.

    char *word[]={"frank","engl ish","edali","s lina"};
    size_t word_size=sizeo f(word)/sizeof(char*);
    list<stringword 2(word,word+wor d_size);

    and then what's the contents of list? why the word_size=sizeo f(word)/
    sizeof(char*)?
    i really appreciate your help.
  • Pascal J. Bourguignon

    #2
    Re: list container

    jerry <machel2008@yah oo.cnwrites:
    i have a problem when i read c++ primer ,i don't know what the follow
    codes meaing.
    >
    char *word[]={"frank","engl ish","edali","s lina"};
    Assuming a system using the ASCII encoding, this sets up the program
    to have four static memory blocks (starting from 0x100000 for example)
    initialized with bytes, assuming 8-bit bytes:

    00010000: 66 72 61 6E 6B 00
    00010006: 65 6E 67 6C 69 73 68 00
    0001000E: 65 64 61 6C 69 00
    00010014: 73 6C 69 6E 61 00

    Then it allocates some memory block able to hold four pointers. The
    exact location of this memory block depends on the context of the
    definition. Let's assume it's an automatic variable, so the memory
    block will be allocated on the stack. Assume the stack grows
    "downward" (by decrementing the value of the stack pointer). word is
    defined to point to the address of this block, and the addresses of
    the four memory block defined above are stored in the memory block
    pointed to by word. For example, assuming 32-bit pointers:

    word: 00010000 00010006 0001000E 00010014

    size_t word_size=sizeo f(word)/sizeof(char*)
    sizeof(word) is the size of the memory block holding the four pointers.
    sizeof(char*) is the size of a memory blokc holding one pointer.
    The quotient is therefore then number of pointers stored in word, namely four.

    list<stringword 2(word,word+wor d_size);
    word is the pointer to the memory block holding the four pointers.
    word+word_size is a pointer just beyond that memory block.

    word: 00010000 00010006 0001000E 00010014
    word+word_size:

    list<string>(ch ar**,char**) is a constructor that will build a list of
    strings by copying the four pointers between word and word+word_size
    into strings (using the string(char*) constructor.

    Executing that instruction will thus build a list containing four
    strings containing the character sequences "frank", "english", "edali"
    and "slina".
    and then what's the contents of list? why the word_size=sizeo f(word)/
    sizeof(char*)?
    i really appreciate your help.
    Be sure to read some STL reference such as http://www.cplusplus.com/reference/
    --
    __Pascal Bourguignon__

    Comment

    • Road.Tang

      #3
      Re: list container

      On Oct 15, 4:49 pm, jerry <machel2...@yah oo.cnwrote:
      i have a problem when i read c++ primer ,i don't know what the follow
      codes meaing.
      >
      char *word[]={"frank","engl ish","edali","s lina"};
      first, you must be sure , the variable word is a array of *POINTER*
      to char.
      the element of word is POINTER with type char*.

      size_t word_size=sizeo f(word)/sizeof(char*);
      then , the sizeof(char*) is the sizeof to fixed pointer variable.
      which is a fixed number,only depend the computer you have.
      if you're in 32-bit machine, mostly the sizeof(char*) == 4.

      array word has 4 elements of char*, so sizeof(word) = 4*4 = 16.

      so it isr sure,
      count_of_elemen t = sizeof(alleleme nts) / sizeof(a elements);
      as you word,
      word_size=sizeo f(word)/sizeof(char*);

      list<stringword 2(word,word+wor d_size);
      this statemenet create a list variable , initialized with the
      element of your word array.

      list<stringword 2(word,word+wor d_size);
      equal to
      list<stringword 2(&word[0], &word[word_size]);
      so , you can see, all elements is used to initialize the word2 list.
      >
      and then what's the contents of list? why the word_size=sizeo f(word)/
      sizeof(char*)?
      i really appreciate your help.
      HTH
      -roadt

      Comment

      • red floyd

        #4
        Re: list container

        jerry wrote:
        i have a problem when i read c++ primer ,i don't know what the follow
        codes meaing.
        >
        char *word[]={"frank","engl ish","edali","s lina"};
        size_t word_size=sizeo f(word)/sizeof(char*);
        list<stringword 2(word,word+wor d_size);
        >
        and then what's the contents of list? why the word_size=sizeo f(word)/
        sizeof(char*)?
        i really appreciate your help.
        I believe your question is answered in FAQ 5.2



        Comment

        • James Kanze

          #5
          Re: list container

          On Oct 15, 10:49 am, jerry <machel2...@yah oo.cnwrote:
          i have a problem when i read c++ primer ,i don't know what the
          follow codes meaing.
          char *word[]={"frank","engl ish","edali","s lina"};
          It means that the author of the code is using a deprecated
          feature, which good programmers have avoided for many years now.
          That should be:

          char const* words[]
          = { "frank", "english", "edali", "slina" } ;

          Probably static, as well, and perhaps the pointers should be
          const too:

          static char const* const words[] = ...

          This declares an array of four pointers to a char; for
          historical reasons, they can either point to a single char,
          or the first char in an array of char. In this case, your
          initialization initializes them to point to four anonymous
          arrays of char const (which is why "char const*" is necessary,
          rather than simply "char*"). Each of these arrays is
          initialized with a '\0' terminates sequence of characters,
          specified by the string literals.

          The array has four elements because that's how many initializers
          you gave.
          size_t word_size=sizeo f(word)/sizeof(char*);
          This is an error prone and very out of date way of determining
          the number of elements in the array. Today, you'd almost
          certainly not use it---far better alternatives exist.
          list<stringword 2(word,word+wor d_size);
          This calles the two iterator constructor of list<string>, which
          uses the values between the begin and the end iterator to
          initialize the list.

          Today, the idiomatic way of writing this code would be:

          static char const* const words[]
          = { "frank", "english", "edali", "slina" } ;
          std::list< std::string words2( begin( words ), end( words ) ) ;

          Where you've got some library header which defines begin and end
          appropriately:

          template< typename T, std::size_t N >
          T*
          begin( T (&array)[ N ] )
          {
          return array ;
          }

          template< typename T, std::size_t N >
          T*
          end( T (&array)[ N ] )
          {
          return array + N ;
          }

          (And no, you're not expected to come up with these functions on
          your own. Or even really understand them if you're just
          starting with C++. But whatever text you're using to teach you
          the language should provide them, at least if it bothers to
          present C style arrays at all.)
          and then what's the contents of list? why the word_size=sizeo f(word)/
          sizeof(char*)?
          Because sizeof returns the number of bytes in an object, but
          pointer arithmetic works with the number of elements. Yet
          another broken feature we've inherited from C.

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...