Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char array not resetting 1

Status
Not open for further replies.

flubbard

Technical User
Jun 11, 2003
148
0
0
US
I am trying to load and process a large text file one page at a time (seperated by a form feed). Problem is that the character array that I am feeding the data into with a getline fills up after a couple of pages. It should only be drawing one page at a time and therefore no more than one page should ever be in the buffer at a time. I have verified that the getPage function does return just one page, however after two calls to it, the function returns blanks.

My guess is that the character buffer is not resetting each time the function is called and therefore it is getting full.

Any thoughts would be greatly appreciated.

- flub

Code:
int main ()
{
  //Variable Declarations ------//
  string DataFileName = "filename.txt",
	Path="./",
	FullFileName;  

  string page;
  ifstream datfile;

  // Process the data file -----//
  // open the file
  FullFileName = Path + DataFileName;
  datfile.open(FullFileName.c_str());

  if (!datfile.is_open())
    {
      cout << "An error occured opening the data file.\nPlease make sure that the data file is present and run program again\n";
      return(-1);
    }

  // get the first page
  page = getPage(datfile);

	

  // search for the first instance of "BRANCH"
  while (!findString(page, "BRANCH"))
  {
	  // get the next page
	  page = getPage(datfile);
	  // display the page
	  cout << endl << page << endl;

  }


  datfile.close();

	  
  return (1);
}

string getPage (ifstream &infile)
{
	char ff = char(0x0c);
	const int SIZE = 4800;
	char *buffer = new char[SIZE];	
	
	infile.getline (buffer, SIZE, ff);

	return (buffer);
}
 
Wow, that's pretty scary...
I saw pretty good code using STL strings & ifstreams in the main() function; but then I got to getPage() and C++ went right out the window.
getPage() returns a string, but you are returning buffer which is a dynamically allocated char array. This means you have a memory leak since that memory never gets deleted, and if the line in the file is longer than SIZE, you will overflow the array.

Just use strings like you do in main()
Code:
string getPage( ifstream& inFile )
{
   string page;
   getline( inFile, page, 0x0c );
   return page;
}
 
That will teach me to let my inexperience show. Thanks for the help. I had not seen that form of the function and thought that I had to use the old character array type.

- flub
 
Code:
string getPage( ifstream& inFile )
{
   string page;
   getline( inFile, page, 0x0c );
   return page;
}
Just wondering - what happens when string value is returned to the caller? Local variable page disappears right before return - so return value itself should be allocated somewhere else dynamically. When it is cleared then?
 
STL string object has a value semantics: its life time depends on a caller context. As usually, it lives until an expression evaluated. For example:
Code:
string s;
s = getPage(...); // new implicit string value returned
// It is NOT local string var object, it's a new copy!
// Now this object discarded, but the copy saved in s...
In that sense std::string values is like as int, double or others value types.
 
Since string is a class, the constructor/destructor takes care of memory allocation & deallocation.
 
> Since string is a class, the constructor/destructor takes care of memory allocation & deallocation.

You are right about additional space for string data but not about class object itself. As I understood, object variables are managed by compiler: local variables and return values are allocated in the stack and destroyed: local variables before return, return values - after return and completing of the callers expression (possibly storing a copy to a callers variable).
Stack structure inside of called function, IMHO:
...
return value
return address
local variables
stack pointer --->
 
> In that sense std::string values is like as int, double or others value types.

int, doubles and pointers are returned in registers, so don't have to be destroyed. I was confused about big return values, needed to be allocated and deleted.
 
Thanks for all the input. I hope that I understand this right, now. In simplistic terms, the original implimentation used a character array which was never fully deleted upon completion of the function, since only the pointer to the first character would be deleted and the whole array would not be returned to available memory. I could have fixed this leak by making a call to delete for the character array.

Though stl strings behave in a similar fashion, the constructor and destructor automatically handle the deletion of the character "array" so that the memory management does not need to be coded into the program. The life of the string should die when the function returns the value and the entire memory should be returned to the available stack by the string destructor.

Is this about right?

- flub
 
Don't mix up returned value life time and implementation details (registers, stack etc). Apropos, returned std::string object is never allocated on the stack.

STL string value (not a pointer or a reference) semantics means: you may work with it as you work with int, double etc. It's another story when its destructor works and its memory is deallocated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top