0

To start off, i know what null void and not defining something do for the code, but a computer only knows binary. I can't tell it to do something i imagined and did not define in one way or the other. So lets assume the compiler would't get a heart attack of what i am trying to make it do - what would these examples look like in assembler or another way of showing the thing behind the keywords:

private int number;

public void method() {}

public int main() {
    int i = 1 * null;
    int j = 1 * number;
    int k = 1 * method();
}
3
  • 2
    What language is this? What semantics do you hope this code will have, that you want to implement in asm? If you want values in assembly that can be "undefined", like perl unset $foo, you need to implement that yourself. Like C++ std::optional (en.cppreference.com/w/cpp/utility/optional), like an <int, bool> pair of values. An int in most languages must be a number, not some "oops I'm not actually an int" indicator, although rare ISAs do have trap values. IDK what you want 1 * method() to do; it doesn't return anything there's nothing to multiply or assign to an int. Commented Dec 2, 2020 at 15:38
  • Or to formulate otherwise how are those three implemented in C and represented in binary (bcs if it's in std it has to be saved somehow, at least void and null need to be) Commented Dec 2, 2020 at 18:42
  • For the undefined variable null, it's like asking "What is the French translation of 'qhwudhwiud'?" You can't "implement" something that doesn't have any meaning in the first place. Saying "let's assume the compiler didn't choke" doesn't help unless you also add assumptions about what it is going to do instead, which takes the question far away from reality. Commented Dec 3, 2020 at 16:39

1 Answer 1

2

C has no concept of a void value; that's an error. void* is just a compile-time-only placeholder for "unspecified, cast before using". i.e. a generic pointer value. It would be meaningless to dereference it in C or in asm, that's why compilers reject it.

An int is fixed width and uses all the bits to represent a number's value, like the simple / normal way of using a register in assembly. It can't hold anything else to indicate "null" or "undefined". If you want something like perl where you can undef $foo, you'd want something like C++ std::optional, like an <int, bool> pair of values. An int in most languages must be a number, not some "oops I'm not actually an int" indicator.

(Although rare ISAs do have trap values, e.g. one's complement machines that disallow the all-ones bit-pattern for signed integers, instead of having it work as a negative 0. But on most ISAs only floating-point values can be NaN - a special bit-pattern that means "not a number".)


C also doesn't have a null keyword. It has NULL as a preprocessor macro (which can be #defined as ( (void*)0 ) or even just 0). (Fun fact: ISO C actually allows implementations to use any bit-pattern they want as the runtime value of a null pointer, with compile-time translation of integer 0 in a pointer context to that bit-pattern.) But all normal implementations on modern CPUs just use 0. Like mov x0, #0 (AArch64). So at least one part of you question does have an answer.


So basically you can't do any of these things in C because there literally isn't any meaning to them, and you can't do them in asm either. There isn't some layer of magic underneath which C compilers are denying you access to.

Write C that can actually compile and look at the asm output (https://godbolt.org/ and How to remove "noise" from GCC/clang assembly output?)

1
  • Thank you for the answer that explains it very well. The reason i asked this question in the first place was that i had to explain this to a friend but couldn't tell him more than Well it just doesn't work ^^ Actually i use godbolt every day because i really like writing much used methods in assembly to optimize them if they're used a few thousand times in certain code. But since the code above is just plain madness i couldn't show him what it would (not) do. Commented Dec 3, 2020 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.