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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:28:38+00:00 2026-05-23T10:28:38+00:00

In unsafe code in C#, I assigned a pointer to the managed variable of

  • 0

In unsafe code in C#, I assigned a pointer to the managed variable of an array type:

int[] array = new int[3];

// ...

fixed (int* ptr = array)
{
    // Some code
}

Then I looked at corresponding part of the IL code:

.locals init ([0] int32[] 'array',
       [1] int32& pinned ptr)

Since this is unsafe code, and int* ptr is declaration of unmanaged pointer (or I think so at the moment), why in the CIL code doesn’t write int32* ptr, instead of int32& ptr?

  • 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-23T10:28:39+00:00Added an answer on May 23, 2026 at 10:28 am

    http://www.ecma-international.org/publications/standards/Ecma-335.htm

    Page 334

    “1.1.5.2 Managed pointers (type &)

    1.2 Managed pointers (&) can point to a local variable, a method argument, a
    field of an object, a field of a value
    type, an element of an array, a static
    field, or the address where an element
    just past the end of an array would be
    stored (for pointer indexes into
    managed arrays).
    Managed pointers
    cannot be null. (They shall be
    reported to the garbage collector,
    even if they do not point to managed
    memory)”

    Page 149

    7.1.2 pinned

    The signature encoding for pinned shall appear only in signatures that describe local variables (§15.4.1.3). While a method with a pinned local variable is executing, the VES shall not relocate the object to which the local refers. That is, if the implementation of the CLI uses a garbage collector that moves objects, the collector shall not move objects that are referenced by an active pinned local variable.
    [Rationale: If unmanaged pointers are used to dereference managed objects, these objects shall be pinned. This happens, for example, when a managed object is passed to a method designed to operate with unmanaged data. end rationale]

    I agree with Hans as to the rational behind the msil language design choice.


    These two things are different:

    int[] arry = new int[5];
    
    fixed (int* ptr = arry)
    {
      ...
    }
    

    vs.

    int* ptr = stackalloc int[5];
    

    If you look at the IL created for the second one, you’ll see this (which I think is what you’re expecting):

    .locals init ([0] int32* ptr)
    

    In the first version (your version), you’re pointing to an instance of System.Array (a managed type). In my version (using stackalloc) you’re pointing to what I think you’re expecting to point to… a block of memory large enough for 5 ints.

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

Sidebar

Related Questions

Read this question today about safe and unsafe code I then read about it
I've noticed a new trend in distributing potentially unsafe code where people will post
I have some code here private void Run() { MyClass c = new MyClass();
I am creating (then altering) a bitmap using unsafe code in a C# winforms
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I was wondering if the C# project setting Allow unsafe code applies only to
Is there a way to prevent developers from committing code when certain unsafe or
What are the limitations of unsafe code, in C#? For example, can I do
how do CLR interact with unsafe code I found various result on Google but
I'm having the following problem: I'm developing a C# application which requires unsafe code

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.