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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Allocating a pointer

Allocating a pointer

Scheduled Pinned Locked Moved C#
csharpc++
8 Posts 3 Posters 1 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.
  • H Offline
    H Offline
    hockymot2008_2009
    wrote on last edited by
    #1

    I have a pointer to a struct. In C++ I can allocate and deallocate it but in C#, I can't. Could you give me a solution plz !

    H E G 4 Replies Last reply
    0
    • H hockymot2008_2009

      I have a pointer to a struct. In C++ I can allocate and deallocate it but in C#, I can't. Could you give me a solution plz !

      H Offline
      H Offline
      hockymot2008_2009
      wrote on last edited by
      #2

      For example I have a struct like this : public unsafe struct V { unsafe public V* backpointer; }; When I declare : V n = new V(); n.backpointer = &x in a loop then the previous value of backpointer changed. Could you plz help me ?

      modified on Sunday, October 5, 2008 9:50 PM

      H G 2 Replies Last reply
      0
      • H hockymot2008_2009

        For example I have a struct like this : public unsafe struct V { unsafe public V* backpointer; }; When I declare : V n = new V(); n.backpointer = &x in a loop then the previous value of backpointer changed. Could you plz help me ?

        modified on Sunday, October 5, 2008 9:50 PM

        H Offline
        H Offline
        hockymot2008_2009
        wrote on last edited by
        #3

        I think that the garbage collector deallocated the memory. How can we stop it plz ?

        G 1 Reply Last reply
        0
        • H hockymot2008_2009

          I have a pointer to a struct. In C++ I can allocate and deallocate it but in C#, I can't. Could you give me a solution plz !

          E Offline
          E Offline
          Eslam Afifi
          wrote on last edited by
          #4

          Use the fixed[^] keyword. More about unsafe code[^]. By the way, you don't need a second unsafe keyword inside another unsafe. And you don't need a semicolon after a struct or class declaration.

          public unsafe struct V
          {
          public V* backpointer; //you don't need another unsafe keyword
          }

          Eslam Afifi

          1 Reply Last reply
          0
          • H hockymot2008_2009

            I have a pointer to a struct. In C++ I can allocate and deallocate it but in C#, I can't. Could you give me a solution plz !

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            In .NET a struct is always a value type, it's not like in C++ where a struct is either a reference type or a value type depending on how it's used. You should use a class, not a struct.

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            • H hockymot2008_2009

              For example I have a struct like this : public unsafe struct V { unsafe public V* backpointer; }; When I declare : V n = new V(); n.backpointer = &x in a loop then the previous value of backpointer changed. Could you plz help me ?

              modified on Sunday, October 5, 2008 9:50 PM

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              hockymot2008_2009 wrote:

              V n = new V();

              That doesn't do what you think that it does. As the struct is a value type, you are not allocating memory for an object, you are just creating a struct value. The value is assigned to the struct n, and will overwrite the previous value in the struct. Is there any reason why you are trying to use structs instead of classes and pointers instead of references?

              Despite everything, the person most likely to be fooling you next is yourself.

              1 Reply Last reply
              0
              • H hockymot2008_2009

                I think that the garbage collector deallocated the memory. How can we stop it plz ?

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                hockymot2008_2009 wrote:

                I think that the garbage collector deallocated the memory.

                No, it didn't, as you never allocated any memory from the heap.

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                • H hockymot2008_2009

                  I have a pointer to a struct. In C++ I can allocate and deallocate it but in C#, I can't. Could you give me a solution plz !

                  H Offline
                  H Offline
                  hockymot2008_2009
                  wrote on last edited by
                  #8

                  Thanks for your replies :D

                  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