[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

list container

jerry

10/15/2008 8:50:00 AM

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<string> word2(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.
4 Answers

pjb

10/15/2008 9:26:00 AM

0

jerry <machel2008@yahoo.cn> writes:

> 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<string> word2(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/...
--
__Pascal Bourguignon__

Road.Tang

10/15/2008 9:32:00 AM

0

On Oct 15, 4:49 pm, jerry <machel2...@yahoo.cn> 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"};
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<string> word2(word,word+word_size);
this statemenet create a list variable , initialized with the
element of your word array.

list<string> word2(word,word+word_size);
equal to
list<string> word2(&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

red floyd

10/15/2008 1:41:00 PM

0

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<string> word2(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-lite/how-to-post.ht...

James Kanze

10/15/2008 7:34:00 PM

0

On Oct 15, 10:49 am, jerry <machel2...@yahoo.cn> 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"};

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<string> word2(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:james.kanze@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