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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:26:24+00:00 2026-06-13T19:26:24+00:00

When I run this code I get a bad_alloc error, Am I accessing the

  • 0

When I run this code I get a bad_alloc error, Am I accessing the subscript operator wrong or something ? To me it makes sense that I go to the appropriate object, it gets returned and then the subscript operator should kick in ? By the way I want to use arrays and not vectors 🙂

class A{
    public:
    A(int size)
    {
        array = new int[size];
    }
    int& operator[](const int &i)
    {
        return array[i]
    }
    private:
       int * array;
};

int main()  {
   A ** a = new A*[10];
   for(int i = 0; i < 10; i++)  {
      a[i] = new A(10);
      for(int l = 0; l < 10; l++)   {
         cout << a[i][l] << endl;
      }
   }
}

Thanks in advance

  • 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-13T19:26:25+00:00Added an answer on June 13, 2026 at 7:26 pm

    You need to dereference the pointer before you can call operator[]

    cout << (*(a[i]))[l] << endl;
    

    Here’s what needs to happen, step by step:

    A* pA = a[i];
    A& rA = *pA;
    int& val = rA[l];
    cout << val;
    

    Currently this happens:

    A* pA = a[i];
    A& ra = *(pA + l); // pA is indexed as an array of As - this is wrong
    cout << ra; // invalid memory
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I run this code on IE8 or lower, I get this error: Object
I have this code and I couldn't run it because i get this error:
I get this error when I run the code below. I have normally used
I get this error: Image When i try to run this code: strComputer =
When I run this C code: // Get rid of the CRT and stuff;
When I try to run this code, I get incorrect maximums and minimums. Could
When I run this code to call the Google API, all I get is
I run this piece of code on Visual C++ 2010 char c[10]; cin.get(&c[0],5); cin.get(&c[2],4);
This Java code compiles fine, but when I try to run it I get:
When I run this code i get 2 numbers (which is good) but the

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.