Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C++
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
  {
char word[30];
  string filename;
 //map<char,int> mymap;
  //map<char,int>::iterator it;


  
  cout << "Name your file> ";
  getline( cin, filename );

  fstream file( filename.c_str() );
  if (!file)
    {
    cout << "I could not open the file. Fooey.\n";
    return EXIT_FAILURE;
    }
  else 
  {
    string line;
    
    while (getline( file, line ))
      {
         if ( line.find("new") != string::npos)
    {
      if ( line.find("int *") != string::npos)
	  {
		//want to catch the pointer in which the memory is allocated  
	  }
    }

	 
     
      }
    file.close();
    }

  return EXIT_SUCCESS;
  }


I am creating a static analyzer to detect memory leaks ..please help
Posted

I think using Regular Expressions will help you a lot in doing this project :

http://linuxgazette.net/issue27/mueller.html[^]

Henry Spencer's Regexp Engine Revisited[^]

http://www.regular-expressions.info/tutorial.html[^]

There are many C++ libraries for Regular Expression pattern matching. Use google to find more resources about it.
 
Share this answer
 
You could also use the return value of the call to line.find() which gives the starting position of the string. However your code has a major problem as an analyzer in that not everyone writes using the same conventions. consider the following lines of code:
C++
int *pOne;
int* pTwo;
int  *    // this is a comment
  pThree;

All of these are syntactically correct but you need quite a lot more to be able to recognise them.
 
Share this answer
 
yes you are right...that is why i did thi to extract the variable name...
C++
  while (getline( file, line ))
    {
       if ( line.find("new") != string::npos)
        {
      temp = line;
      string::iterator It = temp.begin();
while ( *It != '*'  )
  {
      It++;
  }
    It++;
   while ( *It != '='  )
  {

      word = *It;
      It++;
     if (word ==' ')
     {
         continue;

     }

      str = str + word;
   }

but again if the declaration is like this:

C++
int* p ;
p = new int;

the problem still persists..can you help plzzz
 
Share this answer
 
Your code processes a line at a time.

In your example, you have two lines to evaluate.

You're going to need more logic than a simple keyword search to do this.

Essentially you'll need to write a parser that can understand the meaning of statements in context.

I'll throw you a another monkey wrench...

C++
void * p = NULL;

switch(n)
{
case 0:
    p = new CStuff1;
    break;

case 1:
    p = new CStuff2;
    break;

case 2:
    throw("Invalid Value");
    break;

case 3:
    p = new CStuff3;
    break;

default:
    p = NULL;
    break;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900