Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can C# GC move memory objects

Tags:

c#

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.

like image 329
Bob5421 Avatar asked Jul 04 '19 08:07

Bob5421


People also ask

Is Can-C good for your eyes?

Users and scientists have reported that Can-C is also great for: Dry eye syndrome, Corneal disorders, Computer vision syndrome, Eye strain, Ocular inflammation, Blurred vision and other systemic diseases, also a benefit for those who wear contact lenses.

How long does it take Can-C to work?

The minimum time to see results from Can-C eye drops, which is generally twice per day, is 6 months. On the Natural Eye Care website, we offer Can-C eye drops in several bulk packets at the best prices on the Internet.

Is Can-C good for dogs?

SAFE FOR HUMANS AND DOGS - Can-C is the first and only patented NAC eye drop that uses the exact formula proven effective in both animal and human trials, offering a non-invasive alternative to cataract surgery.

What is the best eye drops for cataracts?

1 Lanosterol eye drops could potentially be a safe, non-invasive, and less costly alternative to cataract surgery for patients who have moderate forms of cataracts.


2 Answers

Yes it will.

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

like image 75
David Pilkington Avatar answered Nov 08 '22 01:11

David Pilkington


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.

like image 34
Renat Avatar answered Nov 08 '22 00:11

Renat