473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what is wrong in the code?

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
18 1827
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
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
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
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
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
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
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
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
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
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
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
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
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

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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.