The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Segmentation Fault(coredump) Manish4 Shell Programming and Scripting 1 12-06-2008 05:12 PM
memory fault (coredump) BhuvanEswar SUN Solaris 2 10-17-2008 05:10 AM
Memory fault(coredump) fkaba81 Ubuntu 2 03-26-2008 01:56 PM
Memory fault(coredump) ralo High Level Programming 0 10-14-2005 05:35 AM
Coredump (memory fault) araziki UNIX for Dummies Questions & Answers 1 11-27-2002 12:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-01-2009
Kattoor Kattoor is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 25
Memory fault(coredump) while using Environment variables


Hi,

Actually I am facing one issue while using the getenv() in the C/C++ program.

I want to take the file path from environment variables and if am not defining the environment path, its showing the message like this…!

Memory fault(coredump)

Actually I want to handle the error , when I am not defining the enviornment path.

Eg:code:


Code:
char *LOG_FILE = getenv("LOG_PATH");
FILE * pFile = fopen(LOG_FILE,"a");
int main ()
{
    char name [100];
    gets (name);
    fprintf (pFile,"\nName %s”,name);
    return 0;
}


If am getting the enviornment path locally rather than in the program mentioned above, then its working fine.But I have to use it globally.

Can anybody help me out in this?

Thanks in Advance...!
  #2 (permalink)  
Old 05-01-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,972
This code must be C++, since you cannot declare global variables from function return values in C. This bug is why I think that feature was a bad idea... fopen(getenv("LOG_PATH"),"a"); might work but will crash if LOG_PATH is not set, and you should be calling getenv and fopen in main instead anyway, or at least from a function which knows the proper order to set them in, which the compiler doesn't.

Last edited by Corona688; 05-01-2009 at 12:13 PM..
  #3 (permalink)  
Old 05-01-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
You can declare and initialize variables globally but it will crash if the env variable LOG_PATH is not set and make sure that the proper header files are included...

Code:
#include <stdio.h>
#include <stdlib.h>

char *LOG_FILE = getenv("LOG_PATH");
FILE *pFile = fopen(LOG_FILE, "a");

int main()
{
    char name[100];
    gets(name);
    fprintf (pFile,"\nName %s", name);
    return 0;
}

  #4 (permalink)  
Old 05-01-2009
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,972
Quote:
Originally Posted by shamrock View Post
You can declare and initialize variables globally
Only C++ lets you initialize global variables from function return values, and I don't think it guarantees what order they're set in -- it could call fopen before getenv gets called. C only lets you initialize them from constant values.
  #5 (permalink)  
Old 05-02-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Quote:
Originally Posted by Corona688 View Post
Only C++ lets you initialize global variables from function return values, and I don't think it guarantees what order they're set in -- it could call fopen before getenv gets called. C only lets you initialize them from constant values.
Yes I should have pointed out that this is only possible with a C++ compiler...C compilers don't understand global variable initializations using functions. So you can write in C but compile with a C++ compiler if that makes sense.
  #6 (permalink)  
Old 05-02-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,805

Code:
char *LOG_FILE = NULL;
FILE * pFile =  NULL;

int main ()
{
    char name [100]={0x0};
    LOG_FILE=getenv("LOG_FILE");
    pfile = fopen(LOG_FILE,"a");
    gets (name);
    if(pfile!=NULL)
        fprintf (pFile,"\nName %s”,name);
    return 0;
}

both pfile and LOG_FILE are now global.
  #7 (permalink)  
Old 05-03-2009
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 753
Well I think jim mcnamara's solution is the best one.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0