unsafe code and array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerome

    #1

    unsafe code and array

    Hi all,

    I *have* to use unsafe code, but I have a questions : it seems I can't use
    delete to free something like : int * myarray = new int[size].
    What should I do if I wanna create/delte/resize such an array ? I'm afraid I
    didn't get all about the unsafe code !
    Clearly, how should I use the unsafe code to do something like that (in c++)
    :
    ....
    if(myArray != null)
    delete[] myArray;
    myArray = new int[newsize]
    ....

    Thank you for your help !

    Jérôme


  • Richard Blewett [DevelopMentor]

    #2
    Re: unsafe code and array

    "Jerome" <jlgarbage@wana doo.fr> wrote in message
    news:%23aL8aaRE GHA.3528@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > Hi all,
    >
    > I *have* to use unsafe code, but I have a questions : it seems I can't use
    > delete to free something like : int * myarray = new int[size].
    > What should I do if I wanna create/delte/resize such an array ? I'm afraid
    > I didn't get all about the unsafe code !
    > Clearly, how should I use the unsafe code to do something like that (in
    > c++) :
    > ...
    > if(myArray != null)
    > delete[] myArray;
    > myArray = new int[newsize]
    > ...
    >
    > Thank you for your help !
    >
    > Jérôme
    >[/color]

    why do you need to use unsafe code?

    You don;t need to delete here because .NET is a GC'd environment and the
    allocated array memory will get garbage collected once its eligible.

    Regards

    Richard Blewett - DevelopMentor




    Comment

    • Jerome

      #3
      Re: unsafe code and array

      I agree, but maybe i missed something important : what happens if you use
      the new operator 2 times on the same object ?
      for instance :
      int[] myArray = new int[50];
      myArray = new int[25];

      Can I assume it'll create a first array of 50 int, then delete it a create
      an array of 25 int and GC will manage the whole memory proccess?

      "Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co
      dot uk> a écrit dans le message de news:
      OjGN%23gREGHA.3 728@tk2msftngp1 3.phx.gbl...[color=blue]
      > "Jerome" <jlgarbage@wana doo.fr> wrote in message
      > news:%23aL8aaRE GHA.3528@TK2MSF TNGP12.phx.gbl. ..[color=green]
      >> Hi all,
      >>
      >> I *have* to use unsafe code, but I have a questions : it seems I can't
      >> use delete to free something like : int * myarray = new int[size].
      >> What should I do if I wanna create/delte/resize such an array ? I'm
      >> afraid I didn't get all about the unsafe code !
      >> Clearly, how should I use the unsafe code to do something like that (in
      >> c++) :
      >> ...
      >> if(myArray != null)
      >> delete[] myArray;
      >> myArray = new int[newsize]
      >> ...
      >>
      >> Thank you for your help !
      >>
      >> Jérôme
      >>[/color]
      >
      > why do you need to use unsafe code?
      >
      > You don;t need to delete here because .NET is a GC'd environment and the
      > allocated array memory will get garbage collected once its eligible.
      >
      > Regards
      >
      > Richard Blewett - DevelopMentor
      > http://www.dotnetconsult.co.uk/weblog
      > http://www.dotnetconsult.co.uk
      >[/color]


      Comment

      • Joanna Carter [TeamB]

        #4
        Re: unsafe code and array

        "Jerome" <jlgarbage@wana doo.fr> a écrit dans le message de news:
        ecrLXoREGHA.102 8@TK2MSFTNGP11. phx.gbl...

        |I agree, but maybe i missed something important : what happens if you use
        | the new operator 2 times on the same object ?
        | for instance :
        | int[] myArray = new int[50];
        | myArray = new int[25];
        |
        | Can I assume it'll create a first array of 50 int, then delete it a create
        | an array of 25 int and GC will manage the whole memory proccess?

        Yes, otherwise the Array.Resize method couldn't work.

        Joanna

        --
        Joanna Carter [TeamB]
        Consultant Software Engineer


        Comment

        • Richard Blewett [DevelopMentor]

          #5
          Re: unsafe code and array

          "Jerome" <jlgarbage@wana doo.fr> wrote in message
          news:ecrLXoREGH A.1028@TK2MSFTN GP11.phx.gbl...[color=blue]
          >I agree, but maybe i missed something important : what happens if you use
          >the new operator 2 times on the same object ?
          > for instance :
          > int[] myArray = new int[50];
          > myArray = new int[25];
          >
          > Can I assume it'll create a first array of 50 int, then delete it a create
          > an array of 25 int and GC will manage the whole memory proccess?
          >[/color]

          You can't use the new operator on the same *object* the new operator creates
          a new object. What you are referring to is assigning the new object to the
          same *reference*. And what happens in that case?

          Well assuming that the no other references exist to the old array then next
          time the GC runs it will collect it (this is a simplification of the process
          but in principle this is what happens).

          Regards

          Richard Blewett - DevelopMentor




          Comment

          Working...