Thread: Is there something wrong with this function?

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    Is there something wrong with this function?

    Ive been getting an illegal error in a program, and ive tracked it down to this function (and a certain line), so can someone tell me if there is a memory leak or another problem that would be causing the problem? Thanks.

    Code:
    int a_dll_written(char *dll_line)
    {		
    	ifstream i_dll("dll.txt");
    	char *new_line=new char[256];  //This line is where the error is caused at, and this function is called a lot in the program, so it might be a leak?
    	memset(new_line,'\0',256);
    	
    	int r_val=0;
    		
    	while(!i_dll.eof())
    	{
    		i_dll.getline(new_line,256,'\n');
    		if(strcmpi(new_line,dll_line)==0) r_val=1;  //return 1 if theyre equal
    	}
    	i_dll.close();
    	delete new_line;
    	return r_val;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You are correct. Since you create an array of char's, you need to delete an array of chars. Your method works when you have only allocated one character. Change it to the following:

    delete [] new_line

  3. #3
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    Ooh, so to delete more than just one char pointer you need to use put brackets before the variable name in delete? I was always just doing to without the brackets, thanks!

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    no problemo (Este esta escribido en espanol)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM