Created
May 24, 2017 11:57
-
-
Save markuswustenberg/d912486c25989ccd325e043aa9e2c612 to your computer and use it in GitHub Desktop.
Malloc program in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func main() { | |
var mib int | |
var answer string | |
for { | |
fmt.Println("How much memory, in MiB?") | |
fmt.Scanln(&answer) | |
var err error | |
mib, err = strconv.Atoi(answer) | |
if err != nil { | |
fmt.Println("Not a number, try again.") | |
continue | |
} | |
break | |
} | |
b := mib * 1024 * 1024 | |
arr := make([]byte, b) | |
for i := range arr { | |
arr[i] = 1 | |
} | |
ignore(arr) | |
fmt.Printf("Allocated %v bytes.\n", b) | |
fmt.Println("Press enter to exit.") | |
fmt.Scanln(&answer) | |
} | |
func ignore(_ []byte) { | |
// Nothing | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment