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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:57:33+00:00 2026-05-19T12:57:33+00:00

While doing an assignment for a course and learning C++ while doing it, I

  • 0

While doing an assignment for a course and learning C++ while doing it, I was reading up on when to use stack allocation and dynamic allocation. I’m aware that in a lot of cases it is easier and better to use stack allocation. But there is a simple situation I’m puzzeled about.

Lets say you have a for loop:

for(int i = 0; i < 10; i++)
{
   MyObject obj(file);
   obj.doSomething();
}

Now the problem is that if the Object contains state, it keeps it state (stays the same object) while iterating through the iterations from 1 til 10. Maybe coming from a Java/C# background gets me on the wrong path. But I only see two ways of solving this:

  1. Using dynamic memory.
  2. Not giving file to the constructor but instead to the method doSomething(file) but this isn’t very nice if you have more than one method manipulating the file object e.g. doSomethingElse(file).

So what do you guys do in such a situation, or do you never get yourself in such a situation at all?

Update:
Turns out i misunderstood and it is working as expected. Check the awnsers below! Thanks everyone

  • 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-05-19T12:57:34+00:00Added an answer on May 19, 2026 at 12:57 pm

    In the code you have posted, obj does not maintain any state between iterations.

    for(int i = 0; i < 10; i++) 
    {    
      MyObject obj(file); //obj enters scope, constructor is called with 'file'
      obj.doSomething(); 
    }  //obj loses scope, destructor is called
    

    In that example, obj is a different object every time. It may be using the same ‘stack’ memory location as the previous object, but the class destructor and constructor are called between iterations.

    If you wish to have the object only be constructed once and used repeatedly, construct before the loop.

    function(file)
    {
        MyObject obj(file); //obj enters scope, constructor is called with 'file'
    
        for(int i = 0; i < 10; i++) 
        {    
          obj.doSomething(); //Same object used every iteration
        }  
    
    }  //obj loses scope, destructor is called
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to programming and while doing a lot of reading this
I'm doing a basic homework assignment which looks like this: While input <> -1
While doing normal day-to-day Android development, is it safe to assume that hook methods
I am doing a homework assignment that reads in a book. First, a line
While doing some reading, I came across the terms Intermediate Language and 3AC. IL,
How does PHP handle client connection while doing sleeps? Or put another way ...
I've used strpbrk() occasionally while doing low-level string work in C, but I've never
I've just installed the 3.5 .NET Framework and while doing some cleaning I've notice
A long while back I transitioned to doing all my web application development in
A while ago I was trying to figure out a way of doing this

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.