Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8581 > unrolled thread
| Started by | Angus <anguscomber@gmail.com> |
|---|---|
| First post | 2011-08-09 02:34 -0700 |
| Last post | 2011-08-11 00:30 -0500 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.c++
Don't understand sizeof char* array Angus <anguscomber@gmail.com> - 2011-08-09 02:34 -0700
Re: Don't understand sizeof char* array Ian Collins <ian-news@hotmail.com> - 2011-08-09 21:41 +1200
Re: Don't understand sizeof char* array Juha Nieminen <nospam@thanks.invalid> - 2011-08-09 11:30 +0000
Re: Don't understand sizeof char* array Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-09 08:48 -0400
Re: Don't understand sizeof char* array gwowen <gwowen@gmail.com> - 2011-08-10 01:23 -0700
Re: Don't understand sizeof char* array Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-10 09:00 -0400
Re: Don't understand sizeof char* array Immortal Nephi <Immortal_Nephi@hotmail.com> - 2011-08-10 18:17 -0700
Re: Don't understand sizeof char* array Paavo Helde <myfirstname@osa.pri.ee> - 2011-08-11 00:30 -0500
| From | Angus <anguscomber@gmail.com> |
|---|---|
| Date | 2011-08-09 02:34 -0700 |
| Subject | Don't understand sizeof char* array |
| Message-ID | <925ae74e-7c1e-401e-908e-36a2760f362b@y16g2000yqk.googlegroups.com> |
I have this array:
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
int sz = sizeof(s); // 20 why 20???
I would expect sizeof to report 17. Why is it reporting 20?
to work out the number of elements works ok:
sz = sizeof(char*); // 4 - (size of a pointer)
sz = sizeof(s) / sizeof(char*); // 5 (correct)
I suppose if sizeof(s) had returned 17 then the calculation of the no.
of elements would not be correct. But I would be interested to know
how the array takes up 20 (bytes?)?
Angus
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-09 21:41 +1200 |
| Message-ID | <9acdlmFv2pU5@mid.individual.net> |
| In reply to | #8581 |
On 08/ 9/11 09:34 PM, Angus wrote:
> I have this array:
> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
> int sz = sizeof(s); // 20 why 20???
>
> I would expect sizeof to report 17. Why is it reporting 20?
On your system, sizeof(char*) = 4 and you have 5 in the array.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-08-09 11:30 +0000 |
| Message-ID | <4e411a61$0$2853$7b1e8fa0@news.nbl.fi> |
| In reply to | #8581 |
Angus <anguscomber@gmail.com> wrote:
> I have this array:
> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
> int sz = sizeof(s); // 20 why 20???
>
> I would expect sizeof to report 17. Why is it reporting 20?
>
> to work out the number of elements works ok:
> sz = sizeof(char*); // 4 - (size of a pointer)
> sz = sizeof(s) / sizeof(char*); // 5 (correct)
>
> I suppose if sizeof(s) had returned 17 then the calculation of the no.
> of elements would not be correct. But I would be interested to know
> how the array takes up 20 (bytes?)?
You have an array of 5 pointers, each pointer taking 4 bytes. What else
do you expect?
(Those strings are somewhere else entirely and do not contribute to
the size of the array. It's just the pointers that point to the strings.)
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-08-09 08:48 -0400 |
| Message-ID | <j1rabc$3a7$1@dont-email.me> |
| In reply to | #8581 |
On 8/9/2011 5:34 AM, Angus wrote:
> I have this array:
> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
> int sz = sizeof(s); // 20 why 20???
>
> I would expect sizeof to report 17. Why is it reporting 20?
Why it is reporting 20 you've already found out (can you multiply 4 by
5?) but what is a mystery to me is how you came up with 17? For
instance, sizeof("cat") is 4 (it's an array of four characters that *has
to* include the terminating null character), and sizeof("mouse") is 6
(same principle). Even if you put those characters all in one array:
char s[] = "cat" "cat" "dog" "mouse" "rat";
sizeof(s) will be 18 because it has to add the terminating null character...
And, just for the sake of our children who will be forced to live in the
world where you're going to be programming, don't initialize a pointer
to NON-CONST char with a literal, OK? Your declaration should be
const char* s[] = ...
>
> to work out the number of elements works ok:
> sz = sizeof(char*); // 4 - (size of a pointer)
> sz = sizeof(s) / sizeof(char*); // 5 (correct)
>
> I suppose if sizeof(s) had returned 17 then the calculation of the no.
> of elements would not be correct. But I would be interested to know
> how the array takes up 20 (bytes?)?
Array takes up [sizeof(element) x numberofelements] bytes. Since it's
an array of pointers... Well, you have your explanation.
V
--
I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | gwowen <gwowen@gmail.com> |
|---|---|
| Date | 2011-08-10 01:23 -0700 |
| Message-ID | <4e6b1124-659c-481e-8e22-c1a436c2ed10@dc3g2000vbb.googlegroups.com> |
| In reply to | #8587 |
On Aug 9, 1:48 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 8/9/2011 5:34 AM, Angus wrote:
>
> > I have this array:
> > char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
> > int sz = sizeof(s); // 20 why 20???
>
> > I would expect sizeof to report 17. Why is it reporting 20?
>
> Why it is reporting 20 you've already found out (can you multiply 4 by
> 5?) but what is a mystery to me is how you came up with 17?
You know perfectly well how he came up with 17. You know why its
wrong, but you know how he came up with it.
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-08-10 09:00 -0400 |
| Message-ID | <j1tvct$hnj$1@dont-email.me> |
| In reply to | #8610 |
On 8/10/2011 4:23 AM, gwowen wrote:
> On Aug 9, 1:48 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
>> On 8/9/2011 5:34 AM, Angus wrote:
>>
>>> I have this array:
>>> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
>>> int sz = sizeof(s); // 20 why 20???
>>
>>> I would expect sizeof to report 17. Why is it reporting 20?
>>
>> Why it is reporting 20 you've already found out (can you multiply 4 by
>> 5?) but what is a mystery to me is how you came up with 17?
>
> You know perfectly well how he came up with 17. You know why its
> wrong, but you know how he came up with it.
Shhh! Don't tell him that! You know, give the man a fish...
[toc] | [prev] | [next] | [standalone]
| From | Immortal Nephi <Immortal_Nephi@hotmail.com> |
|---|---|
| Date | 2011-08-10 18:17 -0700 |
| Message-ID | <1fac3a93-382e-44da-8352-e7461b111261@l2g2000vbn.googlegroups.com> |
| In reply to | #8621 |
On Aug 10, 8:00 am, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 8/10/2011 4:23 AM, gwowen wrote:
>
> > On Aug 9, 1:48 pm, Victor Bazarov<v.baza...@comcast.invalid> wrote:
> >> On 8/9/2011 5:34 AM, Angus wrote:
>
> >>> I have this array:
> >>> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
> >>> int sz = sizeof(s); // 20 why 20???
>
> >>> I would expect sizeof to report 17. Why is it reporting 20?
>
> >> Why it is reporting 20 you've already found out (can you multiply 4 by
> >> 5?) but what is a mystery to me is how you came up with 17?
>
> > You know perfectly well how he came up with 17. You know why its
> > wrong, but you know how he came up with it.
>
> Shhh! Don't tell him that! You know, give the man a fish...
He does not need to include terminating null character in ALL string
if he does clever design.
Here is—
char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
Try this
char s[][ 3 ] =
{
{ ‘c’, ‘a’, ‘t’ },
{ ‘c’, ‘a’, ‘t’ },
{ ‘d’, ‘o’, ‘g’ },
{ ‘m’, ‘o’, ‘u’ },
{ ‘r’, ‘a’, ‘t’ }
};
You get all 15 characters. Create another variable to support more
than 3 characters like ‘m’, ‘o’, ‘u’ plus ‘s’ and ‘e’. You will get
17 characters.
Class design takes care to store size information. If you want to
convert from this class to char*, you have to include terminating null
character *ALWAYS*.
You can try to do “cout << s” and you will notice that all five words
are printed together into one word without space character before one
terminating null character is included.
[toc] | [prev] | [next] | [standalone]
| From | Paavo Helde <myfirstname@osa.pri.ee> |
|---|---|
| Date | 2011-08-11 00:30 -0500 |
| Message-ID | <Xns9F3E5687FFB0myfirstnameosapriee@216.196.109.131> |
| In reply to | #8654 |
Immortal Nephi <Immortal_Nephi@hotmail.com> wrote in
news:1fac3a93-382e-44da-8352-e7461b111261@l2g2000vbn.googlegroups.com:
> He does not need to include terminating null character in ALL string
> if he does clever design.
>
> Here is—
>
> char* s[] = {"cat", "cat", "dog", "mouse", "rat"};
>
> Try this
>
> char s[][ 3 ] {
should be: char s[][ 3 ] = {
> { ‘c’, ‘a’, ‘t’ },
> { ‘c’, ‘a’, ‘t’ },
> { ‘d’, ‘o’, ‘g’ },
> { ‘m’, ‘o’, ‘u’ },
> { ‘r’, ‘a’, ‘t’ }
> };
> You can try to do “cout << s” and you will notice that all five words
> are printed together into one word without space character before one
> terminating null character is included.
There is no terminating zero byte in the s array, so passing it to any
function which expects a zero-terminated string will most likely access
memory outside of the array, causing UB.
Also, std::cout << s does not do what you think as the type of s is not
"char*", but "char**". Probably you wanted to write
std::cout << *s;
cheers
Paavo
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web