From 5ea973dc39cc1b3a9826ef94affb33e41c02c733 Mon Sep 17 00:00:00 2001 From: XXIV <13811862+thechampagne@users.noreply.github.com> Date: Sat, 4 Nov 2023 13:17:44 +0300 Subject: [PATCH] langref: fix malloc return type --- doc/langref.html.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 7e4d25a2fa..83215b59ea 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6201,7 +6201,7 @@ struct Foo *do_a_thing(void) {

Zig code

{#syntax_block|zig|call_malloc_from_zig.zig#} // malloc prototype included for reference -extern fn malloc(size: usize) ?*u8; +extern fn malloc(size: usize) ?[*]u8; fn doAThing() ?*Foo { const ptr = malloc(1234) orelse return null; @@ -6210,7 +6210,7 @@ fn doAThing() ?*Foo { {#end_syntax_block#}

Here, Zig is at least as convenient, if not more, than C. And, the type of "ptr" - is {#syntax#}*u8{#endsyntax#} not {#syntax#}?*u8{#endsyntax#}. The {#syntax#}orelse{#endsyntax#} keyword + is {#syntax#}[*]u8{#endsyntax#} not {#syntax#}?[*]u8{#endsyntax#}. The {#syntax#}orelse{#endsyntax#} keyword unwrapped the optional type and therefore {#syntax#}ptr{#endsyntax#} is guaranteed to be non-null everywhere it is used in the function.