C# and pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iLL
    New Member
    • Oct 2006
    • 63

    C# and pointers

    Okay, I'm teaching myself C# and now I have a question about pointers.

    When if I declare a new object, do I need to delete it? Or does garbage collection take care of that too. It seems a little odd to declare a peace of code “unsafe” if it's perfectly safe.

    If it is possible to manually deallocate, how do I do it?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I believe setting something to null will mark it for garbage collection at the first available occurance. That also happens when variables go out of scope. (For the most part, I suppose there are some I/O stuff that will cause an object to not get GC'd)

    The "unsafe" means it does not validate objects for bounds checking, if they're using memory that is not their own and other such things.

    Comment

    • iLL
      New Member
      • Oct 2006
      • 63

      #3
      I don't know if you care, but this is what I gathered since posting this yesterday.

      Pointers are unsafe because garbage collection moves data around. It is possible for GC to move data that is currently being pointed to, making a pointer point to unallocated data. To fix this C# has a concept of a fixed pointer. It looks a little like this:

      Code:
      fixed (int* p1 = &x)
      {
          fixed (double* p2 = &y)
          {
              // Do something with p1 and p2.
          }
      }
      This assures that the data will not be moved during the fixed block. I think, however, this is crap. They need to rework C# pointers so that the data a pointer is pointing is always fixed and you need to manually deallocate pointers.

      However, then we have “ref” and pointers in C#. Doing so would be the first step in making C# the mess that VC++ turned out to be (damn char array, string, Cstring, String conversions).

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I would not see moving C# BACKWARDS towards C++ as a good thing. If you want to have to do memory management on your, don't use .NET, you should be using unmanaged code.

        There is also the IntPtr object that might be helpful?

        Comment

        • iLL
          New Member
          • Oct 2006
          • 63

          #5
          You think so?

          I like C++, I just don't like VC++. Microsoft just over complicated it, I find that I spend more time fighting it then getting work done.

          Though I like managing my own garbage as any C/C++ programmer, I think it would be a bad idea to start complicating C#.


          And I don't usually use .NET being a student, however I think I should learn it myself. I see that most jobs require it.

          Comment

          • jhaxo
            New Member
            • Dec 2007
            • 57

            #6
            You can manually invoke the garbage collector via GC.Collect();

            This will often /usually/frequently start garbage collection. Note there are some memory management issues that have caught me out:

            1. If you have what is known as a 'weak' reference to a persisting variable then garbage collection will not occur. this can amount to the .net version of a memory leak. If you have an object with a reference to another object then that 'other' object won't be collected.

            2. GDI objects need to be .dispose() 'ed.

            Comment

            • iLL
              New Member
              • Oct 2006
              • 63

              #7
              Originally posted by jhaxo
              If you have an object with a reference to another object then that 'other' object won't be collected.
              Now C# implements destructors, right?

              If you set the object variable to null then call GC.Collect() in the destructor, that should take care of things.



              So is that by design, or is it just a bug?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by iLL
                Now C# implements destructors, right?
                Yes it does. I never use them. I use the Finalize method instead.


                Originally posted by iLL
                Now C# implements destructors, right?

                If you set the object variable to null then call GC.Collect() in the destructor, that should take care of things.
                ..
                The general rule of thumb is to never call the Collect method.

                Comment

                • iLL
                  New Member
                  • Oct 2006
                  • 63

                  #9
                  Thanks a lot.

                  I've learned more in these few posts then I have in the few chapters I've read from my C# book

                  Comment

                  Working...