By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

what is wrong in the code?

P: n/a
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
p[1] = 3;
s.p = p;
s.p[1] = 1;
s.p[0] = 2;
}
I don't why cause the problem occurred in the last sentence?
Thanks a lot!

Apr 20 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Le 20-04-2006, Sean Zhang <se*********@gmail.com> a crit*:
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
OK: p[0] == *p == s.i
You code is equivalent to
s.i= 4
p[1] = 3;
Undefined behavior. p[1] does not exists !
s.p = p;
s.p = &s.i;
s.p[1] = 1;
UB
s.p[0] = 2;
OK }


Marc Boyer
Apr 20 '06 #2

P: n/a
Acctually, The error is just happedn in the last sentence! No undefined
behavior error!

Sean Zhang
Marc Boyer 写道:
Le 20-04-2006, Sean Zhang <se*********@gmail.com> a écrit*:
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;


OK: p[0] == *p == s.i
You code is equivalent to
s.i= 4
p[1] = 3;


Undefined behavior. p[1] does not exists !
s.p = p;


s.p = &s.i;
s.p[1] = 1;


UB
s.p[0] = 2;


OK
}


Marc Boyer


Apr 20 '06 #3

P: n/a
Sean Zhang said:
The following is the code:
struct S {
int i;
int * p;
};
void main()
Here is your first problem. The function called 'main' is called by the
system, and so the interface specification is carefully defined by the
language. It isn't your decision. There are two core forms that are
portable across all hosted implementations, and the one you need is:

int main(void)

{
Struct S s;
This is okay. It creates storage for a struct S.
int * p = &s.i;
This is fine. It takes and stores the address of s.i.
p[0] = 4;
This is fine too. It stores the value 4 in s.i.
p[1] = 3;


This is not fine. You are trying to write the value 3 into an array element
that does not exist. The result is undefined behaviour.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #4

P: n/a
Sean Zhang said:
Acctually, The error is just happedn in the last sentence! No undefined
behavior
Wrong. The behaviour is undefined from the very first call to main.
error!


Yes, error. Just because you don't get a message saying it's wrong, that
doesn't mean it's right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #5

P: n/a
Le 20-04-2006, Sean Zhang <se*********@gmail.com> a crit*:
Acctually, The error is just happedn in the last sentence! No undefined
behavior error!


Newby on Usenet ? Please, answer after the message your are replying
to.

On your problem, did you know what means 'undefined behavior' ?
It means that 'from now, everything can happen', and yes often,
whith UB, the program 'seems to work' some time, and then
crash after the problem itself.

With some experience on C and on how typical compiler
works, one can often guess why it crashes at a given line and
not at the UB itself.
But to get a correct behavior, avoid UB.

Marc Boyer
Apr 20 '06 #6

P: n/a
Yeah, you are right in the syntax perspective! But I want to know why
the last sentence produce the runtime error when I execute the binary
code after I compile it on VC6!

Sean
Richard Heathfield 写道:
Sean Zhang said:
Acctually, The error is just happedn in the last sentence! No undefined
behavior


Wrong. The behaviour is undefined from the very first call to main.
error!


Yes, error. Just because you don't get a message saying it's wrong, that
doesn't mean it's right.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Apr 20 '06 #7

P: n/a
Sean Zhang said:
Yeah, you are right in the syntax perspective!
I didn't say anything about syntax. I said a couple of things about
undefined behaviour.
But I want to know why
the last sentence produce the runtime error when I execute the binary
code after I compile it on VC6!


It is because your program has at least two instances of undefined
behaviour. One is your misdefinition of main's interface, and the other is
that you are writing to an object that doesn't exist.

Either of these can be sufficient to cause a runtime error.

The fix is to remove instances of undefined behaviour from your program.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #8

P: n/a
Sean Zhang wrote:
Yeah, you are right in the syntax perspective! But I want to know why
the last sentence produce the runtime error when I execute the binary
code after I compile it on VC6!

Please don't top post!

Who knows? You have indirectly set s.p to 1, so all bets are off.

--
Ian Collins.
Apr 20 '06 #9

P: n/a
Thanks to Marc Boyer and you!
I have tested it in this way:
If I reverse the last two sentence, then compile and run it, it is
normal, no runtime error under windows o.s. So, It is weird to me!

Sean
Richard Heathfield 写道:
Sean Zhang said:
Yeah, you are right in the syntax perspective!


I didn't say anything about syntax. I said a couple of things about
undefined behaviour.
But I want to know why
the last sentence produce the runtime error when I execute the binary
code after I compile it on VC6!


It is because your program has at least two instances of undefined
behaviour. One is your misdefinition of main's interface, and the other is
that you are writing to an object that doesn't exist.

Either of these can be sufficient to cause a runtime error.

The fix is to remove instances of undefined behaviour from your program.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Apr 20 '06 #10

P: n/a
Sean Zhang wrote:
Thanks to Marc Boyer and you!
I have tested it in this way:
If I reverse the last two sentence, then compile and run it, it is
normal, no runtime error under windows o.s. So, It is weird to me!

Please don't top post!

If you play with fire, or undefined behaviour, you will get burnt.

--
Ian Collins.
Apr 20 '06 #11

P: n/a
Sean Zhang said:
Thanks to Marc Boyer and you!
I have tested it in this way:
If I reverse the last two sentence, then compile and run it, it is
normal, no runtime error under windows o.s.
It's still wrong, though.
So, It is weird to me!


Making random changes to the code will not help you to understand it.

There are at least two problems with your code. I pointed these out to you
earlier on. When they are both fixed, your program will have two fewer
bugs, and therefore its behaviour will be easier for you to understand.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #12

P: n/a
Richard Heathfield <in*****@invalid.invalid> wrote:
Sean Zhang said:
The following is the code: <...>

In addition to what others pointed out,
Struct S s;


'Struct' should be 'struct'

Apr 20 '06 #13

P: n/a
On 20 Apr 2006 02:41:56 -0700, in comp.lang.c , "Sean Zhang"
<se*********@gmail.com> wrote:
I have tested it in this way:
If I reverse the last two sentence, then compile and run it, it is
normal, no runtime error under windows o.s. So, It is weird to me!


It only worked by accident. Its still broken code.

I saw a good analogy for this recently:

Say you get into your car and put on a blindfold. You start driving
blind. You don't necessarily crash into anything immediately. You may
never crash, if you're lucky. You still made a serious error right at
the start. The fact that you didn't have a crash doesn't change the
fact that you did something bad....

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 20 '06 #14

P: n/a

Sean Zhang wrote:
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
p[1] = 3;
s.p = p;
s.p[1] = 1;
s.p[0] = 2;
}
I don't why cause the problem occurred in the last sentence?
Thanks a lot!


Dear friend,
Your are not allocating memory for the "s.p" .Only you assign
the address of p.One thing you note that you are accessing the memory
in an illegal way.

Apr 20 '06 #15

P: n/a
balasam said:

Sean Zhang wrote:
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
p[1] = 3;
s.p = p;
s.p[1] = 1;
s.p[0] = 2;
}
I don't why cause the problem occurred in the last sentence?
Thanks a lot!
Dear friend,
Your are not allocating memory for the "s.p" .Only you assign
the address of p.


He gives p a value, anyway.

Not allocating the memory is not (exactly) the problem. He does, after all,
point s.p at a legal object (s.i, in other words).
One thing you note that you are accessing the memory
in an illegal way.


That is certainly true.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 20 '06 #16

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On 20 Apr 2006 02:41:56 -0700, in comp.lang.c , "Sean Zhang"
<se*********@gmail.com> wrote:
I have tested it in this way:
If I reverse the last two sentence, then compile and run it, it is
normal, no runtime error under windows o.s. So, It is weird to me!


It only worked by accident. Its still broken code.

I saw a good analogy for this recently:

Say you get into your car and put on a blindfold. You start driving
blind. You don't necessarily crash into anything immediately. You may
never crash, if you're lucky. You still made a serious error right at
the start. The fact that you didn't have a crash doesn't change the
fact that you did something bad....


Suppose the first time you try this, you run into something. You
stop, take off the blindfold, reverse it, and put it back on again.
This time you don't run into anything, and you arrive at your
destination. Reversing the blindfold didn't correct the problem; you
just got lucky.

Reversing the last two statements of your program (note: C has
declarations and statements, not "sentences") didn't fix anything, it
just randomly changed the (already wrong) behavior of your program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 20 '06 #17

P: n/a
"Sean Zhang" <se*********@gmail.com> writes:
The following is the code:
struct S {
int i;
int * p;
};
void main()
{
Struct S s;
int * p = &s.i;
p[0] = 4;
p[1] = 3;
s.p = p;
s.p[1] = 1;
s.p[0] = 2;
}
I don't why cause the problem occurred in the last sentence?


No, that isn't the actual code. If it were, it wouldn't have
compiled, since you misspelled "struct" as "Struct". (Thanks to
Roberto Waltman for noticing this; I completely missed it myself.)

If you want to ask us about your code, you need to show us the *exact*
code. Don't re-type it; copy-and-paste it. Don't make us waste our
time guessing which errors are in your original code and which you
introduced by posting an approximation to it.

And if you're having a problem, tell us what the problem is. From
your description, we can't even tell whether the problem occurs at
compile time or run time. Show us the error message (and again,
copy-and-paste the *exact* message, don't re-type it).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 20 '06 #18

P: n/a
Groovy hepcat Sean Zhang was jivin' on 20 Apr 2006 02:13:06 -0700 in
comp.lang.c.
what is wrong in the code?'s a cool scene! Dig it!
The following is the code:
struct S {
int i;
int * p;
};
void main()
main() should return int, not void.
{
Struct S s;
There's no such thing as Struct. Remember, C is a case sensitive
language. "Struct" (with a capitol "S") is not the same as "struct".
int * p = &s.i;
Fine.
p[0] = 4;
Fine.
p[1] = 3;
BZZZZT! Undefined behaviour. p[1] doesn't exist.
s.p = p;
Fine.
s.p[1] = 1;
BZZZZT! Undefined behaviour. s.p[1] doesn't exist.
s.p[0] = 2;
Fine.
You really need to return a value (after changing main()'s return
type). You can portably return 0, EXIT_SUCCESS or EXIT_FAILURE (the
latter two being macros defined in stdlib.h).
}
I don't why cause the problem occurred in the last sentence?


I realise English is probably not your first language, but I don't
know what that's supposed to mean. There are problems throughout this
code. This could not even have compiled. It is, no doubt, not the code
you actually tried to compile. Don't retype code here. Copy and paste,
so we are sure to see the real code.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Apr 22 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.