Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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];
Posted
Updated 21-May-16 3:40am

Without knowing the error, it's not possible to be exact, but the chances are you forgot to use fixed:
C#
private unsafe void myMethod()
    {
    fixed (double* p = new double[10])
        {
        ...
        }
    }
 
Share this answer
 
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:
C#
double[] myArray = new double[10];
// that's all

See also: Garbage collection (computer science) — Wikipedia, the free encyclopedia[^].

You really need to grab the manual on the platform, language and general programming and read it all, from the beginning to the end; it would be the best to do the simplest exercises. Perhaps, you may also need to "unlearn what you have learned".

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900