Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this setjmp/jongjmp usable legal?

Reply
Thread Tools

Is this setjmp/jongjmp usable legal?

 
 
88888 Dihedral
Guest
Posts: n/a
 
      01-06-2013
Player於 2013年1月4日星期五UTC+8上午2時11分15秒 道:
> I'm not quite familiar with setjmp/longjmp (and neither with macros either) but I was trying to make something similar to this "yielding return" construct from C#, namely: when a function X "yields returns" some value everything happens like it has actually returned that value; however the state (local vars, IP) is saved, so that when X is called again it starts execution in the next statement after the last "yield return".
>
>
>
> Is this a portable legal solution, as long as I keep local variables volatile:
>
> 1 #include <stdio.h>
>
> 2 #include <setjmp.h>
>
> 3 #define STATE_FUNC static jmp_buf _env; static int first_time = 1; if (first_time) first_time = 0; else longjmp(_env, 1)
>
> 4 #define yield(X) do { if (!setjmp(_env)) return X; } while(0)
>
> 5
>
> 6 int next_integer()
>
> 7 {
>
> 8 STATE_FUNC;
>
> 9 while(1) {
>
> 10 yield(1);
>



Interesting yields here, well in K&R and ANSI C90,
there were no yields.

But that was trivial to use static vars and labels
to implement yields and iterators.

> 11 yield(2);
>
> 12 yield(3);
>
> 13 }
>
> 14 }
>
> 15
>
> 16
>
> 17 int main()
>
> 18 {
>
> 19 int p = 20;
>
> 20 while(p--) printf("%d", next_integer());
>
> 21 return 0;
>
> 22 }


 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-06-2013
On Jan 3, 8:11*pm, James Kuyper <(E-Mail Removed)> wrote:
> On 01/03/2013 02:52 PM, glen herrmannsfeldt wrote:
>
> > James Kuyper <(E-Mail Removed)> wrote:

>
> > (snip)

>
> >> Note: you should not display source code with line numbers; it's
> >> inconvenient for anyone who wants to copy your code and try to
> >> compile it.

>
> > Sometimes it does make the explanation easier, though.

>
> Comments are more useful than line numbers for that purpose, in my
> experience.
>
> > And it isn't that hard to remove them. One (ex) command in vi,

>
> Well, yes, but the ex commands supported by vi are a pretty powerful
> system; the command needed for this case wasn't very complicated, but
> I've known many people who were only moderately familiar with vi who
> would not have been able to figure out how to do it.
>
> > I presume also in many other editors.

>
> I've used many text editors other than 'vi' which had no method I could
> use to remove the line numbers that was any faster than manually going
> to each line and deleting them.


I've used editors with a "column select" and cut that made it fairly
easy
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-07-2013
Nick Keighley <(E-Mail Removed)> writes:
> On Jan 3, 8:11*pm, James Kuyper <(E-Mail Removed)> wrote:
>> On 01/03/2013 02:52 PM, glen herrmannsfeldt wrote:
>> > James Kuyper <(E-Mail Removed)> wrote:

>>
>> > (snip)

>>
>> >> Note: you should not display source code with line numbers; it's
>> >> inconvenient for anyone who wants to copy your code and try to
>> >> compile it.

[...]
>> > And it isn't that hard to remove them. One (ex) command in vi,

>>

[...]
>> I've used many text editors other than 'vi' which had no method I could
>> use to remove the line numbers that was any faster than manually going
>> to each line and deleting them.

>
> I've used editors with a "column select" and cut that made it fairly
> easy


Yes, the existence of editors that make removing line numbers easy
has already been established. The point is that not all editors
do so.

And even with vi, where I know how to remove line numbers with
a single command (though it depends on how the line numbers are
formatted and can mess up indentation), it's an extra step that
discourages me from copy-and-pasting the code to try it myself.
Someone who's asking for help should make things as easy as possible
people trying to help.

A comment like "/* this is line 42 */", where you want to refer to
line 42, is both easier to deal with and makes that line stand out
much better than numbering *every* line.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      01-07-2013
Keith Thompson <(E-Mail Removed)> writes:

> Nick Keighley <(E-Mail Removed)> writes:
>> On Jan 3, 8:11 pm, James Kuyper <(E-Mail Removed)> wrote:
>>> On 01/03/2013 02:52 PM, glen herrmannsfeldt wrote:
>>> > James Kuyper <(E-Mail Removed)> wrote:
>>>
>>> > (snip)
>>>
>>> >> Note: you should not display source code with line numbers; it's
>>> >> inconvenient for anyone who wants to copy your code and try to
>>> >> compile it.

> [...]
>>> > And it isn't that hard to remove them. One (ex) command in vi,

> [...]
>
> Yes, the existence of editors that make removing line numbers easy
> has already been established. [snip elaboration]
>
> A comment like "/* this is line 42 */", where you want to refer to
> line 42, is both easier to deal with and makes that line stand out
> much better than numbering *every* line.


I just don't agree with this, at least not always. Sometimes
putting in line numbers works just fine, and can work better than
putting markers in comments. In the particular example given at
the top of the thread, I wasn't bothered by the line numbers, nor
do I think comment markers would have been (for me) an improvement;
in fact probably the opposite.

I realize other people may react differently, and that is perfectly
okay - I don't expect other people will automatically like the same
things I do, or vice versa necessarily. But I have to raise an
objection when such a reaction is expressed like it's an absolute
statement, rather than just a personal or reader-dependent opinion,
which I believe is closer to the truth of the matter here.
 
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
IIS on XP Pro is now so slow it's not usable. =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= ASP .Net 6 09-22-2005 06:50 PM
How to join aspnet_Profile and aspnet_Users and get usable data. RedEye ASP .Net 0 09-01-2005 04:41 PM
Oldest usable computer for development ? msswasstastic@aol.com HTML 6 06-13-2005 06:52 PM
menu usable? chlori HTML 61 11-07-2004 10:56 PM
Which fixed size (width) font are usable/recommendable ? Arthur Connor HTML 9 01-07-2004 01:27 AM



Advertisments