943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2495
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 5th, 2006
0

GAAAH! Memory issue?

Expand Post »
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. */
Similar Threads
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 5th, 2006
1

Re: GAAAH! Memory issue?

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

please god, thanks. Here's the driver:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 5th, 2006
1

Re: GAAAH! Memory issue?

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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....
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

C++ Syntax (Toggle Plain Text)
  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'.
C++ Syntax (Toggle Plain Text)
  1. if ( i > 2 )
  2. {
  3. row = tempChar - '0';
  4. }
  5.  
  6. else
  7. {
  8. col = tempChar - '0';
  9. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

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.
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

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.

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.


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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

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...
Reputation Points: 46
Solved Threads: 2
Posting Whiz in Training
FireSBurnsmuP is offline Offline
237 posts
since Sep 2006
Dec 5th, 2006
0

Re: GAAAH! Memory issue?

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Dec 5th, 2006
0

Re: GAAAH! Memory issue?


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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: merci merci
Next Thread in C++ Forum Timeline: Boolean Algebra





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC