FAQ
I the example:

type T struct { i int }

func f() {
   var x T
   for i:=0; i < 100; i++ {
     x=T{i}
   }
}

I allocate memory for 100 objects of type T.

Do I allocate memory for 100 objects of type T in this example ?:

func f() {
   x:=new(T)
   for i:=0; i < 100; i++ {
     *x=T{i}
   }
}

michal

--
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

  • Jesse McNelis at Sep 26, 2013 at 9:57 am

    On 26/09/2013 7:47 PM, wrote:
    I the example:

    type T struct { i int }

    func f() {
    var x T
    for i:=0; i < 100; i++ {
    x=T{i}
    }
    }

    I allocate memory for 100 objects of type T.
    You don't. You just assign to the same memory 100 times.
    Do I allocate memory for 100 objects of type T in this example ?:

    func f() {
    x:=new(T)
    for i:=0; i < 100; i++ {
    *x=T{i}
    }
    }
    Again, just assigning.

    --
    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.
  • Chris dollin at Sep 26, 2013 at 9:58 am

    On 26 September 2013 10:46, wrote:

    I the example:

    type T struct { i int }

    func f() {
    var x T
    for i:=0; i < 100; i++ {
    x=T{i}
    }
    }

    I allocate memory for 100 objects of type T.
    I don't think so. There's only one x, which gets overwritten
    100 times. Possibly (and possibly not) there's a separate
    piece of store used by all the T{i} constructions.

    Do I allocate memory for 100 objects of type T in this example ?:

    func f() {
    x:=new(T)
    for i:=0; i < 100; i++ {
    *x=T{i}
    }
    }

    I don't think so. x points to an object of type T, which is
    repeatedly overwritten -- the same as the previous example,
    except that it's *x, not x, that's overwritten.

    Chris

    --
    Chris "allusive" Dollin

    --
    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
postedSep 26, '13 at 9:47a
activeSep 26, '13 at 9:58a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase