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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:03:02+00:00 2026-06-01T01:03:02+00:00

I’ve been thinking on how the memory would be handled in some specific case

  • 0

I’ve been thinking on how the memory would be handled in some specific case which i’ll explain (C# 4.0).

To begin with, i’ve read Eric Lippert answer in order to know what will be invoked whenever
I use new on a variable of type struct

And now, I wonder if the compiler “produces” intelligent code in specific use of the new keyword

To be precise, what will it do if the new is set on a return instruction or even on operator = ?

Assume we have a struct Coord defined as follow:

struct Coord {
    int x, y;
    public Coord(int xx, int yy) {
        x = xx;
        y = yy;
    }
}

I’ll use this struct to store position and velocity in classes (suppose we call them Entity) that have geometric behaviors

Entity is defined as follow:

class Entity {
    private Coord m_pos, m_vel;
    //...
    public Entity() {
        // ...
    }

    public Coord Position {
        get {
            return m_pos;
        }
        set {
            m_pos = value;
        }
    }

    public Coord Velocity {
        get {
            return m_vel;
        }
        set {
            m_vel = value;
        }
    }
}

My main program contains a routine which simply update my entities (and so their position and velocity as well)

Now my question is : What’s the price, if I decide to update these 2 members by using new on it:

Entity a; // initialized

...

private Coord getNewPosOf(Entity e) {
   ...
   return new Coord(computed_x, computed_y);
}

public void routine() {
    a.Velocity = new Coord(x_vel, y_vel);
    a.Position = getNewPosOf(a);
}

Will that code allocate memory in the short-term storage and therefore also copy values from the constructor to the memory location of a. Or will the compiler take
care of things and avoid allocating and copying memory in that specific case?

Note: I have the feeling i’m doings things wrong, maybe I should not be using Properties to edit my Coord-type members (or worst, I may not use
Properties correctly and there is another way to do what I want)

  • 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-01T01:03:04+00:00Added an answer on June 1, 2026 at 1:03 am

    I wonder if the compiler “produces” intelligent code in specific use of the new keyword. To be precise, what will it do if the new is set on a return instruction or even on operator = ?

    First off you should clarify which “compiler” you mean. The C# compiler which compiles C# to IL, or the jit compiler that compiles IL to machine code?

    If you want to know what the C# compiler does try it and see. Use ILDASM to view the output of the C# compiler. Remember that the /optimize switch can in some cases massively change what IL is generated.

    To see what code the jitter produces, first decide which jitter is relevant to you; the compact framework, the 32 bit desktop, the 64 bit desktop, Silverlight, and so on, all have different jitters.

    Next, decide whether you are interested in the debug output of the jitter or the fully optimized output.

    If you want to see the fully optimized output then you have some work to do. Write a program that calls the method you want to see the code for once, and then attach the debugger to it before the second call. The jitter knows when a debugger is attached and generates different code to make it easier to debug, so if you want to know what is happening when you are not debugging, you have to trigger the jitter before it knows there is a debugger attached.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.