Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
VikingofRock
Aug 24, 2008




eschaton posted:

it's entirely legal to call main() recursively

by doing an exec() of your own binary with new arguments

(finding your binary's path is left as an exercise to the reader)

So I've been searching around some more and I'm becoming increasingly convinced that in C, recursive calls to main() are actually legal. I'm curious what the various compiler writers in this thread have to say about it though (I forget if you are one of them, so apologies if you are--I just remember you as one of the people who probably knows much more C/C++ than me).

Adbot
ADBOT LOVES YOU

Sapozhnik
Jan 2, 2005

Nap Ghost
shamefully quoting myself from ages ago but

quote:

comment threads about dumb programming poo poo always be like

"haha that guy can't tie his shoelaces"
"yeah ikr i am a grown rear end man and i can tie my own shoelaces, i shall now demonstrate on this video"
"wow you sure tied your shoelaces real good, my mummy says i am also very good at tying my own shoelaces mostly unassisted and i too am a grown rear end man"
"here is a series of videos of me tying red, orange, and yellow shoelaces, one after the other. you see it's really quite simple. i feel the need to validate my shoelace-tying abilities constantly"
"as a shoelace-tying master allow me to weigh in with my my opinion on the topic"

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

Sapozhnik posted:

shamefully quoting myself from ages ago but

why do u h8 fun mate?

VikingofRock
Aug 24, 2008




LeftistMuslimObama posted:

why do u h8 fun mate?

Agreed with LMO; I enjoy seeing interesting / unusual solutions to a well-known problem.

Mao Zedong Thot
Oct 16, 2008


code:

#![no_std]
#![feature(asm, lang_items, libc, no_std, start)]
 
extern crate libc;
 
const LEN: usize = 413;
static OUT: [u8; LEN] = *b"\
1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n\
16\n17\nFizz\n19\nBuzz\nFizz\n22\n23\nFizz\nBuzz\n26\nFizz\n28\n29\nFizzBuzz\n\
31\n32\nFizz\n34\nBuzz\nFizz\n37\n38\nFizz\nBuzz\n41\nFizz\n43\n44\nFizzBuzz\n\
46\n47\nFizz\n49\nBuzz\nFizz\n52\n53\nFizz\nBuzz\n56\nFizz\n58\n59\nFizzBuzz\n\
61\n62\nFizz\n64\nBuzz\nFizz\n67\n68\nFizz\nBuzz\n71\nFizz\n73\n74\nFizzBuzz\n\
76\n77\nFizz\n79\nBuzz\nFizz\n82\n83\nFizz\nBuzz\n86\nFizz\n88\n89\nFizzBuzz\n\
91\n92\nFizz\n94\nBuzz\nFizz\n97\n98\nFizz\nBuzz\n";
 
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
unsafe {
asm!(
"
mov $$1, %rax
mov $$1, %rdi
mov $0, %rsi
mov $1, %rdx
syscall
"
 :
 : "r" (&OUT[0]) "r" (LEN)
 : "rax", "rdi", "rsi", "rdx"
 :
);
}
0
}
 
#[lang = "eh_personality"] extern fn eh_personality() {}
#[lang = "panic_fmt"] extern fn panic_fmt() {}

quote:

Or the ultimate optimized version with hardcoded output, no standard library or main function, and direct assembly syscalls to write to stdout.

:negative:

From https://www.rosettacode.org/wiki/FizzBuzz#Rust

VikingofRock
Aug 24, 2008





See this is why Rust needs constexpr / turing-complete generics / whatever. You should be able to make the compiler generate that hardcoded output for you! (very cool solution btw)

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Sapozhnik posted:

shamefully quoting myself from ages ago but

man ur a fun guy

Sapozhnik
Jan 2, 2005

Nap Ghost
because the point of fizzbuzz is to prove that you aren't a complete charlatan and absolutely nothing beyond that. passing fizzbuzz with flying colors means you get to continue the phone screen beyond the first 5 minutes, it doesn't get you the job.

if u really feel the need to strenuously prove in a multitude of elaborate ways that you are not in fact a complete charlatan then idk what to tell u

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

VikingofRock posted:

So I've been searching around some more and I'm becoming increasingly convinced that in C, recursive calls to main() are actually legal. I'm curious what the various compiler writers in this thread have to say about it though (I forget if you are one of them, so apologies if you are--I just remember you as one of the people who probably knows much more C/C++ than me).

it's not really a debatable topic; the spec says something or it doesn't. C++ does not allow it.

C++14 Standard posted:

The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is ill- formed. The name main is not otherwise reserved.

Bloody
Mar 3, 2013

ugh fine here's one partial Verilog implementation
code:
module fizzbuzz(input wire reset_b,
, input wire clock_in
, output reg fizz
, output reg buzz
, output reg [6:0] number
, output reg output_valid
, input wire backpressure_in // optional backpressure
);

assign fizz = number % 3 == 0 ? 1'b1 : 1'b0;
assign buzz = number % 5 == 0 ? 1'b1 : 1'b0;

always @(posedge clock_in or negedge reset_b) begin
    if(reset_b == 0) begin
        fizz <= 0;
        buzz <= 0;
        number <= 1;
    end else begin
        if(number <= 100 && backpressure_in == 0) begin
            number <= number + 1;
            output_valid <= 1;
        end else begin
            output_valid <= 0;
        end
    end
end
endmodule
of course, since fizzbuzz specifies that it prints the related output to a console, there is quite a bit more work to do. I would consider a canonical implementation to probably also include a UART and a state machine for generating the right strings but for brevity's sake here's a module that will generate fizzes, buzzes, and fizzbuzzes for every number between 1 and 100 (inclusive) in exactly 100 clock cycles (best case, assuming no backpressure; a UART or similar console-related module would probably have to backpressure it while busy transcribing the last results to the output device)

VikingofRock
Aug 24, 2008




leper khan posted:

it's not really a debatable topic; the spec says something or it doesn't. C++ does not allow it.

Yeah but that's the C++ standards. As far as I can tell the C standards all allow it.

redleader
Aug 18, 2005

Engage according to operational parameters

leper khan posted:

it's not really a debatable topic; the spec says something or it doesn't. C++ does not allow it.

i'm not sure what 'shall not be used' means in the context of the c++ spec, but implementation-defined is not undefined so it might be cool sometimes?

VikingofRock
Aug 24, 2008




redleader posted:

i'm not sure what 'shall not be used' means in the context of the c++ spec, but implementation-defined is not undefined so it might be cool sometimes?

Nah I'm pretty sure that violating a "shall" means that your program is ill-formed, and thus doing so is UB. So I think it's pretty clear that you can not call main() recursively in C++.

JewKiller 3000
Nov 28, 2006

by Lowtax
make another recursive function, have main call it, whooooo caaaares wtbfd

redleader
Aug 18, 2005

Engage according to operational parameters
whoops i just wasted minutes of my life on this planet trying to find what the c++ spec means by "ill-formed" for some reason

Workaday Wizard
Oct 23, 2009

by Pragmatica

Soricidus posted:

is rust good at small programs? I thought it was a big complex language for big complex systems like browsers

it's very good. cargo and crates.io are good and cool.

i made some network protocol (!) parsers using nom and it was easy peasy (the whole code could fit in a single paper).

also multithreaded code is very easy in rust. check rayon if you want some quick parallelism wins.

ps: use rustup to install rust. it's the quickest way to dl and update the toolkit. it works perfectly on windows

Workaday Wizard fucked around with this message at 10:23 on Nov 23, 2016

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

VikingofRock posted:

Nah I'm pretty sure that violating a "shall" means that your program is ill-formed, and thus doing so is UB. So I think it's pretty clear that you can not call main() recursively in C++.

just make your program a quine that compiles and executes itself with new arguments to implement recursion

toiletbrush
May 17, 2010

Bognar posted:

2. using a weird .Any call instead of a foreach loop (or the .ForEach method)
I used to write an extension method on IEnumerable to add an '.each(...)' method but whenever I did I had a spidey sense tingle that it was a bad thing to do - partially because if it didn't already exist it was probably for a good reason, and also because the fact it didn't return anything meant it had to be causing side effects (not that calling that out is a bad thing). Then a developer joined the team who was much better than me and said 'dont do that' and sort of confirmed my spidey sense.

I'm not totally convinced though so I still do it in Swift :hehe:

Xarn
Jun 26, 2015
I see your TMP fizzbuzz and raise you TMP Turing Machine.

http://coliru.stacked-crooked.com/a/de06f2f63f905b7e

Xarn
Jun 26, 2015

Plorkyeran posted:

this would be under 50 lines of code with libclang and is a nice excuse to learn how to use it

Ok, I needed a reason to learn it anyway.

redleader
Aug 18, 2005

Engage according to operational parameters

toiletbrush posted:

I used to write an extension method on IEnumerable to add an '.each(...)' method but whenever I did I had a spidey sense tingle that it was a bad thing to do - partially because if it didn't already exist it was probably for a good reason, and also because the fact it didn't return anything meant it had to be causing side effects (not that calling that out is a bad thing). Then a developer joined the team who was much better than me and said 'dont do that' and sort of confirmed my spidey sense.

I'm not totally convinced though so I still do it in Swift :hehe:

c# hero eric lippert has written about why there isn't an IEnumerable.ForEach() method in the standard library - as you say, it basically boils down to "side effects"

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
I'm kind of pissed off at celery. they keep changing the default way to do stuff (like scheduling tasks), broke a lot of poo poo in version 4.0 and there's zero response from the developers.

what I'm saying is: don't choose celery for your project, maybe consider using erlang or something.

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

redleader posted:

whoops i just wasted minutes of my life on this planet trying to find what the c++ spec means by "ill-formed" for some reason

lomarf

the standard posted:

ill-formed program
program that is not well formed

the standard posted:

well-formed program
C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule (3.2).

leper khan fucked around with this message at 13:48 on Nov 23, 2016

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
oh you're just learning to program, why dont you check out erlang or <OBSCURE ML VARIANT>

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

CRIP EATIN BREAD posted:

oh you're just learning to program, why dont you check out erlang or <OBSCURE ML VARIANT>

an unironically good idea

MononcQc
May 29, 2007

it's a good idea to check a bunch of paradigms when you're feeling somewhat solid on your current ground just to see what else is out there and figuring out different ways to think about problems. Starting in any paradigm isn't too bad as long as you're going to look at other stuff sooner than later to avoid walling yourself up.

ThePeavstenator
Dec 18, 2012

:burger::burger::burger::burger::burger:

Establish the Buns

:burger::burger::burger::burger::burger:

CRIP EATIN BREAD posted:

oh you're just learning to program, why dont you check out erlang or <OBSCURE ML VARIANT>

consider this hosed up world tho:

many computer degrees start students that know nothing off with c++ or Java

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

ThePeavstenator posted:

consider this hosed up world tho:

many computer degrees start students that know nothing off with c++ or Java

back in university we started off with a toy assembler and C. sophomore year we had a course using MIPS.
good thing i already had a course using z80 in HS

do the kids not still get exposure to the joys of assembly?

9-Volt Assault
Jan 27, 2007

Beter twee tetten in de hand dan tien op de vlucht.

CRIP EATIN BREAD posted:

oh you're just learning to program, why dont you check out erlang or <OBSCURE ML VARIANT>

Haskell would be a good first language, i agree. Or perhaps F#.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
no, actually the best first language is C64 basic because that is what i learned first and i turned out fine.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Wheany posted:

i turned out fine.

uhhh

ThePeavstenator
Dec 18, 2012

:burger::burger::burger::burger::burger:

Establish the Buns

:burger::burger::burger::burger::burger:

leper khan posted:

back in university we started off with a toy assembler and C. sophomore year we had a course using MIPS.
good thing i already had a course using z80 in HS

do the kids not still get exposure to the joys of assembly?

I learned MIPS but I also go to a "good" school and am getting a 4 year degree. I don't see how it can be avoided in any CS-type degree tho.

Lutha Mahtin
Oct 10, 2010

Your brokebrain sin is absolved...go and shitpost no more!

leper khan posted:

back in university we started off with a toy assembler and C. sophomore year we had a course using MIPS.
good thing i already had a course using z80 in HS

do the kids not still get exposure to the joys of assembly?

my Computer Organization class in the mid 00s was all MIPS. the book was all diagrams of a MIPS chip and our assembly assignments were in a simulator for it. then a few semesters later we had to build our own personal compiler that output MIPS assembly. i didn't go to a prestigious school but the CS department seemed pretty on-point with teaching the basics and building on previous classes as you went along

we did use Java most of the time, but they chose it because "it's free, runs on everything, and for our courses everything will run identically", rather than for any fad reason. more than one of the long-time profs there told us stories about the bad old days when they got stuck with having to use some compiler package that cost a bunch of money, and the sales bullet point of "oh yeah this is identical across operating systems" was great until students bought copies for their home pcs/macs and whoops guess there are some subtle differences lol

Bloody
Mar 3, 2013

Bloody posted:

ugh fine here's one partial Verilog implementation
code:

module fizzbuzz(input wire reset_b,
, input wire clock_in
, output reg fizz
, output reg buzz
, output reg [6:0] number
, output reg output_valid
, input wire backpressure_in // optional backpressure
);

assign fizz = number % 3 == 0 ? 1'b1 : 1'b0;
assign buzz = number % 5 == 0 ? 1'b1 : 1'b0;

always @(posedge clock_in or negedge reset_b) begin
    if(reset_b == 0) begin
        fizz <= 0;
        buzz <= 0;
        number <= 1;
    end else begin
        if(number <= 100 && backpressure_in == 0) begin
            number <= number + 1;
            output_valid <= 1;
        end else begin
            output_valid <= 0;
        end
    end
end
endmodule

of course, since fizzbuzz specifies that it prints the related output to a console, there is quite a bit more work to do. I would consider a canonical implementation to probably also include a UART and a state machine for generating the right strings but for brevity's sake here's a module that will generate fizzes, buzzes, and fizzbuzzes for every number between 1 and 100 (inclusive) in exactly 100 clock cycles (best case, assuming no backpressure; a UART or similar console-related module would probably have to backpressure it while busy transcribing the last results to the output device)

I did this while very drunk and only see one error (the fizz and buzz can't be reset in there) and only a few design niggles

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

VikingofRock posted:

So I've been searching around some more and I'm becoming increasingly convinced that in C, recursive calls to main() are actually legal. I'm curious what the various compiler writers in this thread have to say about it though (I forget if you are one of them, so apologies if you are--I just remember you as one of the people who probably knows much more C/C++ than me).

gcc, on some architectures, notably windows, injects a call to _main at the beginning of main. _main runs things like static constructors on platforms that don't natively support static constructors in the executable format. depending on how it's implemented, it may not like being called twice

Luigi Thirty
Apr 30, 2006

Emergency confection port.

my ST and Amiga C compilers do the same, main is preceded by a call to _main which runs a bunch of setup code so you don't need boilerplate crap in every program

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av
c c++14 s: why don't queue and stack have a method to pop/dequeue and move the value?

ulmont
Sep 15, 2010

IF I EVER MISS VOTING IN AN ELECTION (EVEN AMERICAN IDOL) ,OR HAVE UNPAID PARKING TICKETS, PLEASE TAKE AWAY MY FRANCHISE

VikingofRock posted:

So I've been searching around some more and I'm becoming increasingly convinced that in C, recursive calls to main() are actually legal.

I think you're right - I looked though all the references to main() in C90 / C99 / C11 and didn't see anything barring it.

Xarn
Jun 26, 2015

hackbunny posted:

c c++14 s: why don't queue and stack have a method to pop/dequeue and move the value?

I could tell you, but then I would have to kill you. :v:

Is there a reason you cannot move from front()/top() and then pop?

Adbot
ADBOT LOVES YOU

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

hackbunny posted:

c c++14 s: why don't queue and stack have a method to pop/dequeue and move the value?

can someone explain this meme to me? its clearly from before my time

  • Locked thread