memory allocation
-
Hi, i wanna allocate memory for some data but i dont know how. With C it loks like struct MyStruct { int number; string Name; struct *Next; } But i dont know how do that in C# so could some one help me. ?? thx
In C# you don't allocate blocks of memory directly. The common language runtime that runs your code handles memory allocation and memory cleanup automatically for you. So instead of malloc, you allocate objects using the new keyword.
MyFoo obj = new MyFoo(); // allocates memory for MyFoo instance and returns that instance.
If you're absolutely certain that you need to allocate a block of memory (the only real scenario for this would be interoping with native code), then you can use
System.Runtime.InteropServices.Marshal.AllocHGlobal
or some of the other interop classes inside theInteropServices
namespace. Does that answer your question?Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
In C# you don't allocate blocks of memory directly. The common language runtime that runs your code handles memory allocation and memory cleanup automatically for you. So instead of malloc, you allocate objects using the new keyword.
MyFoo obj = new MyFoo(); // allocates memory for MyFoo instance and returns that instance.
If you're absolutely certain that you need to allocate a block of memory (the only real scenario for this would be interoping with native code), then you can use
System.Runtime.InteropServices.Marshal.AllocHGlobal
or some of the other interop classes inside theInteropServices
namespace. Does that answer your question?Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
but how can i point to the next instance of myFoo. So if i have to creat 3 data. i explain, the pointer first points to the beginning of my struct so i wanna write some think like first->Next=Second; and so on Seond->Next=Third. So!! -- modified at 12:49 Monday 8th May, 2006
-
but how can i point to the next instance of myFoo. So if i have to creat 3 data. i explain, the pointer first points to the beginning of my struct so i wanna write some think like first->Next=Second; and so on Seond->Next=Third. So!! -- modified at 12:49 Monday 8th May, 2006
Oh, so you're trying to do a linked list. I see. You can do that like so:
class MyClass
{
public MyClass Next;
}MyClass instance1 = new MyClass();
instance1.Next = new MyClass();
instance1.Next.Next = new MyClass(); // and so onTech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
but how can i point to the next instance of myFoo. So if i have to creat 3 data. i explain, the pointer first points to the beginning of my struct so i wanna write some think like first->Next=Second; and so on Seond->Next=Third. So!! -- modified at 12:49 Monday 8th May, 2006
Also, keep in mind that if you're trying to do a linked list, there's one already built into the framework. See System.Collections.Generic.LinkedList[^].
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Also, keep in mind that if you're trying to do a linked list, there's one already built into the framework. See System.Collections.Generic.LinkedList[^].
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
ok i ll; try to do it but one other rquestion, how cans i store the other data which are int Numer and string name; does it look like class Myclass { int number; string name; public Myclass Next; }
torNAdE wrote:
does it look like class Myclass { int number; string name; public Myclass Next; }
Yes. As a general recommendation, class fields should be private. If you need to access them outside of the class, use a property:
class MyClass
{
private int number; // don't expose number to the outside world, make it privatepublic int TheNumber { get { return number; // provide read-only acces to the outside world } }
}
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
torNAdE wrote:
does it look like class Myclass { int number; string name; public Myclass Next; }
Yes. As a general recommendation, class fields should be private. If you need to access them outside of the class, use a property:
class MyClass
{
private int number; // don't expose number to the outside world, make it privatepublic int TheNumber { get { return number; // provide read-only acces to the outside world } }
}
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Is Jesus the Jewish Messiah? The apostle Paul, modernly speaking: Epistles of Paul Judah Himango