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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:20:16+00:00 2026-05-18T12:20:16+00:00

Suppose I write the following code: public ref class Data { public: Data() {

  • 0

Suppose I write the following code:

public ref class Data
{
public:
    Data()
    {
    }

    Int32 Age;
    Int32 year;
};

public void Test()
{
    int age = 30;  
    Int32 year = 2010;  
    int* pAge = &age;  
    int* pYear = &year;


    Data^ data = gcnew Data();
    int* pDataYear = &data->Year; // pData is interior pointer and the compiler will throw error
}

If you compile the program, the compiler will throw error:
error C2440: ‘initializing’ : cannot convert from ‘cli::interior_ptr’ to ‘int *’
So I learned the “&data->Year” is a type of interior pointer.
UPDATES: I tried to use “&(data->Year)”, same error.

But how about pAge and pYear?
Are they native pointers, interior pointers or pinned pointers??

If I want to use them in the following native function:

void ChangeNumber(int* pNum);

Will it be safe to pass either pAge or pYear?

  • 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-18T12:20:17+00:00Added an answer on May 18, 2026 at 12:20 pm

    They (pAge and pYear) are native pointers, and passing them to a native function is safe. Stack variables (locals with automatic storage lifetime) are not subject to being rearranged by the garbage collector, so pinning is not necessary.

    Copying managed data to the stack, then passing it to native functions, solves the gc-moving-managed-data-around problem in many cases (of course, don’t use it in conjunction with callbacks that expect the original variable to be updated before your wrapper has a chance to copy the value back).

    To get a native pointer to managed data, you have to use a pinning pointer. This can be slower than the method of copying the value to the stack, so use it for large values or when you really need the function to operate directly on the same variable (e.g. the variable is used in callbacks or multi-threading).

    Something like:

    pin_ptr<int> p = &mgd_obj.field;
    

    See also the MSDN documentation

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

Sidebar

Related Questions

Suppose someone (other than me) writes the following code and compiles it into an
Suppose you have the following string: white sand, tall waves, warm sun It's easy
Would it suppose any difference regarding overhead to write an import loading all the
Suppose I am writing an application in C++ and C#. I want to write
Java is supposed to be write once, run anywhere and it really can be,
Suppose a long-running process writes to a log file. Suppose that log file is
I need to write a java script. This is supposed to validate if the
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000" .

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.