Private Messages Options Search Blogs Images Chat Cam Portals Calendar FAQ's Join  
Asylum Forums : Powered by vBulletin version 2.2.8 Asylum Forums > WIT - Whore Institute of Technology > Wisdom
Pages (2): [1] 2 »   Last Thread   Next Thread
Author
Thread [new thread]    [post reply]
Dacarlo
Militant Wankgnome

Registered: Oct 2000
Location:
Posts: 9140

Thumbs up Wisdom

The tao that can be tared
is not the entire Tao.
The path that can be specified
is not the Full Path.

We declare the names
of all variables and functions.
Yet the Tao has no type specifier.

Dynamically binding, you realize the magic.
Statically binding, you see only the hierarchy.

Yet magic and hierarchy
arise from the same source,
and this source has a null pointer.

Reference the NULL within NULL,
it is the gateway to all wizardry.

(Taken from Linux MotD)

Report this post to a moderator | IP: Logged

Old Post 12-06-2002 10:51 AM
Dacarlo is offline Click Here to See the Profile for Dacarlo Click here to Send Dacarlo a Private Message Find more posts by Dacarlo Add Dacarlo to your buddy list [P] Edit/Delete Message Reply w/Quote
Large Filipino
Fuck me hard in my arse.

Registered: Feb 2004
Location: in colorado somewhere!
Posts: 25591

wow....

__________________
EEEEEEEEEEEEE!!!!!

Report this post to a moderator | IP: Logged

Old Post 06-02-2005 02:17 AM
Large Filipino is offline Click Here to See the Profile for Large Filipino Click here to Send Large Filipino a Private Message Visit Large Filipino's homepage! Find more posts by Large Filipino Add Large Filipino to your buddy list [P] Edit/Delete Message Reply w/Quote
torque
SupaTwistyPowa!

Registered: Dec 2001
Location: Ducktown, GA
Posts: 1426

Commandment 2, from the C programmers bible.

Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end.

Clearly the holy scriptures were mis-transcribed here, as the words should have been "null pointer", to minimize confusion between the concept of null pointers and the macro NULL (of which more anon).
Otherwise, the meaning is plain. A null pointer points to regions filled with dragons, demons, core dumps, and numberless other foul creatures, all of which delight in frolicing in thy program if thou disturb their sleep. A null pointer doth not point to a 0 of any type, despite some blasphemous old code which impiously assumes this.


Last edited by torque on 06-10-2005 at 09:12 PM

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 09:09 PM
torque is offline Click Here to See the Profile for torque Click here to Send torque a Private Message Find more posts by torque Add torque to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

A null pointer doesn't point to anything, I thought (indeed, isn't that the point of them?), regardless of what the compiler uses to represent them internally.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 09:31 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

I think it's referring to the fact that NULL and a null pointer aren't interchangeble, ie. I can't use NULL when calling a fuction that uses a null pointer as it's terminater, as the compiler treats NULL as an unadorned 0.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 09:48 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

I got the bit about null pointer/NULL, but I was talking about :

quote:
...Otherwise, the meaning is plain. A null pointer points to regions filled with dragons, demons, core dumps, and numberless other foul creatures...

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 09:54 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

It's function dependant, but as an example: If I was using a particularly braindead malloc implementation and called free(void *ptr), where ptr is a null pointer, I could get some truely interesting behaviour(free() is normally used to free up a segment of malloc()'d memory, using the the pointer that malloc() returns after being called).

Thankfully free() in most versions of malloc checks for a NULL pointer and just returns if one is found. But you get the idea.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Last edited by macker on 06-10-2005 at 10:12 PM

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:08 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

To clarify a bit, how null pointers behave when read or written to is also architecture dependant.

edit: Actually, I don't think Java and C# allow pointer operations in general.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Last edited by macker on 06-10-2005 at 10:14 PM

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:11 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

They still don't point to anything at all, though, surely? Even if they are misinterpreted to appear as if they do?

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:12 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

Well, it's a pointer. Saying it points to nothing is, well, inaccurate. As such, what it points to is undefined. On some architectures it's 0(which might be protected(and will either throw an exception of some sort or just silently ignore the operation)), or it could the value of some data that *used* to sit the same place as what the null pointer resides in, or it could be a random bit of information from one of the cache lines in the CPU, etc.

edit: further clarification

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Last edited by macker on 06-10-2005 at 10:27 PM

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:22 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

I was under the impression that it really didn't point to anything, although it can be later assigned to point to something (a pointer is something that can point rather than something that does point, in that sense). Also that the internal representation of a null pointer was unimportant, just that it was guaranteed not to be the same as any pointer that does point to something.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:47 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Or I should perhaps say that a whatever a null pointer points to, it doesn't point to a memory location (this is what is guaranteed by the compiler)? In which case, perhaps one could make the pointer is pointing to dragon. Or kittens. Or anything that isn't a memory location.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 10:54 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

It points to undefined. Saying it doesn't point to a memory location would indicate that it is actually defined. Which it isn't.

Dunno how else to explain it.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 11:20 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Oh, I agree with you, I was just trying to see the logic/humour in the piece saying that they pointed to dragons, etc.

I don't think that saying that 'it doesn't point to a memory location' is the same as saying that it is defined, though, in logic. It means, in effect 'either it is undefined OR it does point to something defined, that isn't a memory location'.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-10-2005 11:54 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

Saying a pointer doesn't point to a memory location is like saying water isn't wet. All pointers point to a memory location, however null pointers don't necessarily point to any *explicit* location until you actually use them. Then for that specific operation they actually *do* have an explicit location. It might seem like stupid semantics, but it's important stupid semantics.

As for the humour, I think it refers to the fact that null pointers can create havoc far from where you expect them to and in ways that you cannot immediately explain. I might have seen where you were going except they included "core dumps" and "numberless" in the list, the former being a common symptom of null pointers, the latter being a reference to its nature.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 01:00 AM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Pointers hold a number that represents a memory location, surely? If that number (or whatever it is) doesn't correspond to a memory location that actually exists, then the pointer doesn't point to a memory location. As null pointers are promised not to be the same as any pointer that does point to a memory location, however they are internally represented, they aren't pointing to a memory location.

Picking on null pointers seems odd when stray pointers are more dangerous.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 01:20 AM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

quote:
Originally posted by Smug Git
Pointers hold a number that represents a memory location, surely?


Yes.

quote:
If that number (or whatever it is) doesn't correspond to a memory location that actually exists, then the pointer doesn't point to a memory location.


Who says the location doesn't exist? Internally the compiler will assign an arbitrary value(or more correctly "bit pattern") to represent a null pointer, which is 0x00000000 for most x86 compilers(I think. It's 0'ish). It will also ensure that no defined pointer will ever evaluate to that value(usually by treating the null as a kind of offset that all other pointer values will be offset by + 1). However, that address isn't necessarily explicity location 0x0(I can't be bothered to count 0's) in relation to the entire address space available on the PC. It might be location 0x0 in relation to the memory segment the pointer happens to be sitting in, 0x0 in the total malloc'd address space of the program, 0x0, 0x0 in the OS pagetable the program is currently sitting in, etc. In short, there is no explicitly set location of the null pointer 'cept that it's whatever the compiler decided to put it as, and subsequently how the OS/CPU/MMU decides to allocate the location of offset 0x0 for your piece of code.

And that's just for most common case x86 compilers. I think certain ancient incarnations of VAX use 0xc0000000 for nulls. Suddenly we get to repeat the whole of the previous paragraph for determinig where 0xc might point to. And it might be different on PowerPC, 6502, 68k, etc. In short, a null pointer's location cannot be defined.

And for extra spicy good funness you get processors like the Z8001 that actually has two bit patterns for NULL.

quote:
As null pointers are promised not to be the same as any pointer that does point to a memory location, however they are internally represented, they aren't pointing to a memory location.


You got the first part right, the second part wrong. It merely states that a null pointer will always be different to a non-null pointer when used to evaluate a statement. The evaluation is actually done on the state of the pointer itself rather than the value of the pointer, when one of the pointers in the condition is null. In other words, the code internally just takes a shortcut.

quote:
Picking on null pointers seems odd when stray pointers are more dangerous.


*shrugs* We're fairly insulated from the dangers of null pointers with high level code. It doesn't mean they've gone away and they're a common source of problems with sloppily written low level stuff like drivers or embeded applications where you might not have the privilege of an MMU(which in and of itself can provide certain protections against null pointers, but only so far as to make them fairly non-fatal to the health of your hardware).

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Last edited by macker on 06-11-2005 at 02:29 AM

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 02:26 AM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

I read a bit more on this, and it is certainly said that pointers are guaranteed not to point to anything 'real'; I guess that it depends on what you mean by 'point to'. As we can't say anything general about the internal representation of the pointer (as it varies) I'd take the definition that it doesn't, in fact, point to anything real. That it can cause problems if it is treated as pointing to something real doesn't, to me, mean that it is pointing to anything real anymore than a map of Middle Earth maps a real place, even though if you treat it as real and use it to navigate, it's going to cause you problems.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 12:20 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

And on the idea that the idea that a pointer, in general, has to point to something, if there exists a number I can put into a pointer that doesn't represent anything real (regardless of whether, on my machine, a null pointer fits this bill) then we can have a pointer that doesn't point to anything. Whether or not a null pointer is that is a different matter, but I'm not at all convinced by your statement that it is 'inaccurate' to say that a pointer points to nothing ('nothing' as meaning 'no-thing').

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 12:25 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

quote:
Originally posted by Smug Git
I read a bit more on this, and it is certainly said that pointers are guaranteed not to point to anything 'real'


Guaranteed not to point to any real data of size null, yes. However the address 0x0 is real, and can often contain something.

Pointers will never be assigned a non-real address in C. A pointer will always point to a valid memory address, however the contents of that address in the case of a null is not guaranteed in any way. The fact that that address might not have any real value to you as a programmer or the program(beyond meaning that it's a null pointer for evaluation purposes) is utterly irrelevant. It's real to your computer, and if you start stomping around in there expect things to happen.

quote:
As we can't say anything general about the internal representation of the pointer (as it varies) I'd take the definition that it doesn't, in fact, point to anything real.



I cried when I saw that sentence. The first half is perfect but you jump to the wrong conclusion.

quote:
That it can cause problems if it is treated as pointing to something real doesn't, to me, mean that it is pointing to anything real anymore than a map of Middle Earth maps a real place, even though if you treat it as real and use it to navigate, it's going to cause you problems.


A null pointer is real. Uniquely real sure, but real nontheless. It points to a real place, however that place has no value as the compiler internally treats the null pointer as being of zero size. Which is why if you try to write or read data from a null pointer you can get unexpected results, because your read/write will extend past zero.

If a null pointer truely pointing to nothing, you would never get any data back from it. And you would never be able to write any data to it. Neither of which is the guaranteed case, hence the undefined nature of null pointers.

You're right in that it points to 'no-thing' or 'no-object', however that concept only applies to your piece of executable code. Beyond your code, that 'no-thing' is actually a 'thing' that might be terribly unhappy if you start throwing bits at it.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 07:13 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

Are you saying that every possible number that can be the pointer 'is something'? I can accept that outside of my executable it might point to something (depending on the machine, etc) but is that guaranteed? In any case, I was talking about the context of the executable in any case.

If a null pointer pointed to nothing, I don't see why that would mean that you couldn't check against it, or change it (redirect it, from pointing at nothing to pointing at something). The null pointer is itself real, as a map of Middle Earth is real. What it depicts/points to might not be real, though.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 06-11-2005 07:18 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

quote:
Originally posted by Smug Git
Are you saying that every possible number that can be the pointer 'is something'?


Well yeah. A pointers size is consistant(usually <= total address space) for that executable, and any libraries it uses. Which is why you can't link a 32 bit binary against a 64 bit library. Or link a 32 bit compiled object file with a 64 bit object file and expect to create a valid executable.

What happens if you put a 64 bit address into a 32 bit pointer is usually platform and compiler specific but it'll generally either be an immediate crash or the address will get truncated to fit the smaller pointer. And if a put a 16 bit address into a 32 bit pointer, it'll generally just be padded, the nature of the padding being dependant on the endianess of the platform.

quote:
I can accept that outside of my executable it might point to something (depending on the machine, etc) but is that guaranteed?



There's no "might" about it. It's pointing to something. And the "no object" statement should have had the caveat "as long as object is treated as zero in size". As soon as you start treating a null pointer as an object of non-zero size, things go wrong because a null pointer is still pointing to an actual place in the memory address space.

Which is why a null pointer can be used in statement evaluations: I'm comparing a pointer with a normal address with a pointer that contains the address pattern I normally associate with null. Therefore the evaluation will immediately fail/pass, because null is supposed to have no value. Otherwise, if both pointers contain non-null addresses, evaluate normally by fetching the pointee values if necessary.

However, there is nothing in C itself that'll say: This pointer has an address I normally associate with null, I won't allow you to do anything because C trusts the programmer implicitly and he might actually be intentionally be writing data to that address. Co C standard defines what bit pattern null pointers actually are, so stopping you from writing to said pattern would result in non-por