what is wrong in the code?

S

Sean Zhang

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!
 
S

Sean Zhang

Acctually, The error is just happedn in the last sentence! No undefined
behavior error!

Sean Zhang
Marc Boyer 写é“:
Le 20-04-2006 said:
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 = &s.i;
s.p[1] = 1;
UB

s.p[0] = 2;
OK
}

Marc Boyer
 
M

Marc Boyer

Le 20-04-2006 said:
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
 
R

Richard Heathfield

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

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.

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

Sean Zhang

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 写é“:
 
R

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

Ian Collins

Sean said:
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.
 
S

Sean Zhang

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 写é“:
 
I

Ian Collins

Sean 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. So, It is weird to me!
Please don't top post!

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

Richard Heathfield

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

Mark McIntyre

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
 
B

balasam

Sean said:
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.
 
R

Richard Heathfield

balasam said:
Sean said:
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.
 
K

Keith Thompson

Mark McIntyre said:
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.
 
K

Keith Thompson

Sean Zhang said:
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).
 
P

Peter Shaggy Haywood

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.

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"?
 

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
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top