473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. #include "Block.h"
  2.  #include "Solver.h"
  3.  #include <iostream>
  4.  #include <istream>
  5.  #include <ostream>
  6.  #include <fstream>
  7.  
  8.  
  9.  using namespace std;
  10.  
  11.  int main( int argc, char* argv[]) {
  12.      if(argc != 3) {
  13.          cerr << "Usage: \"block input-file\" \"output-file" << endl;
  14.          exit(-1);
  15.      }
  16.  
  17.     //streams
  18.      istream *isp;
  19.     ifstream ifs;
  20.     ostream *osp;
  21.     ofstream ofs;
  22.  
  23.     if (argv[1][0] == '-' && argv[1][1] == '\0') {
  24.         isp = &cin;
  25.     } else {
  26.         ifs.open(argv[1]);
  27.         isp = &ifs;
  28.     }
  29.     istream &is = *isp;
  30.  
  31.     if (argv[2][0] == '-' && argv[2][1] == '\0') {
  32.         osp = &cout;
  33.     } else {
  34.         ofs.open(argv[2]);
  35.         osp = &ofs;
  36.     }
  37.     ostream &os = *osp;
  38.  
  39.     //GET INFO ON THE PUZZLE
  40.     //get rows and cols
  41.     int rows;
  42.     int cols;
  43.     is >> cols >> rows;
  44.     // get starting puzzle into array
  45.     vector<int> starting;
  46.     vector<int> ending;
  47.     char temp;
  48.     int counter = 0;
  49.     //cout << rows << "" << cols << endl; ----ERROR OCCURS AT THIS LINE IF I DO NOT COMMENT IT.
  50.     while(is.good() && counter < (rows*cols)){
  51.         is >> temp;    --------ERROR OCCURS AT THIS LINE    
  52.         starting.push_back((int)temp);
  53.         counter++;
  54.     }
  55.     // get solution puzzle into array    
  56.     counter = 0;
  57.     //int t=rows*cols;
  58.     while(is.good() && counter < (rows*cols)){
  59.         is >> temp;
  60.         ending.push_back((int)temp);
  61.         counter++;
  62.     }    
  63.  
  64.  
  65.     Block ll(os, starting, ending, rows, cols);
  66.     Solver s(ll);
  67.     ll.display(s.solve(starting));
  68.  
  69.  };
Nov 2 '07 #1
1 1436
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
1
by: Attila.Rajmund.Nohl | last post by:
Hello! I'm using KAI C++ Compiler (KCC) Version 4.0d on Sparc Solaris 8 with Sun WorkShop 6 update 2 backend (KCC compiles C++ code to C than uses the Sun compiler to produce machine...
6
by: Andrzej | last post by:
Used to read newsgroup for answers, now have to ask for them as well. I have an application (C#, .NET 1.1) that connects to local db on MSDE 2000 SP3a (using ADO from MDAC 2.71) on one side and...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
2
by: saran | last post by:
I am having a problem with MySQL consuming a lot of memory and eventually throwing an Out of Memory error and restarting itself. The symptoms are that swap usage continues to rise until some...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
2
by: ravishi | last post by:
Well, this is my first topic at this forum and I'm a newbie on C programming too. I'm coding a little program and I've used some "dynamic arrays" on it. Compiling and running the program on Linux...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.