如果我使用“&”返回什么样的指针获取 C++\CLI 中值类型的地址?

发布于 2024-10-07 09:08:39 字数 756 浏览 3 评论 0原文

假设我写了下面的代码:

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
}

如果编译程序,编译器会抛出错误:
错误 C2440:“初始化”:无法从“cli::interior_ptr”转换为“int *”
所以我了解到“&data->Year”是一种内部指针。
更新:我尝试使用“&(data->Year)”,同样的错误。

但是 pageAge 和 pYear 怎么样?
它们是本机指针、内部指针还是固定指针?

如果我想在以下本机函数中使用它们:

void ChangeNumber(int* pNum);

传递 pAge 或 pYear 是否安全?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-10-14 09:08:39

它们(pAgepYear)是本机指针,将它们传递给本机函数是安全的。堆栈变量(具有自动存储生命周期的局部变量)不会被垃圾收集器重新排列,因此不需要固定。

将托管数据复制到堆栈,然后将其传递给本机函数,在许多情况下解决了 gc-moving-driven-data-around 问题(当然,不要将其与期望原始变量更新的回调结合使用)在你的包装器有机会将值复制回来之前)。

要获取指向托管数据的本机指针,您必须使用固定指针。这可能比将值复制到堆栈的方法慢,因此对于较大的值或当您确实需要函数直接操作同一变量时(例如,该变量用于回调或多线程),请使用它。

类似于:

pin_ptr<int> p = &mgd_obj.field;

另请参阅MSDN 文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文