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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:41:19+00:00 2026-05-25T15:41:19+00:00

I wrote a small profiling class with an accompanying guard class to use like

  • 0

I wrote a small profiling class with an accompanying guard class to use like this:

{
    ProfileGuard pg("SampleName");

    // Code to profile
    ...
}

But then I noticed that sometimes gcc optimizes the code so that pg is destructed instantly. Apparently it detects that pg and the other code will not influence eachother. What’s the best way to prevent gcc from doing this?

Update:
Since there is some serious disbelief concerning my question I will check my code again. I based my question on:

3.7.3 3.

If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not
be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to
be unused, except that a class object or its copy/move may be eliminated as specified in 12.8.

Is doing a print a “side effect”?

  • 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-25T15:41:20+00:00Added an answer on May 25, 2026 at 3:41 pm

    Apparently it detects that pg and the other code will not influence eachother.

    How, since that’s patently untrue? They do influence each other (e.g. through output order).

    GCC doesn’t do this. Perhaps you accidentally used an anonymous (and hence temporary) object?

    ProfileGuard("SampleName");
    

    Now, this will be destructed immediately after executing the expression.

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

Sidebar

Related Questions

I wrote small service class and I want implement something like that : service
I wrote a small PHP application that I'd like to distribute. I'm looking for
We wrote a small Windows class library that implements extension methods for some standard
I wrote a small sample of code in C# to capture selected text from
I wrote some small apps using .NET 3.5 but now I am stuck with
I recently wrote a small tool to generate a class for each tier I
I wrote a small program using a custom indexOf function but wanted to dismiss
I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The
I wrote a small Facebook Ap[p in PHP and would like to be able
I wrote a small application to use as a sandbox for testing ideas that

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.