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 8437031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:19:36+00:00 2026-06-10T07:19:36+00:00

I’m getting strange behavior bounds checking a member std::array with mingw (gcc 4.7.0) with

  • 0

I’m getting strange behavior bounds checking a member std::array with mingw (gcc 4.7.0) with the following code

#include <iostream>
#include <array>

class testClass
{
    std::array<int, 2> testArray;

    public:
        testClass();
        void func() const;

};

testClass::testClass() : testArray({{1, 2}})
{
}

void testClass::func() const
{
    for (int i = 0; i < 2; ++i)
        std::cout << testArray.at(i) << '\n' << testArray[i] << '\n';       
}


int main()
{
    testClass test;
    test.func();
}

Output is

0
1
0
2

The error seems to be related to optimization, as it only crops up when compiled with -O, I tried the individual flags enabled by -O but couldn’t narrow it down any further. Making the function non-const also fixes the issue. Could this be a bug or am I missing something?

*edit

Narrowed it down, looks like a bug in the const version of .at()

#include <iostream>
#include <array>

int main()
{
    std::array<int, 2> const testArray = {1, 2};

    for (int i = 0; i < 2; ++i)
        std::cout << testArray.at(i) << '\n' << testArray[i] << '\n';       
}

Same output as above compiled with -std=c++11 -O using mingw 4.7.0 on Windows Xp sp3 and Windows 7 sp1.

*edit 2

Same output again

#include <iostream>
#include <array>

int main()
{
    typedef std::array<int, 2> Tarray;
    Tarray test = {1, 2};

    for (int i = 0; i < 2; ++i)
        std::cout << const_cast<Tarray const*>(&test)->at(i) << '\n' << test.at(i) << '\n';     
}
  • 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-10T07:19:38+00:00Added an answer on June 10, 2026 at 7:19 am

    This is part of array header

    #ifdef __EXCEPTIONS
      constexpr const_reference
      at(size_type __n) const
      {
    return __n < _Nm ? 
           _M_instance[__n] : throw out_of_range(__N("array::at"));
      }
    #else
      const_reference
      at(size_type __n) const
      {
    if (__n >= _Nm)
      std::__throw_out_of_range(__N("array::at"));
    return _M_instance[__n];
      }
    #endif
    

    Undef __EXCEPTIONS in main file(or change #ifdef to #ifndef in array) leads to correct output. I don’t know, this is right solution or not, but it works.

    UPD: I change the code in my array’s header to

    #ifdef __EXCEPTIONS
      constexpr const_reference
      at(size_type __n) const
      {
    return __n < _Nm ? 
           _M_instance[__n] : (throw out_of_range(__N("array::at"))),
                              _M_instance[__n];
       /*return __n < _Nm ? 
                _M_instance[__n] : throw out_of_range(__N("array::at"));*/
      }
    #else
      const_reference
      at(size_type __n) const
      {
    if (__n >= _Nm)
      std::__throw_out_of_range(__N("array::at"));
    return _M_instance[__n];
      }
    #endif
    

    Now everything is working correctly

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.