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 9:20

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?
It could be alloced in the stack or in a register. You can even try to force the compiler to use a register, however you can always get the reference of a variable with & in C, so if you do, it would be stack allocated, however functions will always return a value (which could be a pointer if you wish). Try disassembling some functions if it's still not clear or reading a good compiler book.

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?
While tailcalls + TCO means you can emulate iteration with them, it doesn't mean you have to. On the other hand, there are many legit uses of using tailcalls for things like stack machines, mutually recursive functions and things like networks of closures which can get compiled down to lovely jmps/gotos without wasting stack space. Just because in some languages like Scheme, they think that LAMBDA and TCO lead to the ultimate iteration construct, doesn't mean that's its only usage, or that you absolutely have to use tail-recursion to implement iteration. For example in another Lisp, CL, we have plenty of iteration constructs, and if those aren't enough, you can very easily define more using macros( you can do thte same in Scheme, though I don't think they ever had real macros properly standardized, though most implementations support them, and they were defined in the R4RS? appendix as well).

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