Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Pointers and Sequence Point |
| Date | 2011-05-02 08:23 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ipm7ot$ocm$1@dont-email.me> (permalink) |
| References | <4dbe97cd$0$311$14726298@news.sunsite.dk> |
On 05/02/2011 07:38 AM, arnuld wrote:
> I have this program which is causing confusion to me. I am told that last
> printf() of main() will always print "Second", not "First". I think line:
>
> x = myAlloc(x);
>
> is UB. My colleague is disagreeing on this while I was saying that its UB
> because there is no sequence point here. It is just like using this line:
>
> i = i++;
No, it's not. 6.5p2 contains two separate "shalls" that apply between
consecutive sequence points. "i=i++" violates the first one, against
modifying the value of an object twice. Your example modifies the value
of x only once. I presume, therefore, that you're actually worried about
the second one, against reading the value for any purpose other than to
determine what the new value is.
> But he says that sequence point will not affect the pointers and hence
> this is perfectly valid ANSI C program. I am confused on this:
There is no special exemption of pointers from the rules involving
multiple modifications with no intervening sequence points. "x=x++"
would be just as thoroughly undefined as "i = i++". It's just that
there's a whole lot more relevant sequence points involved in the
evaluation of "x = myAlloc(x)" than you're taking into consideration.
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> enum { SIZE_LIMIT = 10 };
>
> char* myAlloc(char* c);
>
> int main(void)
> {
> char* x = malloc(SIZE_LIMIT * sizeof *x);
> if(NULL == x)
> {
> printf("Out of Memory\n");
> return EXIT_FAILURE;
> }
>
> strcpy(x, "First");
> printf("x = %s\n", x);
> x = myAlloc(x);
There's a sequence point separating evaluation of 'x' as an argument to
myAlloc() from the call to myAlloc() (6.5.2.2p10).
> printf("x = %s\n", x);
>
> return 0;
> }
>
>
> char* myAlloc(char* c)
> {
> c = malloc(SIZE_LIMIT * sizeof *c);
There's a sequence point separating the multiplication from the call to
malloc(), another just before malloc() returns (7.1.4p3), a third at the
end of the full expression (6.8p4).
> if(NULL == c)
There's a sequence point at the end of the full expression.
> {
> printf("Out of Memory\n");
> return NULL;
> }
> else
> {
> strcpy(c, "Second");
There's a sequence point separating the evaluation of the arguments from
the call to strcpy(), a second just before strcpy() returns, and a third
at the end of the full expression.
> }
>
> return c;
There's a sequence point at the end of the full expression.
> }
I count a total of 9 sequence points separating the reading of the value
of x from its modification. One is all that's needed to avoid violating
6.5p2.
--
James Kuyper
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pointers and Sequence Point arnuld <sunrise@invalid.address> - 2011-05-02 11:38 +0000
Re: Pointers and Sequence Point James Kuyper <jameskuyper@verizon.net> - 2011-05-02 08:23 -0400
Re: Pointers and Sequence Point Martin Ambuhl <mambuhl@earthlink.net> - 2011-05-02 09:58 -0400
Re: Pointers and Sequence Point Chad <cdalten@gmail.com> - 2011-05-02 09:36 -0700
Re: Pointers and Sequence Point Angel <angel+news@spamcop.net> - 2011-05-02 17:21 +0000
Re: Pointers and Sequence Point James Kuyper <jameskuyper@verizon.net> - 2011-05-02 23:20 -0400
Re: Pointers and Sequence Point Keith Thompson <kst-u@mib.org> - 2011-05-02 23:26 -0700
Re: Pointers and Sequence Point James Kuyper <jameskuyper@verizon.net> - 2011-05-03 05:33 -0400
Re: Pointers and Sequence Point Kenneth Brody <kenbrody@spamcop.net> - 2011-05-02 13:35 -0400
Re: Pointers and Sequence Point Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-05-03 17:45 +0300
Re: Pointers and Sequence Point John Bode <jfbode1029@gmail.com> - 2011-05-02 10:17 -0700
Re: Pointers and Sequence Point Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-02 10:31 -0700
Re: Pointers and Sequence Point "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-05-02 20:55 -0400
Re: Pointers and Sequence Point David Resnick <lndresnick@gmail.com> - 2011-05-03 08:01 -0700
csiph-web