[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

segmentation fault on malloc

Asked by oumer in C++ Programming Language, Xandros Linux

Tags: fault, segmentation, malloc

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/Emulator_Project/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(command);
 
  if (return_value != 0)
    {
      cout << "The configuration file :" << argv[1] << ": doesn't exist" << endl;
      exit(-1);
    }
}
[+][-]12/19/03 05:02 AM, ID: 9971640Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/03 05:13 AM, ID: 9971689Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/19/03 06:22 AM, ID: 9972126Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/03 07:24 AM, ID: 9972634Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/19/03 10:58 AM, ID: 9974238Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/03 01:14 PM, ID: 9975130Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C++ Programming Language, Xandros Linux
Tags: fault, segmentation, malloc
Sign Up Now!
Solution Provided By: grg99
Participating Experts: 4
Solution Grade: A
 
[+][-]12/19/03 02:36 PM, ID: 9975525Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/03 04:39 PM, ID: 9976091Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/03 04:54 PM, ID: 9976133Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89