Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Quick Compiler Question

Name: Anonymous 2010-02-27 9:06

So I was thinking about C programs a short while ago, and a problem occurred to me.

Let's say that I have a function in which, say, we define a variable. It might look something like this:

int dicks (int cocks)
{
    int wangs;
    /* Do stuff to wangs and cocks here */
    return wangs;
}


Looking at int wangs; it seems that the compiler translates that line into "allocate space for an integer and return a pointer to it." If this is a case, is the compiler setting aside space for a new integer every time I run the function, or does it optimize by assuming that I'm only going to have one instance of it and just set aside space for a single int?

Whatever the answer to that previous question is, how would the compiler handle a recursive function? How would


Never mind, I'm an idiot. I forgot about registers and the stack for a second. I revoke my previous questions.

NEW QUESTION: What is the point of tail-call optimization? If all it does is convert recursive functions into iterative ones, why not just program them that way in the first place?

Name: Anonymous 2010-02-27 11:22

NEW QUESTION: What is the point of tail-call optimization? If all it does is convert recursive functions into iterative ones, why not just program them that way in the first place?
Because iterative syntax doesn't compose, i.e. you can't put things together without losing iteration. For example, say I have this for debugging:

(define-syntax trace (syntax-rules () ((trace op . args) (tracer 'op op . args))))
(define (tracer name op . args)
  (write (cons name args)) (newline)
  (apply op args))


Then this is still iterative:
(define (fib n)
  (let loop ((a 0) (b 1) (n n))
    (if (zero? n)
        b
        (trace loop b (+ a b) (- n 1)))))


Of course that's just the tip of the iceberg, this applies to lots of wrappers, mutually recursive procedures, state machines ( http://www.cs.brown.edu/~sk/Publications/Talks/SwineBeforePerl/ ), parsers...

Once you have tasted the freedom of TCO, everything without it feels like a bondage-and-discipline language.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List