The value of variables

M

minlar

Hello,everyone:
what the value of the variables in the next programe:
{ int x=35;
char str[10];
strcpy(str,"www.google.com");}
what's the value of x and strlen(str)?
any help is welcome, and I am puzzled.
 
R

Robert Gamble

minlar said:
Hello,everyone:
what the value of the variables in the next programe:
{ int x=35;
char str[10];
strcpy(str,"www.google.com");}
what's the value of x and strlen(str)?
any help is welcome, and I am puzzled.

The call to strcpy invokes undefined behavior as it attempts to write
15 bytes to a 10-byte array. The values of x and strlen(str) can be
anything as the behavior of the program is no longer defined.

Robert Gamble
 
G

Gordon Burditt

what the value of the variables in the next programe:
{ int x=35;
char str[10];
strcpy(str,"www.google.com");}

You have invoked the wrath of undefined behavior by copying
beyond the end of str.
what's the value of x and strlen(str)?

Both of them might be 3.141592 (in spite of the fact that neither is
a floating-point type) but anything is possible, and nothing that
happens from here on can be considered "wrong".
any help is welcome, and I am puzzled.

Don't write code like this.

Gordon L. Burditt
 
M

minlar

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

minlar

In visual c++6.0,
when I change strcpy(str,"www.google.com"); to
strcpy(str,"www.google.??m");
the result is same as the first we get.
but if I change the last letter 'm' to other letter, the value of x is
different .
in gcc
when I change any one of the last three letters "com", the value of x
is different.
some one can tell me why?
 
I

Ian Collins

minlar said:
In visual c++6.0,
when I change strcpy(str,"www.google.com"); to
strcpy(str,"www.google.??m");
the result is same as the first we get.
but if I change the last letter 'm' to other letter, the value of x is
different .
in gcc
when I change any one of the last three letters "com", the value of x
is different.
some one can tell me why?
If you had quoted Robert's reply, you would see why. If you quoted your
own post, this one would make sense.

You have written past the end of an array. That is undefined behaviour.
Anything can happen, your toilet might even explode.
 
M

minlar

Ian said:
If you had quoted Robert's reply, you would see why. If you quoted your
own post, this one would make sense.

You have written past the end of an array. That is undefined behaviour.
Anything can happen, your toilet might even explode.
Thanks for your attention.
but the strlen(str) is always right. How strlen works?
 
I

Ian Collins

minlar said:
Thanks for your attention.
but the strlen(str) is always right. How strlen works?
Thanks for quoting. By the way, it's normal to drop the sig (the bit
including and below the "--". I think http://cfaj.freeshell.org/google/
explains how.

Now, back to your problem, strlen happens to work because strcpy stuck a
'\0' at the end of your overwritten string.

Different compilers may give different undefined results because they
may use different alignment of stack variables. If you want to check
this, look at the addresses of your int and your string variables.

Although this may appear to be a pointless exercise, it might help you
spot the symptoms of a buffer overflow next time you see one.
 
M

minlar

Ian said:
Thanks for quoting. By the way, it's normal to drop the sig (the bit
including and below the "--". I think http://cfaj.freeshell.org/google/
explains how.

Now, back to your problem, strlen happens to work because strcpy stuck a
'\0' at the end of your overwritten string.

Different compilers may give different undefined results because they
may use different alignment of stack variables. If you want to check
this, look at the addresses of your int and your string variables.

Although this may appear to be a pointless exercise, it might help you
spot the symptoms of a buffer overflow next time you see one.
Thank you very much .
 
R

Robert Gamble

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
 
K

Keith Thompson

minlar said:
Thanks for your attention.
but the strlen(str) is always right. How strlen works?

It's undefined behavior. That doesn't mean your program is going die
with a segmentation fault or die in some other spectacular manner. It
means the behavior is not defined. As far as the C standard is
concerned, it can do anything -- including behaving exactly as you
expect it to.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Start with
question 11.35.
 
R

Richard Heathfield

minlar said:
Hello,everyone:
what the value of the variables in the next programe:
{ int x=35;
char str[10];
strcpy(str,"www.google.com");}
what's the value of x and strlen(str)?
any help is welcome, and I am puzzled.


I live on the corner house, next to a busy road. From my gate to the corner,
there's 10 yards of fence. Yesterday, I painted the fence, starting at the
gate and proceeding to splash my lovely white paint over the next 15 yards
of fence. Curiously, as I was getting near the end, I noticed a very angry
motor-cyclist, wearing a blue cycle helmet with white paint all over where
the visor should be (silly man, wearing a helmet with paint all over it
like that - it's dangerous!), and shaking his fist at me as he drove away
from the traffic lights.

My question is this: when I paint my fence again tomorrow, will he shake his
fist at me again? And will he still be wearing that silly helmet?
 
M

Martin Ambuhl

minlar said:
Hello,everyone:
what the value of the variables in the next programe:
{ int x=35;
char str[10];
strcpy(str,"www.google.com");}

You just tried to write beyond the bounds of str.
If you're lucky, nothing worse than an exploding computer happened.
what's the value of x and strlen(str)? 42

any help is welcome, and I am puzzled.

Why be puzzled? You wrote code without defined behavior and you now
want us to define it for you?
 
M

minlar

Martin said:
Why be puzzled? You wrote code without defined behavior and you now
want us to define it for you?
Thanks for your question.
I knowed the error on defineing variables,actualy,how the computer
allocate memory is my puzzle .I want to know some physical handling
about it. Thanks to everyone who have answered my problem, you have
given me many hints.
 
C

Charles Richmond

minlar said:
In visual c++6.0,
when I change strcpy(str,"www.google.com"); to
strcpy(str,"www.google.??m");
the result is same as the first we get.
but if I change the last letter 'm' to other letter, the value of x is
different .
in gcc
when I change any one of the last three letters "com", the value of x
is different.
some one can tell me why?
What part of "undefined behavior" do you *not* understand???
When you violate the C standard and get undefined behavior,
*anything* can happen. Anything!!! You can get all sorts of
different results because the program *no* longer is required
to produce any specific result. Undefined behavior.
 
J

John F

minlar said:
Thanks for your question.
I knowed the error on defineing variables,actualy,how the computer
allocate memory is my puzzle .I want to know some physical handling
about it. Thanks to everyone who have answered my problem, you have
given me many hints.


Ask you implementation for mor information about that. But remember:
Undefined behavior is worse than implementationspecific behaviour. So
your implementation might tell you that it is undefined too (which is
very likely).
 
A

Andrew Poelstra

Thanks for your question.
I knowed the error on defineing variables,actualy,how the computer
allocate memory is my puzzle .I want to know some physical handling
about it. Thanks to everyone who have answered my problem, you have
given me many hints.
Physical handling of the memory is system-dependant. It won't act the
same on different systems. If I paint my fence over here in Western
Canada, Richard's cyclist won't be shaking his fist at me. Perhaps a
bearded police officer named Dan will. Or maybe my fence will explode
because one of my friends decided to bomb it.
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top