Gamestudio Links
Zorro Links
Newest Posts
ZorroGPT
by TipmyPip. 04/25/26 16:09
Z9 getting Error 058
by jcl. 04/24/26 17:48
Stooq now requires an API key
by jcl. 04/13/26 09:42
Strange "Alien" Skull created with >Knubber<
by NeoDumont. 04/10/26 18:58
400 free seamless texture pack downl. here !
by NeoDumont. 04/08/26 19:55
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
2 registered members (Grant, TipmyPip), 3,403 guests, and 11 spiders.
Key: Admin, Global Mod, Mod
Newest Members
valino, juergenwue, VladMak, Geir, ondrej
19209 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
0 == NULL ??? #278143
07/12/09 11:33
07/12/09 11:33
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline OP
Serious User
pegamode  Offline OP
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
Hi,

is it true that NULL is defined as 0 ??? (was stated in another thread)
If it is so then why is it so?

NULL should be NULL and 0 should be 0.

Regards,
Pegamode.

Re: 0 == NULL ??? [Re: pegamode] #278193
07/12/09 15:56
07/12/09 15:56
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
litec.h:


#define NULL 0

Re: 0 == NULL ??? [Re: FBL] #278202
07/12/09 16:26
07/12/09 16:26
Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
Cowabanga Offline
Expert
Cowabanga  Offline
Expert

Joined: Aug 2008
Posts: 2,838
take me down to the paradise c...
@Firoball:
If that's right, then we'll have to write 250 like this:
25NULL

Correct me if i'm wrong, because AFAIK defines is applying on everything.

Re: 0 == NULL ??? [Re: FBL] #278209
07/12/09 16:49
07/12/09 16:49
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline
Serious User
MichaelGale  Offline
Serious User
M

Joined: Aug 2005
Posts: 1,230
NULL should be defined as pointer to a memory address like this:

#define NULL ((void*)0x00000000)


Your friendly mod is at your service.
Re: 0 == NULL ??? [Re: MichaelGale] #278242
07/12/09 19:35
07/12/09 19:35
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Originally Posted By: Cowabanga
@Firoball:
If that's right, then we'll have to write 250 like this:
25NULL

Correct me if i'm wrong, because AFAIK defines is applying on everything.
Cowabanga: You COULD always look in LITEC.H yourself and see... blush
25NULL wont work but "(25-NULL)==25" does work.

MichaelFriedrich: AFAIK though, what is the actual difference between "((void*)0x00000000)" and "0" if there is no type attached?
(besides the type that is implied by its location in the final code)
The way I see it, the #define NULL 0 is bypassing any typecasting, just by being a 'define'.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: 0 == NULL ??? [Re: EvilSOB] #278246
07/12/09 19:43
07/12/09 19:43
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline
Serious User
MichaelGale  Offline
Serious User
M

Joined: Aug 2005
Posts: 1,230
@EvilSOB: The difference is that if you assign ((void*)0x00000000) to a pointer, the pointer will point to an empty memory address and therefore the pointer will indeed be invalid as it points to 'nothing'. However, if you assign 0 to a variable, you will obviously set the value of that object to 0 which is 'something'. That's in simple terms though. The problem is that #define NULL 0 is typeless. Therefore the compiler won't know what it actually works with. If you assign this typeless object to something else, the compiler will most likely assume that both sides of the operation have the same type. And therefore the compiler will assign a value that equals 0 in binary to the object. However, if you tell the compiler explicitly that you have a pointer that points at nothing, the compiler can set the object to NULL.


Your friendly mod is at your service.
Re: 0 == NULL ??? [Re: MichaelGale] #278257
07/12/09 20:32
07/12/09 20:32
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
i cant ACTUALLY dis-agree with any of that. BUT
Quote:
if you assign ((void*)0x00000000) to a pointer, the pointer will point to an empty memory address and therefore the pointer will indeed be invalid as it points to 'nothing'.
True, but if you assign 0 to a 'pointer', it will be expecting an address because its a pointer, not a value.
So it will look for (implied)address (0) and find the same as ((void*)0x00000000).


Quote:
Therefore the compiler won't know what it actually works with
Yes, but I would re-phrase this as "...the compiler won't CARE what it actually works with..."


Quote:
the compiler will most likely assume that both sides of the operation have the same type
No. The compiler WILL assume that both sides have the same type as the LEFT side of the operation.
Again, its a programming 'standard'(?) called "implied typecasting".


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: 0 == NULL ??? [Re: EvilSOB] #278263
07/12/09 20:40
07/12/09 20:40
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
This discussion has been there before.

Re: 0 == NULL ??? [Re: EvilSOB] #278265
07/12/09 20:49
07/12/09 20:49
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline
Serious User
MichaelGale  Offline
Serious User
M

Joined: Aug 2005
Posts: 1,230
Quote:

Quote:

the compiler will most likely assume that both sides of the operation have the same type

No. The compiler WILL assume that both sides have the same type as the LEFT side of the operation.
Again, its a programming 'standard'(?) called "implied typecasting".

True, that's kinda what I meant. However, if you imagine a situation like this:

Code:
if(null == some_object)



it's the left hand operator that has no type wink

Regarding the first part, I guess a major role here are the differences between the ways different compilers handle situations like this.

#define NULL 0

replaces all occurrences of NULL with 0. When a compiler translates that to machine code, it will store that 0 somewhere in the data section of the executable which is eventually stored at some memory address. Therefore some compiler may follow this logic and treat it as data stored at some memory address that is then referred to in the pointer or other compilers may follow your logic and treat it as a memory address. Maybe Lite-C does it that way.

However, from my experiences in operating system development, a 0 would often not be sufficient and had to be replaced ((void*)0x00000000) to work correctly (using GCC).


Your friendly mod is at your service.
Re: 0 == NULL ??? [Re: MichaelGale] #278271
07/12/09 21:18
07/12/09 21:18
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Unfortunately, you chose a bad example, but feel free to try others.
Im enjoying this discussion.

This example is bad because the IF is a purely numerical comparison.
So in this case null is replaced (by the compiler define) as a literal zero.
This zero is then type-cast by the compiler to type 'int' cause its an integer and a literal.
This now (int)0 is compared (==) against some_object.
To do this, some_object is now recast to a type 'int' as well.
So if it was a pointer, it now has a re-cast value equivalant to (int)(addressof)some_object.
So if some_object was = ((void*)0x00000000), it now has a value of (int)0.

So therefore,
If some_object was a valid pointer, the IF returns false, as expected.
If some_object was a NULL pointer, the IF returns true, as expected.

or, if you already knew that some_object was a variable rather than a pointer
If some_object was a variable equal to zero, the IF returns true, as expected.
If some_object was a variable not equal to zero, the IF returns false, as expected.

See why it was a bad choice of example.


Quote:
However, from my experiences in operating system development, a 0 would often not be sufficient and had to be replaced ((void*)0x00000000) to work correctly (using GCC).
I suspect that a compiler capabable of generating an operating system is very likely
to be much more low-level and stringent than a (relitivly) simple game-engine.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 1 of 3 1 2 3

Moderated by  old_bill, Tobias 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1