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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:49:32+00:00 2026-05-26T09:49:32+00:00

I have this (rather useless) code: __declspec(noinline) int foo( char* ptr, int offset )

  • 0

I have this (rather useless) code:

 __declspec(noinline)
 int foo( char* ptr, int offset )
 {
    if( 5 / offset == 3 ) {
      return 1;
    }
    if( ptr != ptr + offset ) {
      return 2;
    }
    return 0;
 }

 int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
 {
    if( foo( 0, 0 ) ) {
       rand();
    }
 }

I compile that with optimizations on and get this disassembly:

    141: __declspec(noinline)
    142: int foo( char* ptr, int offset )
    143: {
    144:   if( 5 / offset == 3 ) {
00401000  push        5  
00401002  pop         eax  
00401003  cdq  
00401004  xor         ecx,ecx  
00401006  idiv        eax,ecx  
00401008  sub         eax,3  
0040100B  neg         eax  
0040100D  sbb         eax,eax  
0040100F  inc         eax  
    145:     return 1;
    146:   }
    147:   if( ptr != ptr + offset ) {
    148:     return 2;
    149:   }
    150:   return 0;
    151: }
00401010  ret  
    152: 
    153: int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
    154: {
    155:    if( foo( 0, 0 ) ) {
00401011  call        foo (401000h)  
00401016  test        eax,eax  
00401018  je          wmain+0Fh (401020h)  
    156:        rand();
0040101A  call        dword ptr [__imp__rand (4020A0h)]  
    157:    }
    158: }
00401020  xor         eax,eax  
00401022  ret

The compiler preserved a foo() function call, but compiled foo() by propagating parameters known at compile time into the function body and optimizing the code. It even emitted

warning C4723: potential divide by 0

Is that expected behavior of Visual C++?

  • 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-26T09:49:32+00:00Added an answer on May 26, 2026 at 9:49 am

    I guess so. You told it not to inline the function, but you didn’t say it couldn’t modify the function based on how it is used. It can see that the function is only ever called as foo(0,0), so why shouldn’t it optimize the function for that?

    try inserting a call to something like foo(1,2) in addition to the (0,0) call and see what happens.

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

Sidebar

Related Questions

I'm rather new to Django and I'm using Django 1.0. I have this: forms.py:
This may be rather noobish but I'm gonna ask anyhow. I have a class
This has been a rather problematic issue on numerous occasions. We have alot of
Is there some way when sending this message to specify that I rather have
I'm rather new to WPF, so maybe this is a simple question. I have
This may be a silly question, but right now I have a rather large
I will try to keep this as simple as possible. I have a rather
I have this code in jQuery, that I want to reimplement with the prototype
I have read this manual: http://git-scm.com/docs/gitignore As I am working with gitosis, I rather
I have this rather simple SQL query, but it takes almost a minute to

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.