将内存分配给双指针 [英] Allocating memory to double pointer

查看:64
本文介绍了将内存分配给双指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double * p;

p = new double [10]; - >它显示错误



如何分配内存到p ??



我有什么试过:



double * p;

p = new double [10];

double *p;
p=new double[10]; --> It is showing error

how to allocate memory to p??

What I have tried:

double *p;
p=new double[10];

推荐答案

在不知道错误的情况下,它不可能是准确的,但是您忘记使用固定的可能性:

Without knowing the error, it's not possible to be exact, but the chances are you forgot to use fixed:
private unsafe void myMethod()
    {
    fixed (double* p = new double[10])
        {
        ...
        }
    }


在C#中,仅在特殊情况下允许指针,仅在 unsafe 内容下。请参阅:

不安全(C#参考) [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/aa288474%28v=vs.71%29.aspx>不安全代码教程(C#) [ ^ ]。



管理内存的指针通常与固定有关:

固定声明(C#参考) [ ^ ]。



如果您需要更深入地了解上述内容,则很少使用上述所有内容技术。



从你的问题来看,很明显你不需要指针,你是只是不知道.NET和C#如何在高级别上运行。使用.NET,您正在使用托管内存。您不释放内存而不显式分配内存,而是在使用引用类型的构造函数(您需要学习引用类型与值类型)和数组初始化时分配内存:

In C#, pointers are allowed only in special cases, only under unsafe content. Please see:
unsafe (C# Reference)[^],
Unsafe Code Tutorial (C#)[^].

Pointers to managed memory usually related to pinning:
fixed Statement (C# Reference)[^].

All the above is used very rarely in the cases when you need much deeper understanding of the technology.

From your question, it's apparent that you don't really need pointers, you are just unaware how .NET and C# work on the high level. With .NET, you are working with managed memory. You don't deallocate memory and not allocate it explicitly, instead, memory is allocated when you use constructors of the reference types (you will need to learn reference types vs. value types) and initialization of arrays:
double[] myArray = new double[10];
// that's all



另请参阅:垃圾收集(计算机科学) - 维基百科,免费的百科全书 [ ^ ]。



你真的需要掌握平台上的手册,语言和一般编程,并从头到尾阅读全部内容;做最简单的练习是最好的。也许,你可能还需要忘掉你学到的东西。



-SA


这篇关于将内存分配给双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆