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. How to delete a pointer.

How to delete a pointer.

Scheduled Pinned Locked Moved C#
tutorial
9 Posts 5 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.
  • D Offline
    D Offline
    daavena
    wrote on last edited by
    #1

    Hi, I have this: int a = 5; int *p = a; Ho can I delete the pointer. Thank you.

    D A P 3 Replies Last reply
    0
    • D daavena

      Hi, I have this: int a = 5; int *p = a; Ho can I delete the pointer. Thank you.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      What do you mean? There's nothing here to "delete". It would appear that you're comming from a C++ background and trying to use pointers the exact same you did before. C# does support pointers, but not to the extent that C++ does.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      D 1 Reply Last reply
      0
      • D daavena

        Hi, I have this: int a = 5; int *p = a; Ho can I delete the pointer. Thank you.

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        I'm not sure that you need to, C# has that garbage collection thing. Where it clears memory that's not being used every now and then.

        My current favourite word is: Bauble!

        -SK Genius

        D 2 Replies Last reply
        0
        • A Anthony Mushrow

          I'm not sure that you need to, C# has that garbage collection thing. Where it clears memory that's not being used every now and then.

          My current favourite word is: Bauble!

          -SK Genius

          D Offline
          D Offline
          daavena
          wrote on last edited by
          #4

          C# has the garbage collection I agree with you. But I have to use usafe code. when I turn it on, wil garbage collection still work?

          C 1 Reply Last reply
          0
          • A Anthony Mushrow

            I'm not sure that you need to, C# has that garbage collection thing. Where it clears memory that's not being used every now and then.

            My current favourite word is: Bauble!

            -SK Genius

            D Offline
            D Offline
            daavena
            wrote on last edited by
            #5

            C# has the garbage collection I agree with you. I have to use usafe code, when I turn it on, wil garbage collection still work?

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              What do you mean? There's nothing here to "delete". It would appear that you're comming from a C++ background and trying to use pointers the exact same you did before. C# does support pointers, but not to the extent that C++ does.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              D Offline
              D Offline
              daavena
              wrote on last edited by
              #6

              Hi, I use unsafe code - I am able to work with pointers(I think).

              D 1 Reply Last reply
              0
              • D daavena

                Hi, I have this: int a = 5; int *p = a; Ho can I delete the pointer. Thank you.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                It'll go out of scope.

                1 Reply Last reply
                0
                • D daavena

                  C# has the garbage collection I agree with you. But I have to use usafe code. when I turn it on, wil garbage collection still work?

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  daavena wrote:

                  But I have to use usafe code. when I turn it on, wil garbage collection still work?

                  Yes, it will still work. However, remember it is non-deterministic. If you are using pointers into the managed heap you need to make the pointer [fixed](http://msdn2.microsoft.com/en-us/library/f58wzh21\(VS.80\).aspx)[[^](http://msdn2.microsoft.com/en-us/library/f58wzh21\(VS.80\).aspx "New Window")] to ensure that garbage collection does not occur on the object you have a pointer to (remember the garbage collection process will also attempt to defragment the heap, so even if the object isn't being collected it may be moved around). So, once you are finished with your object just ensure it is no longer fixed and allow the managed reference to the memory go out of scope and the garbage collector will get it. If you are encapsulating some piece of native code into a class you may want to look at the IDisposable interface as a way of cleaning up resources when an object of that class is no longer required. This is especially important if the native code did the memory allocation rather than you allocate memory then pass it to the native code.


                  Upcoming FREE developer events: * Developer Day Scotland My website

                  1 Reply Last reply
                  0
                  • D daavena

                    Hi, I use unsafe code - I am able to work with pointers(I think).

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Yeah, with pointers, unsafe code is a given. You haven't answered anyones questions about what you're trying to do with this. There is no such thing as "deleting" with a pointer in C#.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007

                    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