Mail Archives: djgpp/1997/02/17/21:24:43
John M. Aldrich (fighteer AT cs DOT com) wrote:
: Ruiter de M wrote:
: >
: > : >char string[5]="abcde" and
: > : >char *string="abcde" are equivalent.
That's not what _I_ (mdruiter) wrote :)
: Here's something interesting... those two definitions are technically
: equivalent in function because of variable alignment in memory. Take a
: look:
:
: char string[5] = "abcde";
:
: This actually allocates eight bytes, not five, because of memory
: alignment. 'string' becomes an alias for the address of those eight
: bytes. The null at the end of the string will get put in the sixth
: byte, which, even though it was not actually allocated as part of the
: string, is guaranteed by the alignment to not be touched by any other
: variable. So even though this is completely wrong in form, it should
: never cause a crash. If you tried "char string[8] = "abcdefgh";"
: though...
:
: char *string = "abcde";
:
: This allocates two entities: 'string', which is a 4-byte pointer, and
: the string "abcde" itself, which is located somewhere in the program's
: static data space, and is also rounded up to eight bytes.
::
: Despite these differences, both can be referenced identically within the
: program.
I think not, see below.
: > Important difference: string[] is writable, *string is not.
:
: Not true. Both refer to a single character in memory, which the
: compiler is more than happy to let you modify. The only thing the
: compiler will complain about is trying to change the value of string
: itself, if it is allocated as an array. Of course, if you try to write
: to the pointer-allocated string, you are changing your program's static
: data space, which can have unpredictable effects if there are any other
: variables in your program that also refer to that same string.
Yes, you say _static_ data space. Which means it is not at all writable.
For instance:
-----
#include <stdio.h>
char string1[5]="abcde";
char *string2="abcde";
int main(void)
{
printf("Here before the first try:\n");
string1[0] = 'z';
printf("Here before the second try:\n");
string2[0] = 'z';
printf("All worked out well.\n");
return 0;
}
-----
Gives the following (at least on a SPARCstation with Solaris, I don't have
-----
DJGPP ready here.):
Here before the first try:
Here before the second try:
Segmentation Fault
-----
Not unpredictable at all. string1 is writable, string2 is not. Or am I getting
something wrong?
Maybe you mean there is no difference in _pointer_, which I think is not
true either.
For instance:
-----
#include <stdio.h>
char string1[5]="abcde";
char *string2="abcde";
int main(void)
{
printf("Here before the first try:\n");
string2 = NULL;
printf("Here before the second try:\n");
string1 = NULL;
printf("All worked out well.\n");
return 0;
}
-----
This doesn't compile, giving:
-----
test.c: In function `main':
test.c:11: incompatible types in assignment
-----
No error for string2, error for string1.
So. Different pointers to different memory. Or am I getting it wrong again?
--
Groeten, Michel. http://www.cs.vu.nl/~mdruiter
\----/==\----/
\ / \ / "Life is cool.", Beavis.
\/ \/
- Raw text -