Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #88 Allocate 1M bytes

Create a new bytes buffer buf of size 1,000,000.

var buf = new byte[1000000];
type Byte is range 0 .. 255
  with Size => 8;
      
type Byte_Array is array (Positive range <>) of Byte;  
      
Buf : Byte_Array (1 .. 1_000_000);
#include <stdlib.h>
void *buf = malloc(1000000);
vector<byte> _buf(1'000'000);
import std.c.stdlib;
import core.memory;
void[] buf = GC.malloc(1024 * 1024)[0..1024 * 1024];
import std.c.stdlib;
import core.memory;
void[] buf = malloc(1024 * 1024)[0..1024 * 1024];
import 'dart:typed_data';
var buf = BytesBuilder().addByte(1000000);
  use iso_c_binding, only : c_int8_t
  integer(kind=c_int8_t), dimension(:), allocatable :: a

  allocate (a(10**6))
buf := make([]byte, 1_000_000)
import Data.Vector.Unboxed.Mutable as V
import Data.Word                        (Word8)
createBuf :: Int -> IO (V.IOVector Word8)
createBuf n = V.new n

main :: IO ()
main = do
  buf <- createBuf 1000000
  return ()
let buf = new Buffer (1e6)
byte[] buf = new byte[1000000];
byte buf[] = new byte[1_000_000];
byte buf[] = new byte[(int) 1e6];
$memStart = memory_get_usage();
$buf = str_repeat("\x00", 1000 * 1000);
$memEnd = memory_get_usage();
echo $memEnd - $memStart, PHP_EOL;
var
  buf: Pointer;
begin
  buf := GetMem(1000000);
end;    
my $buf = ' ' * 1_000_000;
buf = bytearray(1000000)
buf = (' ' * 1_000_000).bytes
let buf: Vec<u8> = Vec::with_capacity(1000000);

New implementation...
< >
programming-idioms.org