Live data from Hacker News

*(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

youtube.com

191–199 of 199 posts

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#191
post #99

Earlier quoted context omitted.

> sizeof(char) (== 1 almost everywhere) sizeof char is 1 by definition everywhere. /pedantic

In the mathematical sense of almost, a property that holds everywhere does qualify as holding almost everywhere.

[deleted]

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#192
post #99

Earlier quoted context omitted.

> sizeof(char) (== 1 almost everywhere) sizeof char is 1 by definition everywhere. /pedantic

In the mathematical sense of almost, a property that holds everywhere does qualify as holding almost everywhere.

Guess can be taken as a shortened explaination of what a programming language committee is tasked with making happen. Likely why Lisp so successful/useful.

-----

unless initial property is start of dynamic operation, in which case, holding almost anywhere begins at the first operation after the start of the dynamic operation. process / lambda / epsilon calculi is just symbolic math. address 0 static, everything else dynamic.

per math, dimension N is static, to be able to "change things up" in dimension N, need to to be almost everywhere higher than dimension n. Edge cases are weird in any dimension. Guess why logicians just do the equivalent of C's !0

(cast classic logic) A=1 (cast boolean logic) B=0

C statement !(!B == A) hold everywhere and almost everywhere depends on how read C spec to interpret A & B.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#194

Earlier quoted context omitted.

This is C code. Coder needs specifity hwo to align the char pointer to HW alignment expectations for memory address to prevent a segmentation fault.

Char pointers have alignment 1 on any conforming implementation and all pointers are aligned at 0. The code will write to "address 0", assuming the compiler hasn't freaked out because (char*)0 is the char null pointer and thus undefined behavior to access. Note the additional caveat that address 0 may not be the first 8 bits of mapped memory (which isn't necessarily addressable).

[deleted]

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#195

Earlier quoted context omitted.

This is C code. Coder needs specifity hwo to align the char pointer to HW alignment expectations for memory address to prevent a segmentation fault.

Char pointers have alignment 1 on any conforming implementation and all pointers are aligned at 0. The code will write to "address 0", assuming the compiler hasn't freaked out because (char*)0 is the char null pointer and thus undefined behavior to access. Note the additional caveat that address 0 may not be the first 8 bits of mapped memory (which isn't necessarily addressable).

This is no-checks C. The C compiler has no run-time power. Oh wait, wasn't specified if statement was C++ or the C++ subset of C.

The code can do something shifty at run time, without a power surge, to the address reference before the address is de-referenced

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#196
post #134

The intention could be to force the program to execute its exception handler

Ah, why punch card readers died out -- not paying attention to endianness[0] and aligning the punch card feeder/reader perpendicular to normal handler plane instead of parallel.

Pointers and type safety still causing issues decades later.

[0] : https://en.wikipedia.org/wiki/Endianness

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#197
post #156

Earlier quoted context omitted.

No, it’s not a guarantee, it’s just a noisy filter with a very high false negative rate and a high enough false positive rate (as visible in malpractice insurance).

I certainly is, those numbers would be much higher without that filter.

I don’t think you know what “guarantee” means. If it was a guarantee, malpractice insurance wouldn’t exist.

Re: *(char*)0 = 0; – What does the C++ programmer intend with this code? [video]

#199
post #118

Earlier quoted context omitted.

You're mostly correct but to be pedantic, in C the literal value 0 does not actually mean address 0. The literal value of 0 is an implementation defined value that represents a null pointer, but that value does not have to be address 0, it could be some other address. This is significant because the following two snippets of code are not required to be equal in C. char* c = (char*)(0); char* d; memset(&d, 0, sizeof(d…

> that value does not have to be address 0, it could be some other address. That's what the spec says, but are there any computers around anymore where nullptr is not zero? The spec should really go with the times IMHO, old hardware can be supported via platform-specific language extensions.

The big place where it happens in modern environments are for pointers to members, where 0 is a bad nullptr value because the member at index 0 is legitimately a thing you want to name. MSVC uses -1 for nullptr in such cases.
Post reply on HN