Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Sequence point doubt

Reply
Thread Tools

Sequence point doubt

 
 
Spoon
Guest
Posts: n/a
 
      11-19-2007
Hello,

Is the following code valid:

char buf[1024];
int size = 666;
size += sprintf(buf+size, "%d", size);
size += sprintf(buf+size, "%d", size);

(Expected behavior: at offset 666, buf contains "666669\0".)

Is it equivalent to the following code:

char buf[1024];
int size = 666;
int temp = sprintf(buf+size, "%d", size);
size += temp;
int temp = sprintf(buf+size, "%d", size);
size += temp;

AFAIU, the function call is a sequence point, therefore size can only be
updated after sprintf has been evaluated with the "old" value of size.

This reasoning might be incorrect if sprintf were a macro, right?
(Can it be?)

Regards.
 
Reply With Quote
 
 
 
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      11-19-2007
In article <47419cea$0$4790$>,
Spoon <root@localhost> wrote:
>Hello,
>
>Is the following code valid:
>
>char buf[1024];
>int size = 666;
>size += sprintf(buf+size, "%d", size);
>size += sprintf(buf+size, "%d", size);
>
>(Expected behavior: at offset 666, buf contains "666669\0".)
>
>Is it equivalent to the following code:
>
>char buf[1024];
>int size = 666;
>int temp = sprintf(buf+size, "%d", size);
>size += temp;
>int temp = sprintf(buf+size, "%d", size);
>size += temp;
>
>AFAIU, the function call is a sequence point, therefore size can only be
>updated after sprintf has been evaluated with the "old" value of size.


That's correct, though the reason I'd give is that both of the places
where size is evaluated in the sprintf call are used to determine the
value that is eventually stored into size (they're needed for the call
to sprintf, and the return from sprintf is added to the old value of
size).


>This reasoning might be incorrect if sprintf were a macro, right?
>(Can it be?)


Any standard library function is allowed to be a macro, but in most of
them are required to act enough like functions that you don't usually
need to care about the difference.

The only thing I can think of is that the return value doesn't depend
on the buffer you ask sprintf to print into, but I can't come up with a
reasonable macro implementation that would break this. So I'll go
ahead and claim that this is well-defined in every possible case,
secure in the knowledge that the language lawyers who have finished
their morning coffee will give several examples that will prove me
wrong.


dave

 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      11-19-2007
On 19 Nov, 14:25, Spoon <root@localhost> wrote:
> Hello,
>
> Is the following code valid:
>
> char buf[1024];
> int size = 666;
> size += sprintf(buf+size, "%d", size);
> size += sprintf(buf+size, "%d", size);
>
> (Expected behavior: at offset 666, buf contains "666669\0".)


Yes , it's valid. I would expect the same behaviour.

> Is it equivalent to the following code:
>
> char buf[1024];
> int size = 666;
> int temp = sprintf(buf+size, "%d", size);
> size += temp;
> int temp = sprintf(buf+size, "%d", size);


You are redefining temp.

> size += temp;


Apart from the mistake I pointed out they are equivalent.

> AFAIU, the function call is a sequence point, therefore size can only be
> updated after sprintf has been evaluated with the "old" value of size.


You have a sequence point just after the arguments to sprintf()
have been evaluated and before the actual call and another
sequence point just before sprintf() returns.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments