473,413 Members | 1,727 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,413 software developers and data experts.

Precedence of operators

Hi All

I have the following C code:

#include "stdio.h"

int main()
{
int *piArr,iVal,i;
piArr=(int*)malloc(sizeof(int)*5);

for(i=0;i<5;i++)piArr[i]=i;

iVal=*(++piArr) + *(++piArr);

printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
return 0;
}

Result (MS VC compiler): 4,2
Result (gcc) : 3,2

Which one is correct? Or is the behaviour not defined by C standard
and hence is compiler specifc?

Thanks and regards
Harry

May 5 '07 #1
9 1416
harry said:
Hi All

I have the following C code:

#include "stdio.h"

int main()
{
int *piArr,iVal,i;
piArr=(int*)malloc(sizeof(int)*5);

for(i=0;i<5;i++)piArr[i]=i;

iVal=*(++piArr) + *(++piArr);

printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
return 0;
}

Result (MS VC compiler): 4,2
Result (gcc) : 3,2

Which one is correct?
Both. The C standard does not define the behaviour of your program, for
all kinds of bugs - er, I mean reasons.
Or is the behaviour not defined by C standard
and hence is compiler specifc?
Fix the bugs, and the behaviour will become well-defined.

Start by removing the malloc cast, so that the compiler will warn you
about the failure to include <stdlib.h>, and continue by changing
"stdio.h" to <stdio.h- then get rid of all your multiple
modifications of an object in a single statement, and continue by
worrying about your failure to check the result of malloc and your
misspelling of free().

That should keep you busy for a while.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 5 '07 #2

"harry" <ar********@gmail.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
Hi All

I have the following C code:

#include "stdio.h"

int main()
{
int *piArr,iVal,i;
piArr=(int*)malloc(sizeof(int)*5);

for(i=0;i<5;i++)piArr[i]=i;

iVal=*(++piArr) + *(++piArr);

printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
return 0;
}
Result (MS VC compiler): 4,2
Result (gcc) : 3,2

Which one is correct? Or is the behaviour not defined by C standard
and hence is compiler specifc?
Her's your problem
iVal=*(++piArr) + *(++piArr);

incrementing the same pointer twice in one expression or, technically, I
think, sequence point isn't allowed. Compilers have the same problems as
humans in understanding what it means.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
May 5 '07 #3
harry wrote, On 05/05/07 09:25:
Hi All

I have the following C code:

#include "stdio.h"
You should use angle brackets rather than quotes for system headers. You
also need stdlib.h since you use malloc.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *piArr,iVal,i;
It is best to avoid tabs in Usenet, they do not always survive to
peoples displays.
piArr=(int*)malloc(sizeof(int)*5);
You don't need to cast the return value of malloc. The compilers
complained because you did not include stdlib.h
piArr=malloc(5 * sizeof *piArr);
Note the change in how sizeof is being used.
for(i=0;i<5;i++)piArr[i]=i;
The space shortage ended decades ago, so I suggest you start using
spaces to make your code readable.
iVal=*(++piArr) + *(++piArr);
You have obviously not read the FAQ or much of the group. Go to
http://c-faq.com/ and read question 3.2
printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
No such function in C and in the language which has it you do not use
delete for memory allocated with malloc.
return 0;
}

Result (MS VC compiler): 4,2
Result (gcc) : 3,2

Which one is correct? Or is the behaviour not defined by C standard
and hence is compiler specifc?
You code is so broken that as far as the C standard is concerned no
compiler is required to compile it (they are also not required to reject
it) and if a compiler does build an executable that executable is
allowed to do anything, including making your mother show all the
embarrassing photos ever taken of you to all your friends and any
partner you might have. Such are the problems of undefined behaviour.
--
Flash Gordon
May 5 '07 #4
On May 5, 11:09 am, Richard Heathfield <r...@see.sig.invalidwrote:
harry said:


Hi All
I have the following C code:
#include "stdio.h"
int main()
{
int *piArr,iVal,i;
piArr=(int*)malloc(sizeof(int)*5);
for(i=0;i<5;i++)piArr[i]=i;
iVal=*(++piArr) + *(++piArr);
printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
return 0;
}
Result (MS VC compiler): 4,2
Result (gcc) : 3,2
Which one is correct?

Both. The C standard does not define the behaviour of your program, for
all kinds of bugs - er, I mean reasons.
Or is the behaviour not defined by C standard
and hence is compiler specifc?

Fix the bugs, and the behaviour will become well-defined.

Start by removing the malloc cast, so that the compiler will warn you
about the failure to include <stdlib.h>, and continue by changing
"stdio.h" to <stdio.h- then get rid of all your multiple
modifications of an object in a single statement, and continue by
worrying about your failure to check the result of malloc and your
misspelling of free().

That should keep you busy for a while.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.- Hide quoted text -

- Show quoted text -
oops....sorry, didnt look at the rest of stuff. Was too much bothered
with the result of the operation. Anyway, thanks for the answer.

May 5 '07 #5
Malcolm McLean wrote, On 05/05/07 10:10:
>
"harry" <ar********@gmail.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
>Hi All

I have the following C code:

#include "stdio.h"

int main()
{
int *piArr,iVal,i;
piArr=(int*)malloc(sizeof(int)*5);

for(i=0;i<5;i++)piArr[i]=i;

iVal=*(++piArr) + *(++piArr);

printf("%d,%d",iVal,*piArr);
piArr-=2;
delete(piArr);
return 0;
}
>Result (MS VC compiler): 4,2
Result (gcc) : 3,2

Which one is correct? Or is the behaviour not defined by C standard
and hence is compiler specifc?
Her's your problem
iVal=*(++piArr) + *(++piArr);

incrementing the same pointer twice in one expression or, technically, I
think, sequence point isn't allowed. Compilers have the same problems as
humans in understanding what it means.
You need to read it again, that is one problem among many. The version
of gcc I have will not even build an executable from that code, and nor
will g++. Fix that and another version of gcc I have will produce a
program that crashes before outputting anything.
--
Flash Gordon
May 5 '07 #6
(Probably not a person named) harry wrote:
iVal=*(++piArr) + *(++piArr);
Get a copy of the C Standard and read it. A draft of the latest
standard can be found if you google for "Ansi C Standard", a copy of
the C Standard can be downloaded from www.ansi.org for $18. For many
purposes, the free draft is almost as good as the official standard.

An object (piArr) is modified twice without any intervening sequence
point. This invokes undefined behavior.

As it invokes undefined behavior, _anything_ can happen. This is not
only limited to what you could reasonably could imagine, _anything_
can happen, including crashing your computer, letting it be attacked
by viruses, formatting all harddisks on all connected networks, and
costing you lots of money.

May 5 '07 #7
"christian.bau" <ch***********@cbau.wanadoo.co.ukwrites:
(Probably not a person named) harry wrote:
iVal=*(++piArr) + *(++piArr);

Get a copy of the C Standard and read it. A draft of the latest
standard can be found if you google for "Ansi C Standard", a copy of
the C Standard can be downloaded from www.ansi.org for $18. For many
purposes, the free draft is almost as good as the official standard.
And for even most purposes,
http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf is even better.

Yours,

--
Jean-Marc
May 5 '07 #8
harry wrote:
On May 5, 11:09 am, Richard Heathfield <r...@see.sig.invalidwrote:
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.- Hide quoted text -

- Show quoted text -

oops....sorry, didnt look at the rest of stuff. Was too much bothered
with the result of the operation. Anyway, thanks for the answer.

Please learn to trim your posts, in particular signatures (I've left
the example above). I know Google doesn't do that automatically, but
you can do it yourself.


Brian
May 5 '07 #9
harry wrote:
Hi All

I have the following C code:

/* mha: let's start by cleaning it up a little */

#include <stdio.h /* mha: if you meant "stdio.h", we have
no idea what your personal copy of
this header contains. The standard
one is <stdio.h*/
#include <stdlib.h /* mha: you forgot this */

int main()
{
unsigned int *piArr, *savedptr /* mha; to avoid messiness */ , iVal,
i;
const size_t howmany = 5; /* mha: to remove magic numbers */

piArr = malloc(howmany * sizeof *piArr); /* mha: replacing the
poor practice of
'(int *)
malloc(sizeof(int) *
5);' */
/* mha: test for failure added */
if (!piArr) {
fprintf(stderr, "malloc failed for piArr, quiting.\n");
exit(EXIT_FAILURE);
}

for (i = 0; i < howmany; i++) /* mha removing magic number */
piArr[i] = i;

savedptr = piArr; /* mha: to avoid messiness */

iVal = *(++piArr) + *(++piArr); /* mha: this is a mistake. You are
modifying piArr twice between
sequence points. Check the FAQ
(or an elementary text) please.
A helpful compiler might issue a
diagnostic telling you that the
result might not be undefined.
Enable such diagnostics, if you
can. */

printf("%u,%u", iVal, *piArr);
#if 0
/* mha: messy pointer games and call of non-existent function
'delete' replaced ... */
piArr -= 2;
delete(piArr);
#endif
free(savedptr); /* mha: ... here */

return 0;
}
Result (MS VC compiler): 4,2
Result (gcc) : 3,2
Which one is correct?
Neither (but _my_ copy of gcc produces "4,2", not "3,2".
Or is the behaviour not defined by C standard
and hence is compiler specifc?
No, it is not implementation-defined (a standard C term) but undefined.
It is a beginner's error. Don't try to squeeze everything into one
statement.

May 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrew Koenig | last post by:
It has been pointed out to me that various C++ books disagree about the relative precedence of ?: and the assignment operators. In order to satisfy myself about the matter once and for all, I...
9
by: Andy White | last post by:
the statement: foo.b = 7; indicates the first element of the array member of the data structure. My question is, why aren't the brackets evaluated first then the dot operator after since the...
5
by: Angel Tsankov | last post by:
Can someone give a reason why bitwise operators (&,^,|) have lower precedence than equality/inequality test oeprators (==, !=)?
25
by: noe | last post by:
Hello, I'm writing a file system filter driver and I've found in an example this sentence: if (VALID_FAST_IO_DISPATCH_HANDLER( fastIoDispatch, FastIoRead )) { return...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
11
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
7
by: lovecreatesbea... | last post by:
c-faq, 4.3: The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. BSD Manual, OPERATOR(7): Operator Associativity...
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
32
by: silpau | last post by:
hi, i am a bit confused on expression evaluation order in expressions involving unary increment.decrement operators along with binary operators. For example in the following expression x...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.