Re: Stack overflow..



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
.



Relevant Pages

  • Re: Microcontroller Project
    ... I'd then suggst you take a look at some more modern compilers perhaps. ... When the stack overwrites the ... registers (memory mapped) all sorts of fun things happen. ... When the stack overflows you don't see any allocation errors. ...
    (sci.electronics.design)
  • Re: Microcontroller Project
    ... I'd then suggst you take a look at some more modern compilers perhaps. ... When the stack overwrites the ... that hard to avoid unless your program is doing something very strange. ... There is no real reason to avoid C on any micro when good compilers ...
    (sci.electronics.design)
  • Re: Statement on Schildt submitted to wikipedia today
    ... However, what Schildt described was not an abstract stack, but a specific ... but not true -- many compilers will ... support the contention that the standard was designed "to protect profits". ... "Sequence points" were designed to protect profits, ...
    (comp.lang.c.moderated)
  • Re: Comparision of C Sharp and C performance
    ... depending on compilers and options. ... MSIL is the older / original term. ... simple stack machines I have found are underrated. ... the frontend and backend directly with SSA. ...
    (comp.lang.c)
  • Re: Passing an array to a function
    ... is it assured for all C compilers that the "function" will always ... variable "myArray" but only the stack copy)? ... void f(int a ); ...
    (comp.lang.c)