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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:27:37+00:00 2026-06-10T05:27:37+00:00

I am trying to play with __attribute__ to allow a function to be essentially

  • 0

I am trying to play with __attribute__ to allow a function to be essentially compiled with different flags from the rest of the code. For example:

#include <iostream>
#include <vector>

void MyNormalFunction();

void MyDebugabbleFunction() __attribute__((optimize(0)));

void MyNormalFunction()
{
  std::cout << "Test" << std::endl;

  std::vector<int> a;

  for(unsigned int i = 0; i < 10; ++i)
  {
    a.push_back(i);
  }
}

void MyDebugabbleFunction()
{
  std::cout << "Test" << std::endl;

  std::vector<int> a;

  for(unsigned int i = 0; i < 10; ++i)
  {
    a.push_back(i);
  }
}

int main()
{
  MyNormalFunction();
  MyDebugabbleFunction();
  return 0;
}

I am building with -g -O2, but I want to be able to sanely debug MyDebugabbleFunction() — so I used the __attribute__((optimize(0))) on its declaration. However, I can’t really tell any difference when stepping through these two functions with a debugger. I would expect the “seemingly erratic” behavior that I usually see when trying to step through optimized code in MyNormalFunction, but the standard “-g”-only debugger behavior in MyDebuggableFunction.

Is it that I have done something wrong with __attribute__? Or that I have used bad demo code (i.e. code that doesn’t get “optimized a lot”) inside the two functions? Or am I misinterpreting what the difference is supposed to be in the debugger?

I am using gcc 4.6.


EDIT based on GManNickG’s suggestion

I used this code instead, and built with -O2 -g:

#include <iostream>
#include <vector>

int MyNormalFunction();

int MyDebugabbleFunction() __attribute__((optimize(0)));

int MyNormalFunction()
{
  int val = 0; // breakpoint here - debugger does NOT stop here
  val = 1;
  val = 2;
  return val;
} // debugger stops here instead

int MyDebugabbleFunction()
{
  int val = 0;  // breakpoint here - debugger stops here and steps through the next 3 lines as if it were built with only -g
  val = 1;
  val = 2;
  return val;
}

int main()
{
  int a = MyNormalFunction();
  std::cout << a << std::endl;

  int b = MyDebugabbleFunction();
  std::cout << b << std::endl;

  return 0;
}
  • 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-10T05:27:38+00:00Added an answer on June 10, 2026 at 5:27 am

    Try a test like this instead:

    int MyNormalFunction()
    {
        int val = 0;
        val = 1;
        val = 2;
    
        // should optimize to return 2
        return val;
    }
    
    int MyDebuggableFunction() __attribute__((optimize(0)));
    {
        int val = 0;
        val = 1;
        val = 2;
    
        // could optimize to return 2, but attribute blocks that
        return val;
    }
    
    int main()
    {
        // we need to actually output the return values,
        // or main itself could be optimized to nothing
        std::cout << MyNormalFunction() << std::endl;
        std::cout << MyDebuggableFunction() << std::endl;
    }
    

    It’ll make it much easier to follow.


    Note that you should start in main, when stepping through, because it most likely will be reduced to:

    int main()
    {
        std::cout << 2 << std::endl;
        std::cout << MyDebuggableFunction() << std::endl;
    }
    

    If you’re up for it, looking at the disassembly makes this task much easier.

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

Sidebar

Related Questions

Consider the following x86 code example: #include <stdlib.h> static int i; static inline __attribute__((always_inline))
I am trying to play video from streaming url. the code is as following
iam trying play animation with xcode,in specefic Time for example after 3 minutes play
I am trying to play a song from my iPhone music library using AVPlayer.
So I'm trying to play a video and the following code has worked for
I am trying to play a video stream from the internet on the iPhone
Trying to play a specific track from an album with the player view. It
im trying to play a video from the raw folder with phonegap. I modified
I'm trying to play songs from an iPod playlist. As far as playlist may
Hi I am trying to play video from my restful wcf service on mobile

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.