Why Does This Not Crash?

Discussion in 'C' started by it career, Apr 8, 2007.

  1. it career

    it career New Member

    Joined:
    Apr 8, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web Designing
    Home Page:
    http://ijug.net/
    Hello C/C++ experts,

    I write the following code
    Code:
    int a[10];
    int b[10];
    
    //initiliaze all element of array a and b to 0.
    a[11]=255;
    
    Neither the program crashes nor the value of b[0] becomes 255
    What could be the reason?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The language doesn't define how those two are related, merely that each is contiguous and that element n+1 is one element higher in memory than element n. If you have a common implementation, inside a function, and that function uses the stack, which it would be stupid to count on, b[11] will write into a[0].

    I would suggest that if you want to play with such stuff, get into machine language. You'll find lots of undefined behavior in C/C++ accidentally, no need to go looking for a way to turn a cotton purse into sow's excrement; nor will I, personally, teach you how to write exploits.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice