Please quote relevant context when posting a followup. I see that you
are using Google Groups, please read
<
http://cfaj.freeshell.org/google/> before posting again.
Dear Gamble
thanks for your reply.
I write a programe as follows ,and compile successfuly .
#include "stdio.h"
#include "string.h"
int main()
{
int x=35;
char str[10];
strcpy(str,"
www.google.com");
printf("The x value is :%d",x);
printf("The strlen(str) is :%d",strlen(str));
return 0;
}
the result is :
The x value is :28015The strlen(str) is :14Press any key to continue
I run in visual c++6.0.but when I compile it by gcc, another results
apears.
I do think the x value have some relation to ".com", but I can't find
it out.
As I explained in the post you are responding to, writing beyond the
end of an array invokes undefined behavior after which *anything* is
possible, the program is no longer bound by the rules of the language.
It is important to understand this. Undefined behavior can present in
any way, what follows is an observation of how this undefined behavior
apparently presented in your program.
From your output I can assume that the size of an int on your system is
4 bytes, the encoding is ASCII, and your platform is little-endian.
Your implementation is probably storing variables with automatic
storage on a stack, where the storage for variable x is located after
your array. The array size is 10 bytes, your compiler aligned x on a
4-byte boundary, 2 bytes after the array. So the first 10 bytes are
reserved for the array, the next 2 bytes are unused (padding), and the
following 4 bytes are reserved for x. Your call to strcpy copied
"
www.google" into the array, the next 2 bytes, ".c" to the unused
region of the stack, and the "om\0" into the first three bytes of x.
On a little endian platform the least significant bytes of an integer
value are stored at the left (lower address) increasing in signifcance
as you move to the right (higher addresses). The ASCII values for 'o'
and 'm' are 111 and 109 respectively so the bytes that make up x look
like this:
[111] [109] [0] [?] (Remember that strcpy wrote a '\0' at the end of
the string it copied)
If each byte consists of 8 bits, as appears to be the case on your
system, the value of x will be 111 + 109*256 + 0*256*256 +
?*256*256*256
111 + 109*256 = 28015 so the last byte denoted by the question mark
was apparently 0, that explains the value of x that you saw.
The strlen function returns the number of characters up to but not
including the nul character. The call to strcpy wrote 14 characters
followed by the nul character which explains the value returns by
strlen.
<OT>
Again, please realize that the behavior is undefined, the details above
are very specific to your system (byte sizes, alignment, endianness,
character encoding, type sizes, etc. are all system specific and not
all systems use stacks), and as you have already experienced the result
of this undefined behavior can manifest itself in different ways on
different systems or different compilers. In fact, running the same
code multiple times on the same implementation could result in
different results each time. You can never rely on the result of
undefined behavior. The above explanation was provided for academic
purposes only to satisfy your curiosity.
Robert Gamble