Sometimes I use the following code to avoid stack overflow when taking part in coding competition.
int main()
{
static const int _STACK_SIZE = MAXN*10;
static int _STACK[_STACK_SIZE*2], _ESP;
__asm__ __volatile__
(
"movl %%esp, %0
"
"movl %1, %%esp
":
"=g"(_ESP):
"g"(_STACK + _STACK_SIZE):
);
// Do Something..
__asm__ __volatile__
(
"movl %0, %%esp
":
:
"g"(_ESP):
);
}
As far as I know, this asm code backups %esp
and moves the stack to _STACK[]
.
My Question: Why this code cause SIGSEGV
on a x86-64 Linux Server(It runs well on my own x86 Linux)? And how to fix it?
I guess, maybe it's because %esp
is a 64-bit pointer??
I tried to delete the __asm__ __volatile__("movl %0, %%esp
": : "g"(_ESP):);
and it seems runs well?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…