 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
harry Guest
|
Posted: Sat May 05, 2007 9:11 am Post subject: 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 |
|
| Back to top |
|
 |
Malcolm McLean Guest
|
Posted: Sat May 05, 2007 9:11 am Post subject: Re: Precedence of operators |
|
|
"harry" <arijit.bee (AT) gmail (DOT) com> wrote in message
news:1178353519.927994.71590 (AT) w5g2000hsg (DOT) googlegroups.com...
| Quote: | 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 |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Sat May 05, 2007 9:12 am Post subject: Re: Precedence of operators |
|
|
harry said:
| Quote: | 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.
| Quote: | 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. |
|
| Back to top |
|
 |
Jean-Marc Bourguet Guest
|
Posted: Sat May 05, 2007 3:48 pm Post subject: Re: Precedence of operators |
|
|
"christian.bau" <christian.bau (AT) cbau (DOT) wanadoo.co.uk> writes:
| Quote: | (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/wg14/www/docs/n1124.pdf is even better.
Yours,
--
Jean-Marc |
|
| Back to top |
|
 |
Default User Guest
|
Posted: Sat May 05, 2007 9:51 pm Post subject: Re: Precedence of operators |
|
|
harry wrote:
| Quote: | On May 5, 11:09 am, Richard Heathfield <r...@see.sig.invalid> wrote:
--
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 |
|
| Back to top |
|
 |
Martin Ambuhl Guest
|
Posted: Sat May 05, 2007 11:43 pm Post subject: Re: Precedence of operators |
|
|
harry wrote:
| Quote: | 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;
}
| Quote: | 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".
| Quote: | 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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|