Question

I am learning programming for 8 bit 6502 in C compiler (www.cc65.org)

The NES FC has a 8bit 6502 processor and a 2K RAM. However, the following C compiles (into a nes file) and loads successfully in VirtualNES emulator.

#include "conio.h"
#include "stdlib.h"

int dump[1000];

void main()
{
    int *a;
    a = (int*)malloc(19222999);
    cputs("Hello, World!");
    a[0] = 1;
    for(;;); // loop forever, never ends
}

Why this is OK ? clearly I have allocated more memory than 2K in the above C code.

Was it helpful?

Solution 2

You don't check the value of a. (BTW, why do you cast the return value?)

I suppose it is NULL. So a write access to a[0] is no valid C, but it might (depending on the architecture) work nevertheless - and cause havoc and disruption there.

The compiling process works as expected, because for the compiler and the linker, malloc() is a function as anything else. The mistake only happens at runtime.

OTHER TIPS

Segfaults are a wonderful, miraculous boon of the modern age; they represent our computers self-diagnosing their own bugs, moment-to-moment.

In olden times, in contrast, there was very little way to "crash" a computer, in the sense that the computer would have some awareness that something had gone wrong, causing it to do something else instead. Here, you've written a 1 to somewhere in memory (quite possibly overwriting something important!), and then the computer goes on.

The takeaway: In old architectures and embedded systems, "doesn't crash" is a very low bar to clear, and does not indicate that things are OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top