Re: Stack overflow..
- From: Tauno Voipio <tauno.voipio@xxxxxxxxxxxxx>
- Date: Sun, 31 May 2009 19:29:24 GMT
praveenraj1987@xxxxxxxxx wrote:
On May 31, 1:14 am, Nate Eldredge <n...@xxxxxxxxxx> wrote:praveenraj1...@xxxxxxxxx writes:does stack issue a segmentation fault signal in unix/linux??It does on all the machines I tried. Why not try it on yours and see?
#include<iostream>
int main()
{
int b=50;
int a[2]={10,100};
a[-1]=200;
a[-2]=250;
a[-1000]=300;
std::cout<<"b = "<<b<<" "<<&b;
std::cout
<<std::endl<<"a0 = "<<a[0]<<" "<<&a[0]
<<std::endl<<"a1 = "<<a[1]<<" "<<&a[1]
<<std::endl<<"a2 = "<<a[2]<<" "<<&a[2]
<<std::endl<<"a3 = "<<a[3]<<" "<<&a[3]
/*b=a[3] beyond that stack starts writing on memory that is beyond its
stack frame..
return address and other things.. do not to write above that..
even if u overwrite it and get segmentation fault..
that is because u have overwritten the return address and jump to that
memory after functin ends, gives u SEG FAULT..
or your code is trying to access memory that is not on stack and is on
different PAGE.. as by giving a very high aray bound..
which makes it point to past the stack area....
so it is valid to write any where on STACK..*/
<<std::endl<<"a-1 = "<<a[-1]<<" "<<&a[-1]
<<std::endl<<"a-2 = "<<a[-2]<<" "<<&a[-2]
<<std::endl<<"a-3 = "<<a[-1000]<<" "<<&a[-1000];
}
compiled withs gcc...
stack is growing downwards so i have used -ve subscript..
You are messing with the stack area outside the
allocated array boundaries.
The compiled behaviour is *undefined* in this case.
There are no guarantees whatsoever which data you
clobbered. Please understand that the compilers will
do some operations with functions in compiler support
library, and they may use the stack are written to.
--
Tauno Voipio
tauno voipio (at) iki fi
.
- Follow-Ups:
- Re: Stack overflow..
- From: praveenraj1987
- Re: Stack overflow..
- References:
- Stack overflow..
- From: praveenraj1987
- Re: Stack overflow..
- From: Nate Eldredge
- Re: Stack overflow..
- From: praveenraj1987
- Stack overflow..
- Prev by Date: Re: Stack overflow..
- Next by Date: Re: Stack overflow..
- Previous by thread: Re: Stack overflow..
- Next by thread: Re: Stack overflow..
- Index(es):
Relevant Pages
|