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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:06:55+00:00 2026-05-27T13:06:55+00:00

I have seen similar questions (ex here and here ) but never seen a

  • 0

I have seen similar questions (ex here and here) but never seen a definite answer. Suppose we have following code:

..
I:= 1;
I:= 2;
..

Is Delphi compiler guaranties that the first assignment will never be eliminated by optimizer, and implemented by writing the data to the memory address of I variable, not just to some processor register, if I is global or heap-based (ex. field of an object) variable?

  • 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-27T13:06:55+00:00Added an answer on May 27, 2026 at 1:06 pm

    With optimizations enabled, the code above will, at least in some situations, be optimised to remove the first assignment. So this is probably not a very good example!

    That this is so can be seen with this minimal example:

    program OptimiseMe;
    {$APPTYPE CONSOLE}
    {$O+}//switch optimisations on
    var
      I: Integer;
    begin
      I := 1;
      I := 2;
      if I>0 then
        ;
    end.
    

    However, I’ve never encountered problems with the compiler optimising global or heap based variables into registers. It always seems to write it back to the memory. But I’m sure you won’t find anything in the documentation guaranteeing any of that.

    However, this is not the same as MSVC volatile since the cache is in the way and no memory barriers or fences are used. Even when the Delphi compiler emits instructions to write the value to memory, it will first of all go into the cache of the processor on which the code is running. Code executing on other processors will not be able to see that write until the caches have been synchronized.

    If you want to share such a variable between threads, probably the easiest approach is to use the InterlockedXXX functions to enforce the necessary barriers and ensure that all threads have a consistent view of the variable.

    Note: when you talk about volatile, you need to be careful as there are lots of different definitions. The meaning defined in the C and C++ standards is one. The MSVC compiler has a standards compliant but stronger definition. And it’s different again in Java and C#. There’s a lot of complexity here and the InterlockedXXX functions are a great way to hide that complexity.

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

Sidebar

Related Questions

I have seen similar questions on here but haven't found an answer. I'm taking
I have seen similar questions here but I just can't understand them. I am
I have been around and around with this. Have seen similar questions here but
I have seen very similar if not same questions on here but my trials
I have seen similar questions here but , I am not beign able solve
Okay I have seen some very similar questions here but none seem to be
I have seen some similar questions here on stack-overflow, but in my application i
I have seen lots of similar questions here but nothing that quite fits my
I have seen some similar questions here, but the answers dont solve my problem.
I've seen several questions here dealing with similar problems but none of them have

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.