Hi,
I started using some propritory linux distribution (which is based on RedHat) about three days ago. I have copied some c++
programs which I was working on from my personal RedHat 8.0 machine, but somethings seems to be wrong.
The programs compile perfectly but when I
run them I start getting segmentation faults (they run perfectly well on
the redhat machine), and always crash at lines where the code has something to do with allocating memory.
For example, I have attached a simple c++ program
(test_malloc.cpp) that gives me a segmentation fault at line 19: "string
s1(full_path)" as this is doing some malloc calls internally. I also has
the same lines in another big program (I cut and pasted some of the code
into the test_malloc.cpp file), but this time it passes the line 19 and
has the same segmentation fault at line 23 "char *command = (char *)
calloc (.......".
So what can be the problem?
The versions of glibc is 2.2.... on the redhat machine and on the propritory distribution it is is 2.3...... Can it be due to that?
Thanks in advance,
Oumer
#include <iostream>
#include <string.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main (int argc, char *argv[])
{
time_t curtime;
curtime = time (NULL);
char * asc_time = asctime (localtime (&curtime));
char *log_dir = "/home/oumer/projects/Emul
ator_Proje
ct/logs/";
char *full_path = (char *) calloc(strlen(asc_time) + strlen(log_dir),1) ;
strcpy(full_path, log_dir);
strcat(full_path,asc_time)
;
string s1(full_path);
s1= s1.substr(0, s1.length()-1);
replace(s1.begin(), s1.end(), ' ', '_');
replace (s1.begin(), s1.end(), ':', '-');
char *command = (char*) calloc(strlen(s1.c_str()) + 100, 1) ;
strcpy(command,"ls ");
strcat(command, argv[1]);
strcat(command," 2> /dev/null");
int return_value=system(comman
d);
if (return_value != 0)
{
cout << "The configuration file :" << argv[1] << ": doesn't exist" << endl;
exit(-1);
}
}