From: Barry Schwarz on
On Sat, 24 Apr 2010 09:21:10 -0700 (PDT), Ali Karaali
<alicpp(a)gmail.com> wrote:

>On 7 Nisan, 11:26, Francis Glassborow
><francis.glassbo...(a)btinternet.com> wrote:
>> AnonMail2...(a)gmail.com wrote:
>> > On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote:
>> >> What is the difference between char *a="abc" and char b[]="abc"?
>>
>> > Note also that the former takes up more storage space. �*a allocates
>> > space for the characters (including an ending null) plus space for a
>> > pointer. �b[] just allocates space for the characters.
>>
>> > HTH
>>
>> Well I suppose it might, but it also might not. �Consider:
>>
>> void foo(){
>> � �char *a = "abc";
>> � �char b[] = "abc";
>> ...
>>
>> }
>>
>> The compiler allocates storage for a and initialises it to point to the
>> literal "abc". Then it allocates storage for and array of four
>> characters and initialises it by copying the string literal (which could
>> be the same string literal as that pointed to by a but is not required
>> to be.
>
>After copied the string literal, What is happening? The string literal
>still lives or what?
>If so, the program will have 2*4 characters since string literal lives
>whole execution time.
>One of them string literal, other one, the array. Do you see my point?

Not really.

The array object will occupy four characters regardless of what values
it contains. The string literal (technically the static array it gets
compiled to) will always have the four characters that were used to
create it. But, while the static array exists for the life of the
program, the array object (and the pointer) both exist only for the
life of the function. When foo returns, both a and b cease to exist
so only the static array remains.

Furthermore, if the only use of the string literal is to initialize an
array object (as would be the case if we removed the definition of a),
under the "as if" rule the compiler is allowed to optimize away the
resulting static array as long as the array object (b) is initialized
with the specified values.

So, the definition of a must always result in a static array and a
pointer. But the definition of b MUST result in an array object and
MAY result in a static array.

On the other hand let's assume sizeof a is 4. We know that a will be
initialized with the address of the static array. Will the compiler
store that four byte address somewhere and copy the value to a when
foo is entered? Does that mean the definition of a now requires 12
bytes (4 for a, 4 for the address, and 4 for the static array) while
the definition of b requires only 8 (4 for the array object and 4 for
the static array).

The correct answer is given in Francis' response below.

>> Assertions as to what compilers in general will do by way of storage
>> allocation are almost always wrong. Even assertions about what a single
>> compiler will do (with a specific set of compile switches) can be
>> mistaken as the compiler might be using context from elsewhere in the code.

--
Remove del for email
From: Ali Karaali on
On 24 Nisan, 20:45, Barry Schwarz <schwa...(a)dqel.com> wrote:
> On Sat, 24 Apr 2010 09:21:10 -0700 (PDT), Ali Karaali
>
>
>
> <ali...(a)gmail.com> wrote:
> >On 7 Nisan, 11:26, Francis Glassborow
> ><francis.glassbo...(a)btinternet.com> wrote:
> >> AnonMail2...(a)gmail.com wrote:
> >> > On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote:
> >> >> What is the difference between char *a="abc" and char b[]="abc"?
>
> >> > Note also that the former takes up more storage space.  *a allocates
> >> > space for the characters (including an ending null) plus space for a
> >> > pointer.  b[] just allocates space for the characters.
>
> >> > HTH
>
> >> Well I suppose it might, but it also might not.  Consider:
>
> >> void foo(){
> >>    char *a = "abc";
> >>    char b[] = "abc";
> >> ...
>
> >> }
>
> >> The compiler allocates storage for a and initialises it to point to the
> >> literal "abc". Then it allocates storage for and array of four
> >> characters and initialises it by copying the string literal (which could
> >> be the same string literal as that pointed to by a but is not required
> >> to be.
>
> >After copied the string literal, What is happening? The string literal
> >still lives or what?
> >If so, the program will have 2*4 characters since string literal lives
> >whole execution time.
> >One of them string literal, other one, the array. Do you see my point?
>
> Not really.
>
> The array object will occupy four characters regardless of what values
> it contains.  The string literal (technically the static array it gets
> compiled to) will always have the four characters that were used to
> create it.  But, while the static array exists for the life of the
> program, the array object (and the pointer) both exist only for the
> life of the function.  When foo returns, both a and b cease to exist
> so only the static array remains.
>
> Furthermore, if the only use of the string literal is to initialize an
> array object (as would be the case if we removed the definition of a),
> under the "as if" rule the compiler is allowed to optimize away the
> resulting static array as long as the array object (b) is initialized
> with the specified values.

Sorry but you misunderstood me,

I asked the question just for 'b'.
So if we only use the string literal is to initialize an array,
duration time of the array and the string literal is same.
I don't think so.

I just wanted to learn that. I know the 'a' 's situation.



> So, the definition of a must always result in a static array and a
> pointer.  But the definition of b MUST result in an array object and
> MAY result in a static array.
>
> On the other hand let's assume sizeof a is 4.  We know that a will be
> initialized with the address of the static array.  Will the compiler
> store that four byte address somewhere and copy the value to a when
> foo is entered?  Does that mean the definition of a now requires 12
> bytes (4 for a, 4 for the address, and 4 for the static array) while

Of course not.
You misunderstood me.

> the definition of b requires only 8 (4 for the array object and 4 for
> the static array).
>
> The correct answer is given in Francis' response below.
>
> >> Assertions as to what compilers in general will do by way of storage
> >> allocation are almost always wrong. Even assertions about what a single
> >> compiler will do (with a specific set of compile switches) can be
> >> mistaken as the compiler might be using context from elsewhere in the code.
>
> --
> Remove del for email

Ali
First  |  Prev  | 
Pages: 1 2 3
Prev: C++ code for parsing syllables?
Next: memcpy