Seg Fault

This is a discussion on Seg Fault within the C++ Programming forums, part of the General Programming Boards category; Bah, I cant find this bug to save my life. (NOTE: It is 4AM at the moment) here is the ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Seg Fault

    Bah, I cant find this bug to save my life.

    (NOTE: It is 4AM at the moment)

    here is the section of code where the thing happens..

    Code:
    #include "C8memory.h"
    
    C8memory::C8memory()
    {
        
        Memory = new unsigned char [MEMORY_SIZE];
        Registers = new unsigned char [REGISTERS];
        Stack = new unsigned short [STACK];
        
        Reset();
        
    }
    
    C8memory::~C8memory()
    {
        
        delete [] Memory;
        delete [] Registers;
        delete [] Stack;
        
    }
    
    void C8memory::Reset()
    {
        
        memset(Memory, 0, MEMORY_SIZE);
        memset(Registers, 0, REGISTERS);
        memset(Stack, 0, STACK);
        
        PC = 0x200;
        Register_I = 0x200;
        SP = 0;
        
    }     
    
    bool C8memory::LoadRom(char *filename)
    {
        
        std::ifstream rom(filename, std::ios::binary);
        
        if(!rom.is_open()) return false;
        
        rom.seekg(0, std::ios::end);
        const long file_size = rom.tellg();
        rom.seekg(0, std::ios::beg);
        
        char *rom_buffer = new char [file_size];
        
        rom.read(rom_buffer, file_size);
        rom.close();
        
        memcpy(&Memory[0x200], rom_buffer, file_size); //<--HERE is the fault
        
        delete [] rom_buffer;
        return true;
              
    }
    I am exausted and it could possibly be something simple.
    Sorry in advance if it is

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Well depends on how big file_size is in relation to MEMORY_SIZE
    MEMORY_SIZE needs to be at least file_size + 0x200
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    MEMORY_SIZE is defined as 0xFFF.

    I belive file_size was like 0x220 when I was debugging it last night.

    It seemed like Memory wasnt being allocated, But I dont know what would make the constructor not function...

    Is the constructor called when you create a class pointer?

    For example I had this

    Code:
    class another_class
    {
    
    public:
    
        C8memory *memory; //<-- wouldnt the constructor still get called?
    
    };
    Last edited by Vicious; 09-04-2004 at 10:19 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Only if you did in your other_class constructor
    memory = new C8memory;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ah, Thank You.

    I am a class pointer noob..

    Thanks for your help Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 04:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21