Author: Dann Corbit
Date: 17:25:42 03/19/99
Go up one level in this thread
On March 19, 1999 at 19:13:58, Bruce Moreland wrote:
[snip]
>char * sz = "chicken";
>char asz[] = "chicken";
>
>These are not the same thing, by any stretch.
>
>The first one is a 4-byte hunk of memory with a number in it. The number is the
>address of some static data, in this case 8 bytes worth.
>
>The second one is just the 8 bytes of static data.
>
>When the compiler wants to get at the first byte in the word "chicken", in the
>first example it has to go grab the pointer out of memory, then indirect it.
>
>In the second case the address of the data is compiled in. It simply has it
>already, it is a constant, it just indirects through this constant address,
>there is no extra load.
>
>Your example was something very similar, but no, this should not cost you 30%.
>The performance decrease is still an open issue.
They are even more different than you suggest. Array asz is updatable as in:
asz[0] = 'C';
but sz cannot be written to because the string can be stored in read only memory
areas. From the C FAQ:
1.32: What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
A: A string literal can be used in two slightly different ways. As
an array initializer (as in the declaration of char a[]), it
specifies the initial values of the characters in that array.
Anywhere else, it turns into an unnamed, static array of
characters, which may be stored in read-only memory, which is
why you can't safely modify it. In an expression context, the
array is converted at once to a pointer, as usual (see section
6), so the second declaration initializes p to point to the
unnamed array's first element.
(For compiling old code, some compilers have a switch
controlling whether strings are writable or not.)
See also questions 1.31, 6.1, 6.2, and 6.8.
References: K&R2 Sec. 5.5 p. 104; ISO Sec. 6.1.4, Sec. 6.5.7;
Rationale Sec. 3.1.4; H&S Sec. 2.7.4 pp. 31-2.
In any case, the difference in addressing an array and a pointer should be very,
very close to zero. If the array is passed into a function (not wrapped in a
struct or union) then they should be identical in speed.
This is a very strange behavior. I wonder if something else has changed also.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.