Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. memory allocation

memory allocation

Scheduled Pinned Locked Moved C#
csharpperformancehelpquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    torNAdE
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • T torNAdE

      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

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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 the InteropServices 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

      T 1 Reply Last reply
      0
      • J Judah Gabriel 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 the InteropServices 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

        T Offline
        T Offline
        torNAdE
        wrote on last edited by
        #3

        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

        J 2 Replies Last reply
        0
        • T torNAdE

          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

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          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 on

          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

          1 Reply Last reply
          0
          • T torNAdE

            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

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • J Judah Gabriel 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

              T Offline
              T Offline
              torNAdE
              wrote on last edited by
              #6

              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; }

              J 1 Reply Last reply
              0
              • T torNAdE

                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; }

                J Offline
                J Offline
                Judah Gabriel Himango
                wrote on last edited by
                #7

                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 private

                public 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

                T 1 Reply Last reply
                0
                • J Judah Gabriel 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 private

                  public 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

                  T Offline
                  T Offline
                  torNAdE
                  wrote on last edited by
                  #8

                  thank you verry much. it was helpfull:)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups