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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:48:40+00:00 2026-06-11T14:48:40+00:00

Consider the following simple code. struct test { test(int n):numelements(n){ arr = new int[numelements]

  • 0

Consider the following simple code.

    struct test
    {
      test(int n):numelements(n){ arr = new int[numelements] }

      int numelements;
      int* arr;

      ~test()
       {
          delete[]  arr;
       }
    }

   void foo_ref(test& x,n)
   {
       // set first n elements of the array x.arr to random numbers
   }

  void foo_ptr(test* x,n)
   {
       // set first n elements of the array x->arr to random numbers
   }

    int main(void)
    {
     test mystruct(1000000);
     foo_ref(mystruct,20);
     // foo_ptr(&mystruct,20); 


     return 0;
   }

In the above code, foo_ref and foo_ptr perform exactly the same operations on
the object it refers to viz. mystruct. However foo_ref passes the object by reference
and foo_ptr by means of pointers.

In both cases where is the destructor of the object invoked? I know the standard answer for this question always is “when the scope of an object ends”

But consider the case of passing by reference. Within the body of foo_ref
mystruct has the scope of that function. So won’t the destructor of the object be invoked at the end of the function foo_ref?

Maybe my confusion has to do with the understanding of the scope of something.
Please let me know where I am erring in my reasoning.

  • 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-11T14:48:41+00:00Added an answer on June 11, 2026 at 2:48 pm

    I don’t see any const references around here, therefore the answer is “no different in object lifetime between references and pointers”.

    const references bound to temporary expressions have a special rule extending the lifetime of the temporary object, but that doesn’t apply to your code.

    Your confusion probably stems from a statement made in a very popular FAQ, it goes something along the lines of “a reference IS the referred-to object”. As you can see, this is not true. A reference is separate from the object it refers to, each has its own lifetime (and it’s very rare to see this done correctly, but it’s even legal for the lifetime of the reference to extend longer than the lifetime of the object)

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

Sidebar

Related Questions

Consider the following simple header, demo.h: #define PERSIST struct Serialised { int someTransientValue ;
Please consider the following simple use case: public class Foo { public virtual int
Consider my first attempt, a simple type in F# like the following: type Test()
consider the following class and struct public class Entity { public IdType Id {get;set;}
Consider the following pretty simple C++ code: #include <algorithm> #include <iostream> using namespace std;
Consider the following code: #include <stdlib.h> #ifndef TRY #define TRY struct #endif TRY testme
Consider the following code: struct TrainingExample { array<double, N> input; array<double, M> output; };
Consider the following simple piece of code: %hash = ('a'=>1,'b'=>2); print $hash{'b'}; print \n,(\%hash)->{'b'};
Consider the following simple code to create a typesafe equals. This first section allows
Consider the following simple python code >>> L = range(3) >>> L [0, 1,

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.