LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2003, 03:55 PM   #1
sgm
LQ Newbie
 
Registered: Jan 2003
Posts: 1

Rep: Reputation: 0
Question segmentation fault?


I am writing a CGI program for RedHat Linux 7.2 and came across a strange problem (segmentation fault) when running my program. The problem is also exhibited when running from the bash shell prompt so I have eliminated all of the CGI functionality to try to get to the root of the problem. Here is a simple program that exhibits the problem:

//filename test.cc
#include <stdio.h>
#include <algorithm>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string strMethod = getenv("REQUEST_METHOD");

return 0;
}

It was compiled using a makefile containing this:

all:test

test: test.o
g++ -o test test.o

test.o: test.cc
g++ -Wall -c -o test.o test.cc

I would really appreciate any insight that anyone can provide.

Thanks,

sgm
 
Old 01-25-2003, 08:41 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Looks like the environment variable REQUEST_METHOD never exists. You are calling the string constructor on a NULLpointer. This code will not cause a seg faut.

Code:
#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream.h>

using namespace std;

int main(int argc, char *argv[])
{
   int iSize;
   char * pEnv;

   if (( pEnv = getenv("REQUEST_METHOD" )) == NULL )
   {
      cout << "Env variable REQUEST_METHOD does not exist" << endl;
   }
   else
   {
     string strMethod( getenv("REQUEST_METHOD"));
     cout << "String is :" << strMethod.c_str() << endl;
   }
return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration