StackOverFlow's Avatar

Doesn't quite fit anywhere else? Post here!
11 posts Page 1 of 1 First unread post
GhastlyCat
Deuce
Posts: 7
Joined: Sat Nov 17, 2012 3:12 am


I've been wondering a long time, what does Stack's avatar even mean?
Image
Can someone tell me :roll:
Vyper
Deuce
Posts: 13
Joined: Mon Nov 19, 2012 7:29 pm


I really dont think anyone knows but Stack..
Szuwar
Deuce
Posts: 13
Joined: Sat Nov 10, 2012 1:22 pm


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).
danhezee
Former Admin / Co-founder
Former Admin / Co-founder
Posts: 1710
Joined: Wed Oct 03, 2012 12:09 am


Hey stack do you know what recursion is?
rakiru
Coder
Coder
Posts: 1349
Joined: Sun Nov 11, 2012 12:26 pm


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();
	}
HoboHob
Winter Celebration 2013
Winter Celebration 2013
Posts: 979
Joined: Mon Nov 05, 2012 5:02 pm


lol.
tallyyyyyy
Winter Celebration 2013
Winter Celebration 2013
Posts: 180
Joined: Sat Nov 10, 2012 1:04 am


it's cuz smarts
EssigGurkenFred
Deuced Up
Posts: 111
Joined: Tue Nov 06, 2012 3:15 pm


You mean the guy in that picture? He has created the programmer language C.
Sponge
Deuced Up
Posts: 176
Joined: Mon Nov 05, 2012 5:52 pm


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.
rakiru
Coder
Coder
Posts: 1349
Joined: Sun Nov 11, 2012 12:26 pm


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.
GreaseMonkey
Coder
Coder
Posts: 733
Joined: Tue Oct 30, 2012 11:07 pm


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!
11 posts Page 1 of 1 First unread post
Return to “The Lounge”

Who is online

Users browsing this forum: No registered users and 5 guests