Precedence of operators

H

harry

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;

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
 
R

Richard Heathfield

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;

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

Malcolm McLean

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;

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

Flash Gordon

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 said:
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;


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

harry

harry said:




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;


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

Flash Gordon

Malcolm McLean wrote, On 05/05/07 10:10:
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;

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

christian.bau

(Probably not a person named) harry said:
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.
 
D

Default User

harry said:
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
 
M

Martin Ambuhl

harry said:
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;

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top