Live data from Hacker News

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

youtube.com

1–10 of 199 posts

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

#3
Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)...

I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again.

Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the value at that address, you'll get a non-zero and then run code you didn't intend to run. By defining address 0x0 as value 0, you'll avoid this issue. That has two problems.

Alternatively, you define address 0x0 to erase the initial stack pointer so that a nefarious actor can't somehow memory dump and then find out where the program starts its execution. I don't see the benefit for several reasons.

-----

Scenario 1.

The first problem is that you shouldn't write code poorly. If you meant to check a variable's address and instead check its value, that's on you, your test suite, your compiler, and your debugger (and by extension, your colleagues and educators).

Second, writing to address 0x0 on a Cortex usually involves writing to flash... which is not a temporary/nonpersistent change, more involved than simply writing to an address (you usually need to enable the flash controller and set some peripheral registers), and would usually not be allowed on a production device because that's where the bootloader is or that section of memory has been write protected following good practices.

Scenario 2.

The more I think about this one, the less it makes sense. Anyone with debug port access or a memory dump is already inside the castle, well past any defenses e.g. zeroing the main stack pointer. Plus, the same caveats from Scenario 1 apply: if you COULD write to 0x0, it is almost always a terrible idea.

-----

Grand takeaway? This is probably a talk relevant to PC and not embedded. A Cortex-M would definitely complain about this code. Even on an AVR (atmega) changing 0x0 would be changing R0. I don't even know if that register is directly writeable during execution---probably because it's how you set some bootloader bits---but it's definitely dangerous.

With that in mind, I searched for "virtual address space windows" to figure out what goes in an executable at address 0x0. After reading Microsoft's first two articles, it's still unclear what goes in 0x0. Wikipedia? No answer. Next result? Nothing.

Finally, a page about Windows ME/98/95 states address 0x0 is "available to the process" but is "not writable".

After ten more minutes of fruitless searching, I'm willing to just test what happens, because I expect that's not even writable without side effects during execution.

    #include 
    int main() {
        *(char*)0 = 0;
        std::cout 
and then, "g++ test.cpp -o test", and ...

[1] 869 segmentation fault ./test

Linux? Fail.

-----

"cl test.cpp /link /out:test.exe"...

This compiles and runs but does not display the test string. (If you remove the 0x0 assignment, the string displays; no surprise)

Interestingly, I can't delete test.exe right away. It has stayed open:

ERROR: The process with PID 12776 (child process of PID 19728) could not be terminated.

Not only does PID 19728 not exist, I have to use an administrator console to "taskkill test.exe", so this would probably never belong inside user space code on Windows.

-----

Thus, this short code snippet seems like trouble, and the answer I would hope for as an interviewer is, "yeah, I don't write code like that, sorry."

(Coincidentally, I would say the same in response to the professors giving microcontroller exam questions that try to stump students with multiple casts and dereferences in a single line *ahem Moreno*. Stop teaching students methods they will never, ever use in a shop that cares about its code!!)

Now I watch and learn.

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

#4

Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)... I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again. Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the…

Ok, suspicion confirmed after watching: The code snippet itself is basically useless but is a segue into how memory and caches and table walks are arranged as well as "tip of the iceberg" tool and technique recommendations, including cachegrind and gdb and presumably Godbolt, who gets an early mention.

For embedded, this might do something nonawful on a device with memory management e.g. Cortex-A. This exercise is left to the reader.

Moral of the talk? Most of computing is extremely complicated, and you can hand-wave and abstract much of it away unless it's directly in your field. In this case, that would be a compiler engineer or low-level kernel developer.

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

#5

Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)... I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again. Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the…

Ok, suspicion confirmed after watching: The code snippet itself is basically useless but is a segue into how memory and caches and table walks are arranged as well as "tip of the iceberg" tool and technique recommendations, including cachegrind and gdb and presumably Godbolt, who gets an early mention. For embedded, this might do something nonawful on a device with memory management e.g. Cortex-A. This exercise is le…

[deleted]

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

#6

Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)... I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again. Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the…

Ok, suspicion confirmed after watching: The code snippet itself is basically useless but is a segue into how memory and caches and table walks are arranged as well as "tip of the iceberg" tool and technique recommendations, including cachegrind and gdb and presumably Godbolt, who gets an early mention. For embedded, this might do something nonawful on a device with memory management e.g. Cortex-A. This exercise is le…

All C programs do the same thing: look at a character and do nothing with it. —Peter Weinberger [0]

Humorous 'memory' explaination of various levels of 'memory'[1]

Without reviewing the video, looks like a coding artifact related to segment 64k memory of 8/16/32/64 bit dos/MS windows (vs. arm / sparc with flat address space).

far/near pointer delclaration would clarify things a bit.

This is a c language artifact. Using in C++, one would need to impliment/override the default C++ supplied memory management to avoid memory management gotchas.

The code example was a way to make sure that pointer variable on a non-unified memory architecture was initialized to point within a given memory segment.

aka Reference zero's out the non-unique bits and leaves the "memory reference bits for given memory segment" alone / segmented NULL.

Zeroing out the non-unique memory bits still allows one to make use of the upper address bits to find out where in memory the given

memory segment is (useful for implimentation of setjump(), figuring out machine byte order, 1st time in segment, etc.).

[0] : Expert C Programming, Deep C Secrets by Peter Weinberger

[1] : http://www.gnu.org/fun/jokes/paging.game.html

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

#8

Before I watch the full talk for the answer (what I've seen so far, he states there's not a right answer)... I know on some microcontrollers (e.g. Arm) addresses 0x0 and 0x4 are usually used to define the initial stack pointer and entry point and then are never needed again. Later, you probably want to detect the presence of null pointers by checking if &maybeAStruct is either 0 or valid. If you accidentally test the…

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.

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

#10
Old school C programmer would make it into a calculus problem for clarity/proof of concept.

Until accessed, a memory location & variable location can be expressed as an indefinate integral.

Memory location access request implies definate integral.

1) evaluation value indefinate of integrand variables: "A" less than/equal to "B" less than/equal to "C" less than/equal to "D". Variable a is start of sequence. Variable D is end of sequence.

2) integrand variables D & A define the continuous range of PC group of bits

3) integrand variables C & B define the cohntinous range of 8 bit char

4) Accessing bits outside the integrand limits implies math equivalent of computer segfault.

In order to not generate a segfault, only positive delta, and epsilon is 0 or 1. aka The "alignment request" implies that A MUST be equal to B; C less than or equal to D.

K&R C short hand for above is anonymous union.[0]

Physics / Mechanical Engineering center of gravity approach rotated 90 & implimented via punch card reader that doesn't skip cards much more interesting physical list processing visual than calculus number line (IMHO). Physical form bit to bulky to carry to/from class room. Fortunately, electrial engineering split the difference via 1/2 way point (45 degree angle). Computer scientests get the autonoma / semantic analysis difference. Software Engineers, by completing the circle with & with out a sigh of "e e e e e e ..."

[0] : http://www.catb.org/esr/structure-packing/

Post reply on HN