Can C# GC move memory objectsHow do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?Deep cloning objectsHow do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Why not inherit from List<T>?

How can an advanced civilization forget how to manufacture its technology?

How did the hit man miss?

Wrapper in return method for test class

Would letting a multiclass character rebuild their character to be single-classed be game-breaking?

If a specific mass of air is polluted, will the pollution stick with it?

What's the fastest way to get Hard To Borrow (HTB) stocks?

How can I deal with a player trying to insert real-world mythology into my homebrew setting?

Correlation of independent random processes

How to achieve this rough borders and stippled illustration look?

Why are Hobbits so fond of mushrooms?

Correct use of ergeben?

Bronze Age Underwater Civilization

During copyediting, journal disagrees about spelling of paper's main topic

Why did my rum cake turn black?

Was adding milk to tea started to reduce employee tea break time?

Is Prophet from Facebook any different from a linear regression?

Reverse the word order in string, without reversing the words

Can I use "candidate" as a verb?

Supporting developers who insist on using their pet language

Is Trump personally blocking people on Twitter?

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

Stuck Apple Mail - how to reset?

Why is dry soil hydrophobic? Bad gardener paradox

Is a Lisp program in both prog-mode and lisp-mode?



Can C# GC move memory objects


How do I calculate someone's age in C#?What is the difference between String and string in C#?Hidden Features of C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?Deep cloning objectsHow do I enumerate an enum in C#?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?Why not inherit from List<T>?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








8















Let's suppose this very basic C# code:



var tab = new int[10];


I have read that non fixed variables can be moved in memory by garbage collector.



My question is: Is it possible that "tab" address change during my program execution ?



I just want to understand.



In fact, no matter if tab value change.










share|improve this question
























  • Follow up question: why do you care? Just interested?

    – Blorgbeard
    Jul 4 at 16:36











  • yes, just interested

    – Bob5421
    Jul 4 at 16:37











  • I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

    – David Pilkington
    Jul 5 at 4:42

















8















Let's suppose this very basic C# code:



var tab = new int[10];


I have read that non fixed variables can be moved in memory by garbage collector.



My question is: Is it possible that "tab" address change during my program execution ?



I just want to understand.



In fact, no matter if tab value change.










share|improve this question
























  • Follow up question: why do you care? Just interested?

    – Blorgbeard
    Jul 4 at 16:36











  • yes, just interested

    – Bob5421
    Jul 4 at 16:37











  • I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

    – David Pilkington
    Jul 5 at 4:42













8












8








8








Let's suppose this very basic C# code:



var tab = new int[10];


I have read that non fixed variables can be moved in memory by garbage collector.



My question is: Is it possible that "tab" address change during my program execution ?



I just want to understand.



In fact, no matter if tab value change.










share|improve this question
















Let's suppose this very basic C# code:



var tab = new int[10];


I have read that non fixed variables can be moved in memory by garbage collector.



My question is: Is it possible that "tab" address change during my program execution ?



I just want to understand.



In fact, no matter if tab value change.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 4 at 18:37









casperOne

66.5k14 gold badges161 silver badges226 bronze badges




66.5k14 gold badges161 silver badges226 bronze badges










asked Jul 4 at 8:06









Bob5421Bob5421

1,5913 gold badges24 silver badges63 bronze badges




1,5913 gold badges24 silver badges63 bronze badges












  • Follow up question: why do you care? Just interested?

    – Blorgbeard
    Jul 4 at 16:36











  • yes, just interested

    – Bob5421
    Jul 4 at 16:37











  • I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

    – David Pilkington
    Jul 5 at 4:42

















  • Follow up question: why do you care? Just interested?

    – Blorgbeard
    Jul 4 at 16:36











  • yes, just interested

    – Bob5421
    Jul 4 at 16:37











  • I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

    – David Pilkington
    Jul 5 at 4:42
















Follow up question: why do you care? Just interested?

– Blorgbeard
Jul 4 at 16:36





Follow up question: why do you care? Just interested?

– Blorgbeard
Jul 4 at 16:36













yes, just interested

– Bob5421
Jul 4 at 16:37





yes, just interested

– Bob5421
Jul 4 at 16:37













I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

– David Pilkington
Jul 5 at 4:42





I have never understood this sites mentality on questions like this? He mentions in the question that he just wanted to understand the underlying workings. But almost every answer still tells OP that it doesn't matter, he shouldn't care.

– David Pilkington
Jul 5 at 4:42












4 Answers
4






active

oldest

votes


















8














Yes it will.



But you can use the fixed keyword to stop the GC from moving it if you so desire.






share|improve this answer






























    8














    Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.



    The point is, in c# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.






    share|improve this answer
































      2














      It is certain that the array object could be moved in memory.



      But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.



      If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.






      share|improve this answer
































        1














        Considering you are using var it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.



        On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.



        If you want to be sure to fix location of that variable you can use "fixed".






        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56883363%2fcan-c-sharp-gc-move-memory-objects%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          8














          Yes it will.



          But you can use the fixed keyword to stop the GC from moving it if you so desire.






          share|improve this answer



























            8














            Yes it will.



            But you can use the fixed keyword to stop the GC from moving it if you so desire.






            share|improve this answer

























              8












              8








              8







              Yes it will.



              But you can use the fixed keyword to stop the GC from moving it if you so desire.






              share|improve this answer













              Yes it will.



              But you can use the fixed keyword to stop the GC from moving it if you so desire.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 4 at 8:13









              David PilkingtonDavid Pilkington

              11.5k2 gold badges27 silver badges56 bronze badges




              11.5k2 gold badges27 silver badges56 bronze badges























                  8














                  Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.



                  The point is, in c# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.






                  share|improve this answer





























                    8














                    Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.



                    The point is, in c# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.






                    share|improve this answer



























                      8












                      8








                      8







                      Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.



                      The point is, in c# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.






                      share|improve this answer















                      Yes. The memory address of tab can be (and most probably will be) changed. Reference: ECMA-334 C# Language Specification, chapter 23.4.



                      The point is, in c# you don't need to bother about memory addresses as it's a managed language. All references to tab variable will be changed accordingly, and your program will survive garbage collection seamlessly.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 4 at 8:24

























                      answered Jul 4 at 8:10









                      RenatRenat

                      2,1441 gold badge11 silver badges22 bronze badges




                      2,1441 gold badge11 silver badges22 bronze badges





















                          2














                          It is certain that the array object could be moved in memory.



                          But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.



                          If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.






                          share|improve this answer





























                            2














                            It is certain that the array object could be moved in memory.



                            But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.



                            If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.






                            share|improve this answer



























                              2












                              2








                              2







                              It is certain that the array object could be moved in memory.



                              But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.



                              If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.






                              share|improve this answer















                              It is certain that the array object could be moved in memory.



                              But note that you cannot obtain the pointer of a managed reference object, or you use a fixed block, it cannot be moved by GC within it.



                              If the array object is moved, the reference from variable tab to the array object is also fixed by GC, so there would be no way for you to see anything impacted by GC.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jul 4 at 8:22

























                              answered Jul 4 at 8:13









                              AlseinAlsein

                              8281 silver badge16 bronze badges




                              8281 silver badge16 bronze badges





















                                  1














                                  Considering you are using var it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.



                                  On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.



                                  If you want to be sure to fix location of that variable you can use "fixed".






                                  share|improve this answer



























                                    1














                                    Considering you are using var it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.



                                    On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.



                                    If you want to be sure to fix location of that variable you can use "fixed".






                                    share|improve this answer

























                                      1












                                      1








                                      1







                                      Considering you are using var it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.



                                      On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.



                                      If you want to be sure to fix location of that variable you can use "fixed".






                                      share|improve this answer













                                      Considering you are using var it means you are defining it in a method. Your variable lives in the context of that method for a very short time, and it is very unlikely that GC will move it.



                                      On the other hand GC will move memory blocks to reduce memory fragmentation so yes it may move your variables. Best part of it you will not even notice it since it is same old "tab" for you.



                                      If you want to be sure to fix location of that variable you can use "fixed".







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 4 at 8:14









                                      edokanedokan

                                      3,24316 silver badges32 bronze badges




                                      3,24316 silver badges32 bronze badges



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56883363%2fcan-c-sharp-gc-move-memory-objects%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                          Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                                          Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?