GAAAH! Memory issue?

Reply

Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

GAAAH! Memory issue?

 
0
  #1
Dec 5th, 2006
Alright, my program is giving me some serious problems. I think I have a memory leak, but I shouldn't have any.

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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: GAAAH! Memory issue?

 
1
  #2
Dec 5th, 2006
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."
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: GAAAH! Memory issue?

 
0
  #3
Dec 5th, 2006
please god, thanks. Here's the driver:
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. #include "proj11mineSweeperGame.h"
  9.  
  10. int main()
  11. {
  12. mineSweeperGame Game;
  13. string sMap;
  14. cout << "Please type in the name of the map file you wish to use:\n";
  15. cin >> sMap;
  16. Game.readMap(sMap);
  17.  
  18. system ("pause");
  19. return 0;
  20. }

Now here's the implementation file:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <string>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. #include "proj11mineSweeperGame.h"
  10.  
  11. void mineSweeperGame::readMap( string sMap ) // reads the map file and sets up the maps
  12. {
  13. ifstream In;
  14.  
  15. In.open( sMap.c_str() );
  16. while ( In == NULL )
  17. {
  18. cout << "open failure. Please check the file's location and try again.\n";
  19. cout << "What is the map file's name? ";
  20. cin >> sMap;
  21.  
  22. In.open( sMap.c_str() );
  23. }
  24. cout << "24\n" << "25\n";
  25. char tempChar;
  26. In.get( tempChar );
  27. unsigned int i = 0;
  28. while ( tempChar != '/n' ) // loop to find the row and col numbers.
  29. {
  30. string tempString;
  31.  
  32. if ( isdigit( tempChar ) )
  33. {
  34. if ( i > 2 )
  35. {
  36. tempString = tempChar;
  37. row = atoi( tempString.c_str() );
  38. }
  39.  
  40. else
  41. {
  42. tempString = tempChar;
  43. col = atoi( tempString.c_str() );
  44. }
  45. }
  46.  
  47. In.get(tempChar);
  48. i++;
  49. }
  50. cout << "50\n";
  51. // time to set up the map vectors.
  52. unsigned int x;
  53. // i is already declared
  54. for ( i = 0 ; i < col ; i++ ) // the loop to set up the columns.
  55. {
  56. vector <char> emptyCol(row, '#');// set up cols with the rows filled with #s
  57. map.push_back(emptyCol); // put them into the map they can see.
  58.  
  59. vector <char> emptyCol2(row, ' ');// set up cols with the rows filled with spaces
  60. txtMap.push_back(emptyCol2); // put them into the map they don't see.
  61. }
  62. cout << "62\n";
  63. // nested loop to find and place the mines in txtMap
  64. for ( i = 0 ; i < col ; i++ ) // tells what column I'm in, and repeats only that many times
  65. {
  66. x = 0;
  67. In.get( tempChar );
  68. while ( tempChar != '\n' ) // go until a newline
  69. {
  70. if ( isdigit( tempChar ) ) // if it's a number, process it.
  71. {
  72. if ( tempChar == 1 ) // if it's a one, there's a mine
  73. {
  74. txtMap[i][x] = 'X'; // X marks the spot XD
  75. }
  76. // if it's a 0, I want a ' ' there; empty.
  77. In.get( tempChar );
  78. x++;
  79. }
  80.  
  81. else /* if it isn't a number, it needs to be skipped.
  82.   why'd you have to put spaces in? haha, kidding.
  83.   I would've put support in anyways. XD */
  84. {
  85. In.get( tempChar );
  86. }
  87. }
  88. }
  89. // the map they see doesn't have the mines in it. i have the isMine function to tell that
  90. cout << "90\n";
  91. cout << "\n Map processed.\n"; // thought they should know that it was done successfully
  92. In.close();
  93. return;
  94. };
  95.  
  96. ostream& operator<< (ostream& Out, mineSweeperGame sMap)
  97. {
  98. cout << "\n ";
  99. unsigned int i;
  100. for ( i = 0 ; i < sMap.col ; i++) // output the indicatory column numbers
  101. {
  102. cout << i << " ";
  103. }
  104. cout << "\n";
  105. for ( i = 0 ; i < sMap.row ; i++ ) // output the rows with indicatory row numbers
  106. {
  107. cout << i << "|";
  108. for (unsigned int x = 0 ; x < sMap.col ; x++ )
  109. {
  110. cout << sMap.map[x][i] << "|";
  111. }
  112. cout << "\n";
  113. }
  114. cout << "==========================================\n";
  115. };

And finally the header:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class mineSweeperGame
  7. {
  8. public:
  9. void readMap( string sMap );
  10. friend ostream& operator<< ( ostream& Out, mineSweeperGame game );
  11. bool isMine( int usrRow, int usrCol );
  12.  
  13. private:
  14. vector< vector<char> > txtMap; //the map as is in text file
  15. vector< vector<char> > map; // the map as seen by the player
  16. int row, // the number of rows in the map
  17. col; // the number of columns in the map
  18. };

Thanks a lot. The faster this gets done, the better, I only have 10 minutes to finish this. CRAP!
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: GAAAH! Memory issue?

 
1
  #4
Dec 5th, 2006
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:
  1. #ifndef TGA_H_
  2. #define TGA_H_
  3.  
  4. // your header file here
  5.  
  6. #endif

2. Don't know why but your loop looks fishy:
  1. while ( In == NULL )
  2. {
  3. cout << "open failure. Please check the file's location and try again.\n";
  4. cout << "What is the map file's name? ";
  5. cin >> sMap;
  6.  
  7. In.open( sMap.c_str() );
  8. }
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:
  1. while ( tempChar != '/n' ) // loop to find the row and col numbers.
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....
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: GAAAH! Memory issue?

 
0
  #5
Dec 5th, 2006
  1. if ( i > 2 )
  2. {
  3. tempString = tempChar;
  4. row = atoi( tempString.c_str() );
  5. }
  6.  
  7. else
  8. {
  9. tempString = tempChar;
  10. col = atoi( tempString.c_str() );
  11. }
The above is much too complicated. All it is doing is converting a single character to an integer. Just simply subtract '0'.
  1. if ( i > 2 )
  2. {
  3. row = tempChar - '0';
  4. }
  5.  
  6. else
  7. {
  8. col = tempChar - '0';
  9. }
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: GAAAH! Memory issue?

 
0
  #6
Dec 5th, 2006
oh... yep. That's a pretty typical typo with me.

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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: GAAAH! Memory issue?

 
1
  #7
Dec 5th, 2006
Originally Posted by FireSBurnsmuP View Post
The while ( In == NULL ) loop actually works perfectly. That's how I've always done that.
I see your logic -- if the file was not found, then ask for a new file name.

Originally Posted by FireSBurnsmuP View Post
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.
Its for instances when a .h file is included two or more times, which often happens with your compiler's standard header files. This prevents symbols from being defined more than once.


Originally Posted by FireSBurnsmuP View Post
And why would subtracting '0' from a character convert it to an integer?
lets take the example of '1'. '1' - '0' = 49 - 48 = 1. You can use a good ascii chart to see all the decimal values of the characters. I like to use '0' in my programs instead of 48 because it makes more sense.
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: GAAAH! Memory issue?

 
0
  #8
Dec 5th, 2006
wow, thanks. Now I get it, it's the decimal values.

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...
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: GAAAH! Memory issue?

 
0
  #9
Dec 5th, 2006
Originally Posted by FireSBurnsmuP View Post
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...
Leave it as TGA_H. The #ifndef followed by the #define are a way to tag your file uniquely so that it never gets #included more than once (which would cause multiple-definition hell). If you use an IDE which autogenerates those lines (old versions of VC++ used to, but I think newer ones use the compiler-specific #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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: GAAAH! Memory issue?

 
0
  #10
Dec 5th, 2006
Originally Posted by FireSBurnsmuP View Post

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...
I always use the header filename, but it doesn'r really matter as long as it isn't duplicated anywhere else.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC