You are not logged in.

#1 2009-12-15 01:32:45

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

C question.

As a challenge, I decided to try making a simple CPU emulator. When I try to execute the program, I get segmentation faults.

What exactly is a segmentation fault? How do I get rid of it?

#include <stdio.h>

main()
{
        int ram[0xffff];
        int pcounter;
        int somevalue = 5;

        ram[0x1024] = 0xff;

        somevalue = ram[pcounter];
        switch (somevalue) {
                case 5:
                        printf("Hello, World!");
                        break;
                default:
                        break;
        }
        return 0;
}

Personally, I'd rather be back in Hobbiton.

Offline

#2 2009-12-15 02:06:25

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Re: C question.

Nvm, I figured it out. I'm such a noob.


Personally, I'd rather be back in Hobbiton.

Offline

#3 2009-12-15 14:40:53

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C question.

What exactly is a segmentation fault?

This may help better your understanding: http://en.wikipedia.org/wiki/Segmentation_fault

Offline

#4 2009-12-15 16:06:20

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: C question.

Commenting on a non-issue here, but I guess my curiosity got the best of me;
Why 0x1024? 1024 is a power of two and would make some kind of sense in that everything boils down to binary, so powers of two are nice.
But in hex 1024 is 0x0400...


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#5 2009-12-15 16:23:25

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: C question.

tlvb wrote:

Commenting on a non-issue here, but I guess my curiosity got the best of me;
Why 0x1024? 1024 is a power of two and would make some kind of sense in that everything boils down to binary, so powers of two are nice.
But in hex 1024 is 0x0400...

You're completely right, OP should take note tongue
That's probably the only reason he used 1024.

Offline

Board footer

Powered by FluxBB