[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3507.0. "ANOTHER MEMORY LEAK- SEEMS SOMEWHAT STRANGE" by TAV02::MILCHIN () Thu Mar 20 1997 19:33

	HI!

	Unfortunately, I'm completely lost to figure why the following 
simple program crashes. More precisly, on OPENVMS ALPHA V6.2 with DEC C V5.5 and
latest ECO for CXXL$011_SHR.EXE, it causes the memory leak by 'swallowing' any
size of process dynamic memory.

	The program is:
************************8888********************************************
#include <string.hxx>
#include <fstream.hxx>

main(int argc,char** argv)
{
  String line="";
  String copy="";
  ifstream input("a.map");
  for (int i=1; input.getline(line,100,'\n') ; i++) {
    cerr << line << "\n";
    line = line(5,line.length()-5);
//    line = copy;
    cerr << line << "\n";
    
  }
}

// A.MAP FILE

** output of "syn_name" - do not change
**
 * i sc_r1_srx_idlecnt_ma n243
 * i sc_r1_s_unlockcnt_ma<1> n190
 * i sc_r1_s_unlockcnt_ma<2> n259
 * i sc_r1_s_unlockcnt_ma<3> n213
 * i sc_r1_s_unlockcnt_ma<13> n237
 * i sc_r1_s_unlockcnt_ma<12> n234
 * i sc_r1_s_unlockcnt_ma<11> n248
 * i sc_r1_srx_idlecnt_ma<2> n242
 * i sc_r1_s_unlockcnt_ma<4> n268
 * i sc_r1_s_unlockcnt_ma<6> n264
 * i sc_r1_srx_cipherenb n187
** i sc_r1_srx_idlecnt_ma<1> n220
 * i sc_r1_srx_idlecnt_ma<3> n230
 * i sc_r1_s_unlockcnt_ma<9> n280
 * i sc_r1_s_unlockcnt_ma n275
 * i sc_r1_s_unlockcnt_ma<8> n196
 * i sc_r1_s_unlockcnt_ma<7> n206
 * i sc_r1_s_unlockcnt_ma<10> n222
 * i sc_r1_srx_testmodea n279
 * i sc_r1_s_unlockcnt_ma<5> n197
 * i sc_r1_srx_rs_ma n215
 * i sc_r1_srx_rs_ma<1> n254
 * i sc_r1_srx_rs_ma<2> n194
*************************************************************************************88
	THANKS IN ADVANCE!
	MICHAEL

T.RTitleUserPersonal
Name
DateLines
3507.1analysis of problem in .0DECC::J_WARDThu Mar 20 1997 21:2231
// I think this is the problem:

#include <string.hxx>
#include <fstream.hxx>

void main()
{
  String line="";
  ifstream input("a.map");

  // there is no istream::getline(String&,...)
  // therefore line is being silently converted to
  // a char*, using the internal char* pointer representation
  // inside the string class. This char* has only been
  // allocated to hold a null byte, so trying to put 80
  // chars into it is going to seg fault...

  // If you are going to use istream::getline() or
  // istream::get() you shouldn't use a String argument. 
  // they were never overloaded on String, I think it's
  // unfortunate that String::operator char* even exists. 
  // The new ANSI string library does not have a conversion. 

  input.getline(line,80);

  // To do it the right way:
  // char buf[120]; // whatever the max size of a line is
  // input.getline(buf,100);
  // String line(buf); 

}
3507.2THANKSTAV02::MILCHINFri Mar 21 1997 07:3911
-->  // there is no istream::getline(String&,...)
-->  // therefore line is being silently converted to
-->  // a char*, using the internal char* pointer representation
-->  // inside the string class. This char* has only been
-->  // allocated to hold a null byte, so trying to put 80
-->  // chars into it is going to seg fault...

	Excelent explanation. I missed the non-existance of
Istream::getline(String&,...). Thank you.
		MICHAEL