Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 16th, 2005, 01:55 PM
Radde
Guest
 
Posts: n/a
Default size of char ch[] = "" ??

Hi all,

int main()
{
char ch[] = "";
cout<<"size is"<<sizeof(ch)<<endl;
return 0;
}

the above code prints size of ch is 1, why is it?? even though ch[] is
empty, what for 1 byte?? Ideally size of ch[] should be zero by looking
at the code, but why 1??


Cheers..

  #2  
Old August 16th, 2005, 02:05 PM
benben
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??


"Radde" <msradder@gmail.com> wrote in message
news:1124196466.727712.321880@z14g2000cwz.googlegr oups.com...[color=blue]
> Hi all,
>
> int main()
> {
> char ch[] = "";
> cout<<"size is"<<sizeof(ch)<<endl;
> return 0;
> }
>
> the above code prints size of ch is 1, why is it?? even though ch[] is
> empty, what for 1 byte?? Ideally size of ch[] should be zero by looking
> at the code, but why 1??
>
>
> Cheers..
>[/color]

"" has a terminating zero ('\0') character so that makes the size 1.

Ben


  #3  
Old August 16th, 2005, 03:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??

Radde wrote:
[color=blue]
> Hi all,
>
> int main()
> {
> char ch[] = "";
> cout<<"size is"<<sizeof(ch)<<endl;
> return 0;
> }
>
> the above code prints size of ch is 1, why is it??[/color]

String literals are always terminated with a '\0' character.
Btw, even if this were possible as you want it, the compiler wouldn't accept
it, because objects must always have a size of at least one. So an array
with zero elements is not allowed in C++.
The reason for that is that each object must have a distinct address, and an
object that occupies no storage can't have an address.

  #4  
Old August 16th, 2005, 03:55 PM
Richard Herring
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??

In message <ddsrg4$tbf$01$1@news.t-online.com>, Rolf Magnus
<ramagnus@t-online.de> writes[color=blue]
>Radde wrote:
>[color=green]
>> Hi all,
>>
>> int main()
>> {
>> char ch[] = "";
>> cout<<"size is"<<sizeof(ch)<<endl;
>> return 0;
>> }
>>
>> the above code prints size of ch is 1, why is it??[/color]
>
>String literals are always terminated with a '\0' character.
>Btw, even if this were possible as you want it, the compiler wouldn't accept
>it, because objects must always have a size of at least one. So an array
>with zero elements is not allowed in C++.
>The reason for that is that each object must have a distinct address, and an
>object that occupies no storage can't have an address.
>[/color]
( Except when it can: T *p = new T[0]; :-)


--
Richard Herring
  #5  
Old August 17th, 2005, 02:15 PM
kwijibo28
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??


Richard Herring wrote:[color=blue]
> In message <ddsrg4$tbf$01$1@news.t-online.com>, Rolf Magnus
> <ramagnus@t-online.de> writes[color=green]
> >Radde wrote:
> >[color=darkred]
> >> Hi all,
> >>
> >> int main()
> >> {
> >> char ch[] = "";
> >> cout<<"size is"<<sizeof(ch)<<endl;
> >> return 0;
> >> }
> >>
> >> the above code prints size of ch is 1, why is it??[/color]
> >
> >String literals are always terminated with a '\0' character.
> >Btw, even if this were possible as you want it, the compiler wouldn't accept
> >it, because objects must always have a size of at least one. So an array
> >with zero elements is not allowed in C++.
> >The reason for that is that each object must have a distinct address, and an
> >object that occupies no storage can't have an address.
> >[/color]
> ( Except when it can: T *p = new T[0]; :-)
>[/color]

Well the object here is a pointer on some type T. The pointer occupies
some storage and have a distinct address. So Rolf`s definition still
hold for this example. It may contain the address on some valid memory
chuck or it may be set to 0 (a memory address that lead to undefined
behaviour if dereferenced).

kwijibo

  #6  
Old August 17th, 2005, 03:15 PM
Richard Herring
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??

In message <1124284031.592937.288480@f14g2000cwb.googlegroups .com>,
kwijibo28 <kwijibo28@hotmail.com> writes[color=blue]
>
>Richard Herring wrote:[color=green]
>> In message <ddsrg4$tbf$01$1@news.t-online.com>, Rolf Magnus
>> <ramagnus@t-online.de> writes[color=darkred]
>> >Radde wrote:
>> >
>> >> Hi all,
>> >>
>> >> int main()
>> >> {
>> >> char ch[] = "";
>> >> cout<<"size is"<<sizeof(ch)<<endl;
>> >> return 0;
>> >> }
>> >>
>> >> the above code prints size of ch is 1, why is it??
>> >
>> >String literals are always terminated with a '\0' character.
>> >Btw, even if this were possible as you want it, the compiler wouldn't accept
>> >it, because objects must always have a size of at least one. So an array
>> >with zero elements is not allowed in C++.
>> >The reason for that is that each object must have a distinct address, and an
>> >object that occupies no storage can't have an address.
>> >[/color]
>> ( Except when it can: T *p = new T[0]; :-)
>>[/color]
>
>Well the object here is a pointer on some type T. The pointer occupies
>some storage and have a distinct address. So Rolf`s definition still
>hold for this example. It may contain the address on some valid memory
>chuck or it may be set to 0 (a memory address that lead to undefined
>behaviour if dereferenced).[/color]

What is "it"? My 'p' contains a distinct address which is not 0.


--
Richard Herring
  #7  
Old August 17th, 2005, 04:25 PM
kwijibo28
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??

I'm sorry, it seems you are right. I've just took a look at the
standard and even if you allocate a zero length array the new operator
will return a "non-null pointer value". My initial thought was that
"new T[0]" must return a NULL.

This raise another question. Does omitting to delete a zero length
array will lead to a memory leak?
I guess so. Compiler must store the array length somewhere so the
delete[] operator will know how much memory to deallocate.

Kwijibo

  #8  
Old August 17th, 2005, 05:45 PM
Richard Herring
Guest
 
Posts: n/a
Default Re: size of char ch[] = "" ??

In message <1124292013.789533.69760@g44g2000cwa.googlegroups. com>,
kwijibo28 <kwijibo28@hotmail.com> writes[color=blue]
>I'm sorry, it seems you are right. I've just took a look at the
>standard and even if you allocate a zero length array the new operator
>will return a "non-null pointer value". My initial thought was that
>"new T[0]" must return a NULL.
>
>This raise another question. Does omitting to delete a zero length
>array will lead to a memory leak?[/color]

Yes, as Michiel Salters pointed out in
<1124182196.663538.271390@g14g2000cwa.googlegroups .com>
in the thread entitled "the pointer this"...
[color=blue]
>I guess so. Compiler must store the array length somewhere so the
>delete[] operator will know how much memory to deallocate.
>[/color]
Not necessarily: it might maintain separate pools for small objects.

But as MS said, whether that's so or not, each call of new T[0] must
return a *distinct* pointer, so eventually all distinct values will be
used up.

--
Richard Herring
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles