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: Pointer;
begin
  buf := GetMem(1024 * 1024);
end;    
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);
import std.c.stdlib;
import core.memory;
void[] buf = malloc(1024 * 1024)[0..1024 * 1024];
import std.c.stdlib;
import core.memory;
void[] buf = GC.malloc(1024 * 1024)[0..1024 * 1024];
buf := make([]byte, 1000000)
byte[] buf = new byte[1000000];
$memStart = memory_get_usage();
$buf = str_repeat("\x00", 1000 * 1000);
$memEnd = memory_get_usage();
echo $memEnd - $memStart, PHP_EOL;
my $buf = ' ' * 1_000_000;
buf = bytearray(1000000)
let buf: Vec<u8> = Vec::with_capacity(1024 * 1024);

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org