By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

list container

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

char *word[]={"frank","english","edali","slina"};
size_t word_size=sizeof(word)/sizeof(char*);
list<stringword2(word,word+word_size);

and then what's the contents of list? why the word_size=sizeof(word)/
sizeof(char*)?
i really appreciate your help.
Oct 15 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
jerry <ma********@yahoo.cnwrites:
i have a problem when i read c++ primer ,i don't know what the follow
codes meaing.

char *word[]={"frank","english","edali","slina"};
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=sizeof(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<stringword2(word,word+word_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>(char**,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=sizeof(word)/
sizeof(char*)?
i really appreciate your help.
Be sure to read some STL reference such as http://www.cplusplus.com/reference/
--
__Pascal Bourguignon__
Oct 15 '08 #2

P: n/a
On Oct 15, 4:49*pm, jerry <machel2...@yahoo.cnwrote:
i have a problem when i read c++ primer ,i don't know what the follow
codes meaing.

char *word[]={"frank","english","edali","slina"};
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=sizeof(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_element = sizeof(allelements) / sizeof(a elements);
as you word,
word_size=sizeof(word)/sizeof(char*);

list<stringword2(word,word+word_size);
this statemenet create a list variable , initialized with the
element of your word array.

list<stringword2(word,word+word_size);
equal to
list<stringword2(&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=sizeof(word)/
sizeof(char*)?
i really appreciate your help.
HTH
-roadt
Oct 15 '08 #3

P: n/a
jerry wrote:
i have a problem when i read c++ primer ,i don't know what the follow
codes meaing.

char *word[]={"frank","english","edali","slina"};
size_t word_size=sizeof(word)/sizeof(char*);
list<stringword2(word,word+word_size);

and then what's the contents of list? why the word_size=sizeof(word)/
sizeof(char*)?
i really appreciate your help.
I believe your question is answered in FAQ 5.2

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2

Oct 15 '08 #4

P: n/a
On Oct 15, 10:49*am, jerry <machel2...@yahoo.cnwrote:
i have a problem when i read c++ primer ,i don't know what the
follow codes meaing.
char *word[]={"frank","english","edali","slina"};
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=sizeof(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<stringword2(word,word+word_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=sizeof(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:ja*********@gmail.com
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
Oct 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.