| | |
GAAAH! Memory issue?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Alright, my program is giving me some serious problems. I think I have a memory leak, but I shouldn't have any.
I have
The problem was that it was getting to line 24, outputting something, then never outputting line 25. It froze. My syntax is entirely correct there, but I think I have a memory leak causing it to freeze. What's my problem?
/* this is c++. I am using g++ on a debian 'etch' linux machine. */
I have
ifstream In;, followed by In.open(thefile.c_str());, a series of In.get( tempChar );s, then at the end of the function I have In.close();.The problem was that it was getting to line 24, outputting something, then never outputting line 25. It froze. My syntax is entirely correct there, but I think I have a memory leak causing it to freeze. What's my problem?
/* this is c++. I am using g++ on a debian 'etch' linux machine. */
Damn computer! It ate everything!
Look for pointers and dynamic memory allocation. Oh and if you want us to help you find the error, maybe you should post some code (or at least up to the point where it freezes). Thanks.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
please god, thanks. Here's the driver:
Now here's the implementation file:
And finally the header:
Thanks a lot. The faster this gets done, the better, I only have 10 minutes to finish this. CRAP!
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <fstream> #include <string> #include <iomanip> using namespace std; #include "proj11mineSweeperGame.h" int main() { mineSweeperGame Game; string sMap; cout << "Please type in the name of the map file you wish to use:\n"; cin >> sMap; Game.readMap(sMap); system ("pause"); return 0; }
Now here's the implementation file:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> #include <vector> #include <string> #include <cstdlib> using namespace std; #include "proj11mineSweeperGame.h" void mineSweeperGame::readMap( string sMap ) // reads the map file and sets up the maps { ifstream In; In.open( sMap.c_str() ); while ( In == NULL ) { cout << "open failure. Please check the file's location and try again.\n"; cout << "What is the map file's name? "; cin >> sMap; In.open( sMap.c_str() ); } cout << "24\n" << "25\n"; char tempChar; In.get( tempChar ); unsigned int i = 0; while ( tempChar != '/n' ) // loop to find the row and col numbers. { string tempString; if ( isdigit( tempChar ) ) { if ( i > 2 ) { tempString = tempChar; row = atoi( tempString.c_str() ); } else { tempString = tempChar; col = atoi( tempString.c_str() ); } } In.get(tempChar); i++; } cout << "50\n"; // time to set up the map vectors. unsigned int x; // i is already declared for ( i = 0 ; i < col ; i++ ) // the loop to set up the columns. { vector <char> emptyCol(row, '#');// set up cols with the rows filled with #s map.push_back(emptyCol); // put them into the map they can see. vector <char> emptyCol2(row, ' ');// set up cols with the rows filled with spaces txtMap.push_back(emptyCol2); // put them into the map they don't see. } cout << "62\n"; // nested loop to find and place the mines in txtMap for ( i = 0 ; i < col ; i++ ) // tells what column I'm in, and repeats only that many times { x = 0; In.get( tempChar ); while ( tempChar != '\n' ) // go until a newline { if ( isdigit( tempChar ) ) // if it's a number, process it. { if ( tempChar == 1 ) // if it's a one, there's a mine { txtMap[i][x] = 'X'; // X marks the spot XD } // if it's a 0, I want a ' ' there; empty. In.get( tempChar ); x++; } else /* if it isn't a number, it needs to be skipped. why'd you have to put spaces in? haha, kidding. I would've put support in anyways. XD */ { In.get( tempChar ); } } } // the map they see doesn't have the mines in it. i have the isMine function to tell that cout << "90\n"; cout << "\n Map processed.\n"; // thought they should know that it was done successfully In.close(); return; }; ostream& operator<< (ostream& Out, mineSweeperGame sMap) { cout << "\n "; unsigned int i; for ( i = 0 ; i < sMap.col ; i++) // output the indicatory column numbers { cout << i << " "; } cout << "\n"; for ( i = 0 ; i < sMap.row ; i++ ) // output the rows with indicatory row numbers { cout << i << "|"; for (unsigned int x = 0 ; x < sMap.col ; x++ ) { cout << sMap.map[x][i] << "|"; } cout << "\n"; } cout << "==========================================\n"; };
And finally the header:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> using namespace std; class mineSweeperGame { public: void readMap( string sMap ); friend ostream& operator<< ( ostream& Out, mineSweeperGame game ); bool isMine( int usrRow, int usrCol ); private: vector< vector<char> > txtMap; //the map as is in text file vector< vector<char> > map; // the map as seen by the player int row, // the number of rows in the map col; // the number of columns in the map };
Thanks a lot. The faster this gets done, the better, I only have 10 minutes to finish this. CRAP!
Damn computer! It ate everything!
Hmm...here is what I think you should do:
1. Always keep a guard condition while making header files which does away with the problem of recursive inclusion of headers. Something like:
2. Don't know why but your loop looks fishy:
Try removing the loop and ducking out if the fle is not found.
3. Tada..and this I think according to me is the culprit:
Just to let you know there is no such thing as /n, it must have been a typo mistake since you should write \n if validating against a newline character.
4. Oh yes...don't use system("pause") , getchar( ) achieves the same thing without putting portability at stake....
1. Always keep a guard condition while making header files which does away with the problem of recursive inclusion of headers. Something like:
C++ Syntax (Toggle Plain Text)
#ifndef TGA_H_ #define TGA_H_ // your header file here #endif
2. Don't know why but your loop looks fishy:
C++ Syntax (Toggle Plain Text)
while ( In == NULL ) { cout << "open failure. Please check the file's location and try again.\n"; cout << "What is the map file's name? "; cin >> sMap; In.open( sMap.c_str() ); }
3. Tada..and this I think according to me is the culprit:
C++ Syntax (Toggle Plain Text)
while ( tempChar != '/n' ) // loop to find the row and col numbers.
4. Oh yes...don't use system("pause") , getchar( ) achieves the same thing without putting portability at stake....
I don't accept change; I don't deserve to live.
C++ Syntax (Toggle Plain Text)
if ( i > 2 ) { tempString = tempChar; row = atoi( tempString.c_str() ); } else { tempString = tempChar; col = atoi( tempString.c_str() ); }
C++ Syntax (Toggle Plain Text)
if ( i > 2 ) { row = tempChar - '0'; } else { col = tempChar - '0'; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
oh... yep. That's a pretty typical typo with me.
The
I never did understand the stuff to prevent the recursive header thing, I don't really get it yet. I'll have to ask someone to explain that for me.
And why would subtracting '0' from a character convert it to an integer? isn't that what atoi is for? granted, atoi is meant to be used with c-strings but it still gets the job done for characters. I don't remember if it worked without the addition of the string converted to a c-string though... that part gave me a lot of hell.
and oh yeah... system("pause") is a dos thing... that wouldn't have worked if my program ever got that far ^_^U
Alrighty, well thanks a lot guys. I didn't get this turned in on time, but I'm still going to finish writing it.
The
while ( In == NULL ) loop actually works perfectly. That's how I've always done that.I never did understand the stuff to prevent the recursive header thing, I don't really get it yet. I'll have to ask someone to explain that for me.
And why would subtracting '0' from a character convert it to an integer? isn't that what atoi is for? granted, atoi is meant to be used with c-strings but it still gets the job done for characters. I don't remember if it worked without the addition of the string converted to a c-string though... that part gave me a lot of hell.
and oh yeah... system("pause") is a dos thing... that wouldn't have worked if my program ever got that far ^_^U
Alrighty, well thanks a lot guys. I didn't get this turned in on time, but I'm still going to finish writing it.
Last edited by FireSBurnsmuP; Dec 5th, 2006 at 5:13 pm.
Damn computer! It ate everything!
•
•
•
•
The while ( In == NULL ) loop actually works perfectly. That's how I've always done that. •
•
•
•
I never did understand the stuff to prevent the recursive header thing, I don't really get it yet. I'll have to ask someone to explain that for me.
•
•
•
•
And why would subtracting '0' from a character convert it to an integer?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
while I'm at it, for the #ifndef stuff, should I be putting the class name in there instead of a TGA_H? I thought I had seen it done that way before... #pragma once) you'd see pretty much a random string so there would never be issues with another file having the same tag as yours. ![]() |
Similar Threads
- Memory Issue's! Please Help!! (Motherboards, CPUs and RAM)
Other Threads in the C++ Forum
- Previous Thread: merci merci
- Next Thread: Boolean Algebra
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






