Memory error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brasewel
    New Member
    • Sep 2007
    • 3

    Memory error

    Hi guys...I keep getting an error saying "Cannot access memory at address 0x2e". I have commented at which line it occurs. The code compiles but spits out the error at runtime. Any help would be greatly appreciated.

    Here is the code snippet

    [code=cpp]
    #include "Block.h"
    #include "Solver.h"
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <fstream>


    using namespace std;

    int main( int argc, char* argv[]) {
    if(argc != 3) {
    cerr << "Usage: \"block input-file\" \"output-file" << endl;
    exit(-1);
    }

    //streams
    istream *isp;
    ifstream ifs;
    ostream *osp;
    ofstream ofs;

    if (argv[1][0] == '-' && argv[1][1] == '\0') {
    isp = &cin;
    } else {
    ifs.open(argv[1]);
    isp = &ifs;
    }
    istream &is = *isp;

    if (argv[2][0] == '-' && argv[2][1] == '\0') {
    osp = &cout;
    } else {
    ofs.open(argv[2]);
    osp = &ofs;
    }
    ostream &os = *osp;

    //GET INFO ON THE PUZZLE
    //get rows and cols
    int rows;
    int cols;
    is >> cols >> rows;
    // get starting puzzle into array
    vector<int> starting;
    vector<int> ending;
    char temp;
    int counter = 0;
    //cout << rows << "" << cols << endl; ----ERROR OCCURS AT THIS LINE IF I DO NOT COMMENT IT.
    while(is.good() && counter < (rows*cols)){
    is >> temp; --------ERROR OCCURS AT THIS LINE
    starting.push_b ack((int)temp);
    counter++;
    }
    // get solution puzzle into array
    counter = 0;
    //int t=rows*cols;
    while(is.good() && counter < (rows*cols)){
    is >> temp;
    ending.push_bac k((int)temp);
    counter++;
    }


    Block ll(os, starting, ending, rows, cols);
    Solver s(ll);
    ll.display(s.so lve(starting));

    };[/code]
    Last edited by Banfa; Nov 3 '07, 01:00 PM. Reason: Added code tags as per posting guidelines
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Originally posted by brasewel
    int rows;
    int cols;
    is >> cols >> rows;
    You never check a) that rows and cols were fetched without error or b) that they contain valid values and not some junk like -34456.

    The loops look odd. You use >> to fetch a char but you cast it to an int. What's that about. a char with 65 is an int with 65. Why the cast??

    Also, by fetching a char you get one byte. You need to getch 4 bytes to get an int.

    So exactly what are you doing? Are you working with char?? If so, you need a vector<char>. If you have ints, then you can't fetch into a char. You need to fetch into an int.

    A file with 1234 56768 is 9 chars but only 2 ints.

    Please explain your disc file content.

    Comment

    Working...