Solved

getenv Crashes!

Posted on 2004-10-20
6
1,137 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

803 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