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

  • Home
  • SEARCH
  • 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 7818601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:37:47+00:00 2026-06-02T06:37:47+00:00

g++ gives warning array subscript is above array bound… when I compile the following

  • 0

g++ gives warning array subscript is above array bound... when I compile the following code (-Wall -O2)

#include <iostream>
#include <algorithm>
using namespace std;
int a[10];
int n;
int main(){
    sort(a, a+n);

    return 0;
}

but this code compiles without any warning:

#include <iostream>
#include <algorithm>
using namespace std;
int a[100];
int n;
int main(){
    sort(a, a+n);

    return 0;
} 

Why does that happen?

  • 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-02T06:37:49+00:00Added an answer on June 2, 2026 at 6:37 am

    When poking around on the net for issues related to -Warray-bounds in GCC, it seems that problems with it crop up occasionally but those problems usually seem to be tied to very specific code scenarios. The feature seems to rely on the optimizer – this is why the -O2 option is necessary for you to see the problem. (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35903#c9 where it’s mentioned that “the reason this requires -O2 is that -Warray-bounds requires VRP to warn” – VRP is Value Range Propagation). Minute differences in source can easily cause different behavior in the optimizer.

    Anyway, I think the more important thing about these problems with -Warray-bounds is that they seem to also be tied to very specific compiler versions. For example, this bug (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949) reported against 4.4.3 (for all I know, this bug may be what’s causing the warning for you) is reported as working for 4.2.4, 4.5.1, and 4.6.0, while it’s also broken in 4.5.0. The fix for it apparently went into some 4.5.x version of the compiler.

    Anyway, the bottom line: if this is really a problems for you (ie., it’s not just a curiosity), you might want to consider one of the following workarounds/fixes:

    • move to a version of GCC that’s 4.5.1 or better
    • work around the problem with a code change. As mentioned, small changes may prevent the warning from being triggered
    • turn off the warning with -Wno-array-bounds
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when i am analyze my project following code gives me leakage warning. is there
The following code in VS2008 gives me a variable is already defined warning: if
The following code does not give a warning with g++ 4.1.1 and -Wall .
I'm using old php script that depends on session_register to gives warning messages but
when I'm trying to compile my c program it gives me this error warning:
When using the MnSymbol package, pdflatex gives two font warnings: LaTeX Font Warning: Encoding
I’m using g++ with warning level -Wall -Wextra and treating warnings as errors (
Can someone explain the reason/importance of why javascriptlint (not jslint) gives the warning inc_dec_within_stmt
php -v gives this PHP Warning: PHP Startup: memcache: Unable to initialize module Module
Opening an XML file in Internet explorer gives a security warning. IE has a

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.