Page 1 of 1

StackOverFlow's Avatar

Posted: Tue Nov 20, 2012 6:14 pm
by GhastlyCat
I've been wondering a long time, what does Stack's avatar even mean?
Image
Can someone tell me :roll:

Re: StackOverFlow's Avatar

Posted: Tue Nov 20, 2012 6:15 pm
by Vyper
I really dont think anyone knows but Stack..

Re: StackOverFlow's Avatar

Posted: Tue Nov 20, 2012 6:24 pm
by Szuwar
Recursive function is a function that call itself like so:
Code: Select all
int CalcMass(int x){ return CalcMass(x+1); }
Every call cause pointer to be added to a stack (kind of memory) which is of course limited.

So his avatar says that her weight is so huge that calculating it using such method would cause whole stack to be used and still not having any results (more or less).

Re: StackOverFlow's Avatar

Posted: Tue Nov 20, 2012 8:21 pm
by danhezee
Hey stack do you know what recursion is?

Re: StackOverFlow's Avatar

Posted: Tue Nov 20, 2012 11:41 pm
by rakiru
On this topic, a snippet from one of my projects:
Code: Select all
	/**
	 * Infraction is almighty.
	 * Infraction cannot be contained within a single object.
	 * If you attempt to do so, Infraction shall burst
	 * forth from its prison and destroy your world.
	 */
	public Infraction() {
		new Infraction();
	}

Re: StackOverFlow's Avatar

Posted: Wed Nov 21, 2012 3:11 am
by HoboHob
lol.

Re: StackOverFlow's Avatar

Posted: Wed Nov 21, 2012 4:23 am
by tallyyyyyy
it's cuz smarts

Re: StackOverFlow's Avatar

Posted: Wed Nov 21, 2012 1:58 pm
by EssigGurkenFred
You mean the guy in that picture? He has created the programmer language C.

Re: StackOverFlow's Avatar

Posted: Thu Nov 22, 2012 2:13 am
by Sponge
Szuwar wrote:
So his avatar says that her weight is so huge that calculating it using such method would cause whole stack to be used and still not having any results (more or less).
Correct me if I'm wrong, a stack overflow is when a stack is taking up way too much memory. For example:
Code: Select all
int ovrflow() {
return ovrflow();
}
This will create an integer variable called "ovrflow". It's value is taken from the variable "ovrflow", which if you didn't notice, is itself. What will happen is that it will create ovrflow, then look to ovrflow for answers on what integer it should represent. ovrflow will return to look for ovrflow for answers. Then it will look to ovrflow for answers. ovrflow will tell it to look to ovrflow for answers. Now just repeat that for a while and you got a stack overflow.

What the picture means is that the function would have to be so large to calculate your mother's weight, that it would cause so many big numbers that it will take up so much memory that it will go excessively deep into the function, and return a stack overflow.

Re: StackOverFlow's Avatar

Posted: Thu Nov 22, 2012 7:42 pm
by rakiru
Sponge wrote:
Szuwar wrote:
So his avatar says that her weight is so huge that calculating it using such method would cause whole stack to be used and still not having any results (more or less).
Correct me if I'm wrong, a stack overflow is when a stack is taking up way too much memory. For example:
Code: Select all
int ovrflow() {
return ovrflow();
}
This will create an integer variable called "ovrflow". It's value is taken from the variable "ovrflow", which if you didn't notice, is itself. What will happen is that it will create ovrflow, then look to ovrflow for answers on what integer it should represent. ovrflow will return to look for ovrflow for answers. Then it will look to ovrflow for answers. ovrflow will tell it to look to ovrflow for answers. Now just repeat that for a while and you got a stack overflow.

What the picture means is that the function would have to be so large to calculate your mother's weight, that it would cause so many big numbers that it will take up so much memory that it will go excessively deep into the function, and return a stack overflow.
It's the call stack, specifically.

Re: StackOverFlow's Avatar

Posted: Sun Nov 25, 2012 10:27 am
by GreaseMonkey
Sponge wrote:
Correct me if I'm wrong, a stack overflow is when a stack is taking up way too much memory. For example:
Code: Select all
int ovrflow() {
return ovrflow();
}
You're right, but that code is likely to be optimised into a tailcall, which will not chew your stack.

You're expecting this:
Code: Select all
ovrflow:
  call ovrflow
  ret
But you'll probably just get this instead:
Code: Select all
ovrflow:
  jmp ovrflow
A better example would be the good ol' factorial function:
Code: Select all
int fac(int n)
{
  return (n <= 1
    ? 1
    : n * fac(n-1));
}
(Yeah, I like that form in some cases, it's basically (A?B:C) = (if A then B else C))

That second call has to get the result from fac(n-1) and multiply it by "n". As there is still stuff to do, this will not be optimised to a tail call.
Code: Select all
fac:
  mov eax,[esp+4]
  cmp eax,1
  jg .cont
  mov eax,1
  ret 4
.cont:
  mov ecx, eax
  dec ecx
  push ecx
  call fac
  mul eax, eax, ecx
  ret 4
(yeah yeah assuming 32-bit x86, also we're using callee clear for this)

This can then cause a stack overflow if you're trying to calculate, say, 2,000,000,000 factorial (chewing close to 16GB of RAM in a 4GB virtual address space).

Apparently this works a bit more nicely, though:
Code: Select all
int fac_part(int n, int s)
{
  return (n <= 1
    ? s
    : fac_part(n-1, s*n));
}

int fac(int n)
{
  return fac_part(n, 1);
}
which can be optimised to
Code: Select all
fac_part:
  mov eax,[esp+8] ; eax <- s
  mov ecx,[esp+4] ; ecx <- n
  cmp ecx, 1
  jg .cont
  ret 8
.cont:
  mul eax, eax, ecx
  dec ecx
  mov [esp+8],eax
  mov [esp+4],ecx
  jmp fac_part

fac:
  mov eax,[esp+4]
  push dword 1
  push eax
  call fac_part
  ret 4
Of course, it would probably optimise further by jumping to "cmp eax, 1" rather than the start of fac_part, and dropping the two "write to the stack" lines before "jmp fac_part", but you MIGHT understand where I'm coming from (no pun intended).

But yeah, that's one way to avoid a stack overflow!