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

The Archive Base Latest Questions

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

While working on a C++ project with Visual C++ Express 2010 I’ve found an

  • 0

While working on a C++ project with Visual C++ Express 2010 I’ve found an interesting issue that I’d like to understand. The problem is that the result of my program is different if I compile in Debug or Release modes. I made a small program to reproduce it:

#include <stdio.h>


int firstarg(int i)
{
    printf("First argument called with i = %d\n", i);

    return i;
}

int secondarg(int i)
{
    printf("Second argument called with i = %d\n", i);

    return i;
}

void function(int i, int j)
{
    printf("Function called with %d, %d\n", i,j);
}

int main(int argc, char* argv[])
{

    // Line with the problem!
    for (int i = 0; i < 5; ) function(firstarg(i), secondarg(i++)); 

    return 0;
}

// Result on RELEASE:
Second argument called with i = 0
First argument called with i = 0
Function called with 0, 0
Second argument called with i = 1
First argument called with i = 1
Function called with 1, 1
Second argument called with i = 2
First argument called with i = 2
Function called with 2, 2
Second argument called with i = 3
First argument called with i = 3
Function called with 3, 3
Second argument called with i = 4
First argument called with i = 4
Function called with 4, 4

// Result on DEBUG
Second argument called with i = 0
First argument called with i = 1
Function called with 1, 0
Second argument called with i = 1
First argument called with i = 2
Function called with 2, 1
Second argument called with i = 2
First argument called with i = 3
Function called with 3, 2
Second argument called with i = 3
First argument called with i = 4
Function called with 4, 3
Second argument called with i = 4
First argument called with i = 5
Function called with 5, 4

As you see, in both cases the second argument is evaluated before the first one (that one I expected, if arguments are processed in some kind of LIFO stack); but in release the increment of the variable i is “optimized away” and delayed until the next iteration of the loop. That was unexpected, and I’d really would like to understand what’s going on.

Of course, I can easily “fix” my code by changing the loop to

    for (int i = 0; i < 5; ++i) function(firstarg(i+1), secondarg(i)); 

which will give always the same result regardless of compilation parameters. But, still, what I would really like to understand the reasons behind this optimization of increments.

PS. By the way, I could not reproduce this problem with gcc under linux (Debug with -O0 flag, Release with -O3).

  • 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-10T18:37:46+00:00Added an answer on June 10, 2026 at 6:37 pm

    You are misunderstanding the results. The increment is not “optimized away” or delayed until the next iteration of the loop. You just have no way to see what the value of i is before the next iteration. Try this:

    for (int i = 0; i < 5; )
    {
       function(firstarg(i), secondarg(i++)); 
       printf("At end of iteration, i = %d\n");
    }
    

    And you’ll see that it’s not delayed at all.

    Your code is UB because you have an access of a variable and a modification of that same variable without an intervening sequence point. While you could get really crazy results, in fact you get both of the “expected” results, depending on your optimizations. The order in which function parameters are evaluated is unspecified.

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

Sidebar

Related Questions

While working on my final project for my AS/400 Course, I encountered this problem
while working on a school project i ran into a problem using javascript to
I am working in C#, winform and Mysql. (Visual Studio 2010). In my project
I have a Visual Studio 2010 C++ project that links statically to tinyxmlSTL 2.5.5
I've just come across the weirdest behavior in Visual Studio 2010 while working with
Normally, many files in Visual Studio 2010 are opened in many tabs, while massively working on
I have a Visual Studio 2010 solution that contains 3 projects: a dll project,
While working a project tonight, I ended up using one .js resource file for
While working on an Xcode project i keep getting the spinning wheel while switching
While working on an AppEngine connected Android project, I occasionally have problems with RequestFactory

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.