#hidden control flow

1 messages · Page 1 of 1 (latest)

gray coral
#

I built hello world from the first example language reference.
const std = @import("std");

pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, {s}!\n", .{"world"});
}

I was stepping through the code when this line jumped into memset:

var bytes_written: DWORD = undefined;

This is from WriteFile in windows.zig in the standard library.

I thought zig didn't have any hidden control flow like this.

Why is setting a value to undefined causing memset to be called?

merry rover
#

Undefined sets bytes to 0xaa in debug mode in order to help catch bugs

bleak plinth
#

in all safe build modes (debug and releasesafe), not just debug

gray coral
#

the webpage should be updated to say it has hidden control flow in some cases.

bleak plinth
#

memcpy and memset are often called implicitly to move data around, because the alternative would be to inline the implementation in thousands of places and massively bloat your binary. e.g. a function like the following:

const S = struct { bytes: [4096]u8 };
fn copyABigStruct(dst: *S, src: *const S) void {
    dst.* = src.*;
}

will also probably call memcpy

bleak plinth
#

also it looks like your example was compiled in debug mode? since bytes_written is only a u32, i would expect that the same code in ReleaseSafe would just initialize it to 0xAAAAAAAA inline rather than calling memset. i don't see a reason to care about whether data is moved by memcpy/memset calls or by explicit movs.

full plaza
bleak plinth
#

one could argue asm includes hidden control flow too, e.g. a memory access can cause a page fault which could cause the kernel to perform disk or network i/o, or could send a signal to your process which allows you to execute arbitrary code and choose whether or not to resume after the memory access

gray coral
#

It's fine with me. I just wouldn't make the claim that there's no hidden control flow when there is.

full plaza
#

the real question is not "oh zig have hidden control flow" but what do you consider hidden

#

and any functionnal brain could determine, that a "debug"/"safe" mode, means: more control flow

#

so to me, these are not hidden.

bleak plinth
full plaza
#

i agree

gray coral
#

lol, webpage should be updated to say Zig has no hidden control flow in spirit.

full plaza
#

if something is obvious, how could it be hidden ?

gray coral
#

I'm probably not that smart and I'm new to the language. I saw a var set to undefined and assumed that meant it would explicitly be left uninitialized.

tacit escarp
full plaza
viscid nest
#

undefined just means it's undefined what happens if you use it, doesn't mean uninitialised

#

so the compiler is free to do whatever with it

bleak plinth
viscid nest
#

i'll be damned, it says it's uninitialized

gray coral
#

I see, thanks. I did not expect a branch to memcpy being inserted by the compiler is all.

viscid nest
#

i'm with OP then, it's clearly initialized to 0xAAs

bleak plinth
#

e.g. copying it around

viscid nest
#

that said, zig's docs are heavy WIP

bleak plinth
# full plaza being new does not justify being arrogant

"arrogant" is a bit harsh, i think. let's be welcoming and remember zig's documentation is still incomplete. it's not unreasonable for people to apply their expectations from other languages to zig, and the best response in that case is just to gently correct them.

gentle fractal
viscid nest
tacit escarp
#

the docs are saying a logical usage of the value is a bug (i.e branching on it)

bleak plinth
#

^^^ that's my interpretation too

viscid nest
#

that's some heavy interpretation

#

i'm not very used to this area of programming so it's probably something commonly understood but not very clearly conveyed in the docs then

full plaza
#

‘Not a language semantic’ clearly indicates that the compiler doesn’t treat this value as is and won’t guarantee that this code behaves predictably. Branching based on this value could likely cause bugs, as the compiler doesn’t ensure its consistency. Therefore, using this value logically results in undefined behavior, which can legitimately be considered a bug.

full plaza
#

hey @gray coral, I want to apologize for my earlier message. I interpreted this "initiative" as if you were focused on proving a point. When I mentioned arrogance, I was trying to express how it can sometimes feel like assumptions are made about how things should work without taking a step back to consider different perspectives.

It could be that the language barrier (English isn’t my first language) makes me interpret things more literally than intended. I hope you can understand.

#

anyway, this isn't the death of anyone, have a great day or a great night I absolutely have no clue from where you are

wary timber
echo gulch
#

Zig is not 1.0, so just not optimized everywhere yet.

lament copper