Link to home
Start Free TrialLog in
Avatar of gsbabu
gsbabu

asked on

Segmentation Fault - Core Dumped.

Hi

i am running gcc under unix, when i compile the below source code and execute it under command mode, i get the required output. If the same executable is executed under cron, i am getting an error as "Segmentation Fault - Core Dumped". Could any one help me why i am getting this.

---source start---

#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

int main(int argc, char* argv[]) {
  if ( argc != 2 ) {
   cout << endl << " Usage : " << argv[0] << " <userid>" << endl ;
   cout << " This will delete userid mails from your inbox" << endl ;
   exit(1) ;
  }
  string line ;
  char* usr = getlogin();
  char* inpath="/var/mail/";
  char* tmppath="/tmp/";
  char* inpfl = new char[strlen(usr)+strlen(inpath)+1];
  char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
  strcpy(inpfl, inpath);
  strcat(inpfl, usr);
  strcpy(outfl, tmppath);
  strcat(outfl, usr);
  strcat(outfl, ".tmp");

  ifstream in(inpfl) ;
  ofstream out(outfl) ;
  int found = 0 ;

  while (getline(in,line)) {
   string findW(line) ;
   int strfound = findW.find("From ") ;
   if ( strfound == 0 ) {
      int tstfound = findW.find(string("From ") + argv[1]) ;
     if ( tstfound == 0 )
      found = 1;
     else
     {
      out << line << endl ;
      found = 0 ;
      }
    }
    else
    if ( found == 0 )
      out << line << endl  ;
  }

  char* mvstring = new char[strlen(outfl)+strlen(inpfl)+1];
  strcpy(mvstring,"mv ") ;
  strcat(mvstring,outfl) ;
  strcat(mvstring," ") ;
  strcat(mvstring,inpfl) ;
  system(mvstring) ;
}                                                                                                                                    

---source end---

Note : I doubt the reason to core error is that the system could not recognise the loginname when run under cron, if this is so how to overcome this, is there any way to get the loginname.

Sorry for long summary. helps appreciated.

--gsbabu
Avatar of nietod
nietod

Can't you debug it to determine if that is the problem?  and if not to determine where in the program it is crashing.
You have an error when allocating memory to char strings:

char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
strcpy(outfl, tmppath);
strcat(outfl, usr);
strcat(outfl, ".tmp");

You forgot to count ".tmp" string - it takes bytes also.

The same error with mvstring.

I suggest that you don't use char*, use class string instead.


 
dunno if this will help, but

char* outfl = new char[strlen(usr)+strlen(tmppath)+1];
 
is too short... cos u add ".tmp" so it should be +5 instead of +1...

similar prob with
char* mvstring = new char[strlen(outfl)+strlen(inpfl)+1];
To be more concrete:
change respective strings to:
char* outfl = new char[strlen(usr)+strlen(tmppath)+5];

char* mvstring = new char[strlen(outfl)+strlen(inpfl)+5];
 

Avatar of gsbabu

ASKER

Hi

Even after increasing the size, i do get the core error. is there any other way do i get the loginname.

-gsbabu
Then you should compile the program with debugging (-g for gcc), then run it, get core file, run gdb (gnu debugger) with core file and your binary, then print out stack trace and find out where does your code goes off.

The other way:
add
   printf ("main() line 1");
at line 1, line 2 at line 2, etc...
then run the program and see where do you get until crash. Then you'll get the line with error.

After that, post that line here and i will help you find out the bug.
We need to know where it is crashing.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gsbabu

ASKER

Hi sereda,

Even after adding the print lines, it goes on print till the end. but when i do "strings core" i see the following(will it be helpful).

--
main() line 18
main() line 19
main() line 20
main() line 21
main() line 22
main() line 23
out of memory
n > max_size ()
pos > len
len - n1 > max_size () - n2                                                    
....
B7Hv
Cv4W
Infinity
coCa?
8stdiobuf
12istdiostream
12ostdiostream
Internal Compiler Bug: No runtime type matcher.
9exception
13bad_exception
bad_alloc
bad_alloc
bad_alloc
....

----
No, strings won't be much help.
As i got it, your program prints all debug strings till the end of main() and then dumps core?
Then this happens in local or global object destructor, presumably in string.
Avatar of gsbabu

ASKER

Hi jkr,

Initially it worked perfectly, but i will make one more count for the cron to execute. anyhow the points are for you. thanks for your comment. if you don't mind can i know your e-mail id. thanks once again.

--gsbabu
ugnasu01@thiru.vetri.com