?
Solved

getenv Crashes!

Posted on 2004-10-20
6
Medium Priority
?
1,191 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Accepted Solution

by:
stefan73 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

741 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