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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:14:58+00:00 2026-05-11T14:14:58+00:00

This is not academic code or a hypothetical quesiton. The original problem was converting

  • 0

This is not academic code or a hypothetical quesiton. The original problem was converting code from HP11 to HP1123 Itanium. Basically it boils down to a compile error on HP1123 Itanium. It has me really scratching my head when reproducing it on Windows for study. I have stripped all but the most basic aspects… You may have to press control D to exit a console window if you run it as is:

#include 'stdafx.h' #include <iostream> using namespace std;  int _tmain(int argc, _TCHAR* argv[]) {     char blah[6];     const int IAMCONST = 3;     int *pTOCONST;     pTOCONST = (int *)  &IAMCONST;     (*pTOCONST) = 7;     printf('IAMCONST %d \n',IAMCONST);     printf('WHATISPOINTEDAT %d \n',(*pTOCONST));     printf('Address of IAMCONST %x   pTOCONST %x\n',&IAMCONST, (pTOCONST));     cin >> blah;         return 0; } 

Here is the output

IAMCONST 3 WHATISPOINTEDAT 7 Address of IAMCONST 35f9f0   pTOCONST 35f9f0 

All I can say is what the heck? Is it undefined to do this? It is the most counterintuitive thing I have seen for such a simple example.

Update:

Indeed after searching for a while the Menu Debug >> Windows >> Disassembly had exactly the optimization that was described below.

    printf('IAMCONST %d \n',IAMCONST); 0024360E  mov         esi,esp  00243610  push        3     00243612  push        offset string 'IAMCONST %d \n' (2458D0h)  00243617  call        dword ptr [__imp__printf (248338h)]  0024361D  add         esp,8  00243620  cmp         esi,esp  00243622  call        @ILT+325(__RTC_CheckEsp) (24114Ah)  

Thank you all!

  • 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. 2026-05-11T14:14:59+00:00Added an answer on May 11, 2026 at 2:14 pm

    Looks like the compiler is optimizing

    printf('IAMCONST %d \n',IAMCONST); 

    into

    printf('IAMCONST %d \n',3); 

    since you said that IAMCONST is a const int.

    But since you’re taking the address of IAMCONST, it has to actually be located on the stack somewhere, and the constness can’t be enforced, so the memory at that location (*pTOCONST) is mutable after all.

    In short: you casted away the constness, don’t do that. Poor, defenseless C…

    Addendum

    Using GCC for x86, with -O0 (no optimizations), the generated assembly

    main:     leal    4(%esp), %ecx     andl    $-16, %esp     pushl   -4(%ecx)     pushl   %ebp     movl    %esp, %ebp     pushl   %ecx     subl    $36, %esp     movl    $3, -12(%ebp)     leal    -12(%ebp), %eax     movl    %eax, -8(%ebp)     movl    -8(%ebp), %eax     movl    $7, (%eax)     movl    -12(%ebp), %eax     movl    %eax, 4(%esp)     movl    $.LC0, (%esp)     call    printf     movl    -8(%ebp), %eax     movl    (%eax), %eax     movl    %eax, 4(%esp)     movl    $.LC1, (%esp)     call    printf 

    copies from *(bp-12) on the stack to printf‘s arguments. However, using -O1 (as well as -Os, -O2, -O3, and other optimization levels),

    main:     leal    4(%esp), %ecx     andl    $-16, %esp     pushl   -4(%ecx)     pushl   %ebp     movl    %esp, %ebp     pushl   %ecx     subl    $20, %esp     movl    $3, 4(%esp)     movl    $.LC0, (%esp)     call    printf     movl    $7, 4(%esp)     movl    $.LC1, (%esp)     call    printf 

    you can clearly see that the constant 3 is used instead.

    If you are using Visual Studio’s CL.EXE, /Od disables optimization. This varies from compiler to compiler.

    Be warned that the C specification allows the C compiler to assume that the target of any int * pointer never overlaps the memory location of a const int, so you really shouldn’t be doing this at all if you want predictable behavior.

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is actually quite easy using VSTO (Visual Studio Tools… May 11, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer No. VMWare and the rest actually virtualize the hardware of… May 11, 2026 at 6:44 pm
  • Editorial Team
    Editorial Team added an answer Finally! I (partially) solved it: It looks like the CDockablePane… May 11, 2026 at 6:44 pm

Related Questions

Is Software Testing really given its importance at the academic level? I believe there
I'm looking for a profiler to use with native C++. It certainly does not
I've been tinkering with small functions on my own time, trying to find ways
I have created a small Java application to automatically test some expressions for a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.