#what is happening ?
1 messages · Page 1 of 1 (latest)
(in this case from -2,147,483,647 to 2,147,483,647)
what does that have to do with the infinite loop ?
i think it's a real small one actually
so this is running forever bc it's always less than 1
Well, is a big number just because is far from 0
but it won't actually run forever, just "forever"
the magnitude is big
Either way
Yes, it will run till it reaches 2
which is a lot of iterations
You can probably see this by printing the start
so you say undefined it will start with - ?
Is whatever value, in debug mode for ease of debugging is 0xAAAAAA
wait wait wait it doesn't make sense to me it is -1 but i am incresing it should go up
my understanding is undefined means "don't set this value to anything" - i.e. it'll be whatever value the memory occupying that space was before you allocated it
indeed
Yes, but is a big number
hard to understand are you referring to the type size ????????
0xaaaa < 1 if you interpret 0xaaaa as a signed 32-bit int
(the point is that the sign bit is set)
Clarifying question, is any undefined set to that?
in debug mode, yes
see more here: https://zig.news/kristoff/what-s-undefined-in-zig-9h
has nothing to do with size. Has everything to do with how memory (and stack allocation I guess) works
undefined in debug is -1563262968
in i32
undefined is 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA... fitted to the size of integer you are using
-1563262968 to 1 is a lot of iterations that might seem like forever, specialy while printing
no impossible
it depends on the type size
the value of what undefined ends up being does depend on the type size, but that value isn't really relevant to what undefined is
my understand to the keyword is undefined means the type size
that's wrong
what the value of undefined is is related to the representation
But what undefined is is not
Undefined is the absense of being defined, in debug it has a value given that is easier to debug that way
on release modes it will have the value that memory space have, sometimes is 0, sometimes is not
coincidence
which representation ?
the fact that undefined happens to be 8 with a i8 is entirely coincidence
Use undefined to leave variables uninitialized:
assign_undefined.zig
const print = @import("std").debug.print; pub fn main() void { var x: i32 = undefined; x = 1; print("{d}", .{x}); }Shell
$ zig build-exe assign_undefined.zig $ ./assign_undefined 1undefined can be coerced to any type. Once this happens, it is no longer possible to detect that the value is undefined. undefined means the value could be anything, even something that is nonsense according to the type. Translated into English, undefined means "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."
In Debug mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger. However, this behavior is only an implementation feature, not a language semantic, so it is not guaranteed to be observable to code.
ok i understand when i use undefined means the variable that i am using undefined with will be asigned to 0xaaa depending on the type size right ?
but
where are those random minus numbers coming from ?
sorry it is confusing a little bit
because the value is undefined, it can and will be anything
if you dont want it to be undefined
give it a value
it has nothing to do with type size
negative numbers are a lie
more like
negative numbers use a representation form that let us decide in binary what is a negative number
how a lie explain where are they coming from ?
they are coming from the value that was previously there
which can and will be random in most cases
in debug mode it wont be random
but in release modes it will
if the number that the memory held before happens to match what a negative number representation, it will be negative
ok ok this makes sense
either way: you are using undefined wrong, you should be defining the value to be "0" or something
yep
anytime you read undefined, you are doing something wrong
the point is undefined is whatever and shouldn't be used
so basically undefined assigns 0xaaa to a variable and this address was assigned to something right ?
read the text I sent
In Debug mode, Zig writes 0xaa bytes to undefined memory. This is to catch bugs early, and to help detect use of undefined memory in a debugger. However, this behavior is only an implementation feature, not a language semantic, so it is not guaranteed to be observable to code.
aaaaah confusing how is it undefined while it is generating values
please read the documentation
undefined is whatever, it's whatever, the documentation says so, it doesn't have a real value =V
quote from the documentation: "Not a meaningful value. Using this value would be a bug. The value will be unused, or overwritten before being used."
when you get memory, almost never is clean, so it has the value it was there before
But the text like it says, undefined means is not a meaninful value, it's a value that make no sense to use before asigning it to something
well the docs are good but it is missing some points
Signed numbers use the top bit of an otherwise unsigned number to indicate if it's negative or not.
(It's called Two's Complement)
Not sure if that's what you meant
But worth pointing out nonetheless
0xaaaaaaaa has the top bit set, for instance.
But indeed, in release modes, it really will be whatever the memory had in it already.
And that doesn't have any guarentee about it at all
Thus, it can be any value
undefined is generally useful for times where you are literally about to overwrite its value anyway, and only want to declare its existent right now.
Memory allocators also set freed memory to the same 0xaaaaaaaa pattern to aid with debugging too.
You also can find that .deinit() functions will overwrite the structure with 0xaaaaaaaas for similar reasons too.
the docs arent missing anything, they explain it well
Perhaps a better question is: What things do you think it's missing that you'd want it to mention?
Or, How would you improve it?
i have not seen it mentioning the value being generated that is coming out of the blue
from the website:
In Debug mode, Zig writes 0xaa bytes to undefined memory.
When you compile in debug and set a value to undefined, it'll be filled with 0xAAAA... (all the bytes will be set to 0xAA). this is an intentional decision to make it easier to spot errors
In release, nothing actually happens and nothing is set - if you access the value without defining it, all bets are off and you'll be interacting with dirty memory
undefined is used as a placeholder. The value is not strictly defined at runtime (in release). The compiler reserves some space on the stack for the variable and nothing more, meaning that the variable's value can be anything that was at its place in memory beforehand - most likely junk. Hence the name undefined - the value is not defined by a known rule when created and cannot be dependent on, otherwise it's an undefined behavior.
I've always kinda thought that the keyword should be uninitialized or something similar, as I've seen quite a lot of people confuse it - and there's not really a good reason to make it abstract.
But yeah - no value is being generated there.
It was just already there from some previous operation.
Then again, it is initialized, just not to a fixed default value or a value of your choosing. So it is literally undefined. Maybe call it "unspecified".