FAQ
func (s *Stack) pop() interface{} {
  if s.isEmpty() {
      return nil
  }
  removal := s.items[len(s.items) - 1]
  s.items = s.items[:len(s.items) - 1]
  return removal
}

Does the backing array of the slice still contain a reference to the removed item, will it interfere with GC?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dmitry Vyukov at Jun 16, 2013 at 4:34 pm

    On Sun, Jun 16, 2013 at 8:25 PM, Jon Renner wrote:
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    removal := s.items[len(s.items) - 1]
    s.items = s.items[:len(s.items) - 1]
    return removal
    }

    Does the backing array of the slice still contain a reference to the removed item, will it interfere with GC?
    Yes, it will interfere. Assign nil to the element.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jon Renner at Jun 17, 2013 at 1:32 am
    Like so?

    // remove most recently added item
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items[i] = nil
    s.items = s.items[:i]
    return removal
    }

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitry Vyukov at Jun 17, 2013 at 6:48 am

    On Mon, Jun 17, 2013 at 5:32 AM, Jon Renner wrote:
    Like so?

    // remove most recently added item
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items[i] = nil
    s.items = s.items[:i]
    return removal
    }

    yup

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Pow3rlife at Jun 18, 2013 at 1:02 pm
    W dniu poniedziałek, 17 czerwca 2013 08:47:45 UTC+2 użytkownik Dmitry
    Vyukov napisał:
    On Mon, Jun 17, 2013 at 5:32 AM, Jon Renner wrote:
    Like so?

    // remove most recently added item
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items[i] = nil
    s.items = s.items[:i]
    return removal
    }

    yup

    Why not do that?

    func (s *Stack) pop() interface{} {
         if s.isEmpty() {
             return nil
         }
         i := len(s.items) - 1
         removal := s.items[i]
         s.items = s.items[:i-1]
         return removal
    }


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitry Vyukov at Jun 18, 2013 at 1:19 pm

    On Tue, Jun 18, 2013 at 4:55 PM, wrote:

    W dniu poniedziałek, 17 czerwca 2013 08:47:45 UTC+2 użytkownik Dmitry Vyukov
    napisał:
    On Mon, Jun 17, 2013 at 5:32 AM, Jon Renner wrote:
    Like so?

    // remove most recently added item
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items[i] = nil
    s.items = s.items[:i]
    return removal
    }

    yup


    Why not do that?


    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items = s.items[:i-1]
    return removal
    }

    Because the slice still contains the removed value, and this will
    prevent garbage collection of the objects that are referenced by the
    value.

    The values between len and cap are still reachable, consider:
    http://play.golang.org/p/9LDu0OoEPH

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Pow3rlife at Jun 18, 2013 at 3:04 pm
    Ok, thank you.

    W dniu wtorek, 18 czerwca 2013 15:19:30 UTC+2 użytkownik Dmitry Vyukov
    napisał:
    On Tue, Jun 18, 2013 at 4:55 PM, <pow3...@gmail.com <javascript:>>
    wrote:

    W dniu poniedziałek, 17 czerwca 2013 08:47:45 UTC+2 użytkownik Dmitry Vyukov
    napisał:
    On Mon, Jun 17, 2013 at 5:32 AM, Jon Renner wrote:
    Like so?

    // remove most recently added item
    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items[i] = nil
    s.items = s.items[:i]
    return removal
    }

    yup


    Why not do that?


    func (s *Stack) pop() interface{} {
    if s.isEmpty() {
    return nil
    }
    i := len(s.items) - 1
    removal := s.items[i]
    s.items = s.items[:i-1]
    return removal
    }

    Because the slice still contains the removed value, and this will
    prevent garbage collection of the objects that are referenced by the
    value.

    The values between len and cap are still reachable, consider:
    http://play.golang.org/p/9LDu0OoEPH
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 16, '13 at 4:25p
activeJun 18, '13 at 3:04p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase