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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:40:37+00:00 2026-06-15T08:40:37+00:00

In the following code: class Array { public: int& operator[] (unsigned i) { if

  • 0

In the following code:

class Array {
   public:
      int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; }
   private:
      int data[100];
};

int main()
{
   Array a;
   a[10] = 42;
   a[12] += a[13];
   ...
}

(Correct me if I’m wrong) The variable a of type Array is on the stack since new was not used to allocate it. The Array class has int data[100], and the operator overload returns reference to particular index in data.

Referring question.

My question is whether int data[100] is on the stack or heap ? I think it shouldn’t be the stack, otherwise how can a reference return like the one above still work.

Thanks.

  • 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-15T08:40:38+00:00Added an answer on June 15, 2026 at 8:40 am

    It’s on the stack, since as you’ve noted a was allocated on the stack.

    The stack is memory just like the heap; you can return a reference to part of it just like memory allocated on the heap. The only difference is in how the memory is managed.

    The only thing you need to be careful of is not to access memory that’s been deallocated; in the case of the stack, this happens at the end of a‘s scope, while heap-allocated data has to be explicitly deleted.

    In the question you refer to, a reference to a variable declared on the stack is returned from a function; in that case, the variable is destroyed when the function exits, which is why that code is wrong. In your case, however, you’re returning a reference to part of data whose lifetime matches that of the Array object; so as long as a has not been destroyed, it’s safe to access it’s data in this manner.

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

Sidebar

Related Questions

In the following code: class SomeClass { vector<int> i; vector<bool> b; public: int& geti()
I have the following code: class Array { public: int aaa; Array():aaa(1){} void print()
Suppose, I have the following code: class Data { private: int *m_arr; int m_size;
I have the following code: class Test { public static void main(String[] args) {
I have the following code: class SomeClass {}; class SomeOtherClass { SomeClass& someObj; public:
I have encountered the following code: class a { public: void * operator new(size_t
Consider the following code: class myarray { int i; public: myarray(int a) : i(a){
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
i have written the following code class Program { static void Main(string[] args) {
I have the following code :- class A : public B { public: _container

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.