size of char ch[] = "" ??

R

Radde

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..
 
B

benben

Radde said:
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..

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

Ben
 
R

Rolf Magnus

Radde said:
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.
 
R

Richard Herring

Rolf Magnus said:
Radde said:
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.
( Except when it can: T *p = new T[0]; :)
 
K

kwijibo28

Richard said:
Rolf Magnus said:
Radde said:
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.
( Except when it can: T *p = new T[0]; :)

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
 
R

Richard Herring

Richard said:
Rolf Magnus said:
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.
( Except when it can: T *p = new T[0]; :)

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).

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

kwijibo28

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
 
R

Richard Herring

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?

Yes, as Michiel Salters pointed out in
I guess so. Compiler must store the array length somewhere so the
delete[] operator will know how much memory to deallocate.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,539
Members
48,286
Latest member
Amaya678

Latest Threads

Top