Solved

getenv Crashes!

Posted on 2004-10-20
6
1,150 Views
Last Modified: 2008-03-17
I noticed that when i use getenv to get a particular environment variable, the program crashes...

However, i wanted the environemnt variables to be optional.  So, if the env variables are not set, then use the default ones.

But when it's not defined, ...it shuts down.
0
Comment
Question by:jewee
6 Comments
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12364148
Hi jewee,
Please post the relevant part of the code so that we can go further.

Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 12364166
What OS and what compiler are you using?

I had a similar problem like this using GNU compiler on SunOS.
The problem was some where else in the code, but it would currupt the memory so that it would crash when calling getenv.
0
 

Author Comment

by:jewee
ID: 12364250
I call the getEnvVar method in the constructor...

bool ViewForm::getEnvVar()
{
   serverHostName = getenv("SERVER_HOSTNAME");
   if (serverHostName == NULL)
   {
        printf("Error\n");
        return false;
    }
}

I am running on a SunOS
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Accepted Solution

by:
stefan73 earned 500 total points
ID: 12368002
Hi jewee,
Your code snippet looks OK. Just make sure that you don't access (dereference) serverHostName when it's NULL.

You get a core dump, right? Then you can compile your source with -g, run it until a core dump is created and then check either with Workshop or dbx:

cc -g file.c -o binary
./binary # crash!

dbx ./binary core

Cheers!

Stefan
0
 
LVL 30

Expert Comment

by:Axter
ID: 12379211
Is your constructor being called before entry into the main() function?
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12408758
serverHostName is going to point to read-only memory. You don't do anything like accidentally free/deleting it or writing to it do you?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
C++ to C# code conversion issue 4 112
How to print into std::string like sprintf using C++11? 12 86
Create a path if not exists 7 92
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question