Solved

getenv Crashes!

Posted on 2004-10-20
6
1,174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

724 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