[go-nuts] Does Go have functions to fill an array, like memset in C?

6,060 views
Skip to first unread message

Lambda

unread,
May 25, 2010, 7:01:14 AM5/25/10
to golang-nuts
Does Go have functions to fill an array with a specified value, like
memset in C?

chris dollin

unread,
May 25, 2010, 7:15:04 AM5/25/10
to Lambda, golang-nuts
On 25 May 2010 12:01, Lambda <steph...@gmail.com> wrote:
Does Go have functions to fill an array with a specified value, like
memset in C?

Don't think so (couldn't see a hint in the packages after a
quick scan). For loops work, and things get automatically
initialised to their zero values.

Was there a specific use you had in mind?

Chris

--
Chris "allusive" Dollin

Lambda

unread,
May 25, 2010, 7:25:21 AM5/25/10
to golang-nuts
It has popular usage in programming contest.

For example, a problem has multiple test cases. For every case, I have
to populate an array.
If there are 10000 cases, I can define an array for 10000 times.
With memset, I can define only one array, and reuse it for 10000
times.
Maybe it's not important in general programming, but the problem often
limit memory usage.
That's why I guess so many constants use memset this way.

On 5月25日, 下午7时15分, chris dollin <ehog.he...@googlemail.com> wrote:

chris dollin

unread,
May 25, 2010, 7:45:54 AM5/25/10
to Lambda, golang-nuts
On 25 May 2010 12:25, Lambda <steph...@gmail.com> wrote:
With memset, I can define only one array, and reuse it for 10000
times.

With a for loop, too. Yes -- you may not get the very best of
optimised behaviour; but on the other hand, in Go, the loop

    for i, _ in range byteArray { byteArray[i] = 0 }

is something the compiler can recognise needs no bound
checks and can (I do not say does) generate a pretty compact
loop.
 
Maybe it's not important in general programming, but the problem often limit memory usage.

Other languages have for-loops too!

Chris

--
Chris "allusive" Dollin

Steven

unread,
May 25, 2010, 11:57:12 AM5/25/10
to chris dollin, Lambda, golang-nuts
On Tue, May 25, 2010 at 7:45 AM, chris dollin <ehog....@googlemail.com> wrote:
On 25 May 2010 12:25, Lambda <steph...@gmail.com> wrote:
With memset, I can define only one array, and reuse it for 10000
times.

With a for loop, too. Yes -- you may not get the very best of
optimised behaviour; but on the other hand, in Go, the loop

    for i, _ in range byteArray { byteArray[i] = 0 }

Blasphemer!

for i := range byteArray { byteArray[i] = 0 }

Of course, this particular loop would be useless, since the same thing happens when you do
var byteArray [10000]byte
or
byteSlice := make([]byte, 10000) 

Sonia Keys

unread,
May 25, 2010, 1:50:19 PM5/25/10
to golang-nuts
On May 25, 7:25 am, Lambda <stephenh...@gmail.com> wrote:
> ...
> For example, a problem has multiple test cases. For every case, I have
> to populate an array.
> If there are 10000 cases, I can define an array for 10000 times.
> With memset, I can define only one array, and reuse it for 10000
> times.

The built in function copy does exactly this.

chris dollin

unread,
May 25, 2010, 1:55:42 PM5/25/10
to Steven, Lambda, golang-nuts
On 25 May 2010 16:57, Steven <stev...@gmail.com> wrote:
On Tue, May 25, 2010 at 7:45 AM, chris dollin
    for i, _ in range byteArray { byteArray[i] = 0 }

Blasphemer!

for i := range byteArray { byteArray[i] = 0 }

Aiee! The Master Lasher has been summoned. The fingers
will be ... educated.


Of course, this particular loop would be useless, since the same thing happens when you do
var byteArray [10000]byte
or
byteSlice := make([]byte, 10000) 

If you're going to re-use an array, though, as the OP suggested ...

--
Chris "allusive" Dollin

chris dollin

unread,
May 25, 2010, 2:04:15 PM5/25/10
to Sonia Keys, golang-nuts
You'll need two arrays, surely; one for the initial value,
the other to be repeatedly copiedinto.

ebb

unread,
May 25, 2010, 3:47:23 PM5/25/10
to golang-nuts
To get the slice of initial values, you can use bytes.Repeat. For
example, for 100 bytes of value 10:

s0 := bytes.Repeat([]byte{ 10 }, 100)

Reply all
Reply to author
Forward
0 new messages