Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:42:47+00:00 2026-06-18T04:42:47+00:00

In this code below I try to access the ‘-1’th element of an array,

  • 0

In this code below I try to access the ‘-1’th element of an array, I don’t get any runtime error.

#include <stdio.h>

int A[10] = {0};

int main(){

    A[-1] += 12;

    printf("%d",A[-1]);

    return 0;
}

When I run the code, it outputs 12 that means it is adding 12 to the non-existent A[-1]. Till today whenever I had tried to access an out-of-bounds element, I had got a runtime-error. I had never tried it on a simple code before.

Can anyone explain why does my code run successfully?

I ran it on my computer and also on ideone, in both the cases it ran successfully.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:42:48+00:00Added an answer on June 18, 2026 at 4:42 am

    You see, when you allocate a variable like this, it lands on the stack. Stack holds small packages of information about local variables in each function you call, to say it in simple words. The runtime is able to check, whether you exceed the bounds of allocated stack, but not if you write some data in the invalid place on the stack. The stack may look like the following:

    [4 bytes – some ptr][4 bytes – A’s first element][4 bytes – A’s second element] …

    When you try to assign to -1th element of an array, you actually attempt to read four bytes preceding the array (four bytes, because it’s an int array). You overwrite some data held on stack – but that’s still in valid process’s memory, so there are no complaints from the system.

    Try running this code in release mode in Visual Studio:

    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
        // NEVER DO IT ON PURPOSE!
        int i = 0;
        int A[5];
    
        A[-1] = 42;
        printf("%d\n", i);
    
        getchar();
        return 0;
    }
    

    Edit: in response to comments.

    I missed the fact, that A is global. It won’t be held in stack, but instead (mostly probably) in .data segment of the binary module, however the rest of explanation stands: A[-1] is still within process’s memory, so assignment won’t raise AV. However, such assignment will overwrite something, that is before A (possibly a pointer or other part of the binary module) resulting in undefined behavior.

    Note, that my example may work and may not, depending on compiler (or compiler mode). For example, in debug mode the program returns 0 – I guess, that memory manager inserts some sentry data between stack frames to catch errors like buffer over/underrun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code below allows me to find the word error in all my files
I got this code for my sharepoint, but I get a Access is Denied,
OK, so this code below sometimes crashes on the line below //bug!! The error
for some reason I get this error below when trying to use multiple require()
Possible Duplicate: Objective C alloc/release error When I try the code below it crashes
I try to access information from Twitter and I followed this link: http://code.google.com/p/sociallib/wiki/SocialLibGuide I
I trying this code but i always show the error Permission Denied.and i try
This code below is for executing ls -l | wc -l. In the code,
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
This code below doesn't work correctly since my MFC program is in unicode circumstance.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.