Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   setting env vars in unix (http://www.programmingforums.org/showthread.php?t=11828)

Ghosty Nov 7th, 2006 10:38 PM

setting env vars in unix
 
Please examine the following code:

:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(){

  char * envVar;
  envVar = getenv ("PS1");

  cout << "PS1 value is currently: " << *envVar << endl;
  cout << "We will now use putenv to change the PS1 variable" << endl;

  putenv("PS1=<duck>");

  envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << *envVar << endl;
  return 0;
}


My book says that I have to use malloc aquire more space for "<duck>" but I've never used the malloc function. Can anyone tell me how to use malloc or now to use, perhaps, the new call?

Ghosty Nov 7th, 2006 11:58 PM

Updated. I'm using the malloc command successfully now, but I don't understand how to increase the size of my environment variable.

:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main(){

  char * envVar = static_cast<char*>(malloc(7));
  envVar = getenv ("PS1");

  cout << "PS1 value is currently: " << *envVar << endl;
  cout << "We will now use putenv to change the PS1 variable" << endl;

  putenv("PS1=<duck>");

  envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << *envVar << endl;

  free(envVar);
  return 0;
}


DaWei Nov 8th, 2006 1:00 AM

Right away you are setting envVar to an address of memory loaned to you by malloc. Right after that you are setting it to an address given to you by 'getenv'. You have lost the address given to you by malloc, thus you will not be able to free it without barfing in your own shirt pocket. I would suggest that you learn, first, about pointers, and secondly, about malloc/free and its requirements. Actually, since this is C++, you should shelve malloc/free in favor of new/delete.

Ghosty Nov 8th, 2006 1:34 AM

Fixed. Thanks for the reply DaWei. That code is rubbish anyway. My completed assignment is 4 lines of code. I didn't need malloc/free or new/delete as my book was misleading. I do need to get a refresher on pointers though.

:

//This code works
#include <iostream>
#include <cstdlib>

using namespace std;

int main(){

  char name []={"PS1=<duck>"};
  putenv(name);
  char * envVar = getenv ("PS1");
  cout << "PS1 value is currently: " << envVar << endl;

  return 0;
}


The Dark Nov 8th, 2006 3:53 AM

Note that this code will only work from within main. If you made a function using this code, the "name" array will go out of scope while the environment is still using it.
I suspect that it what your book was getting at, when you use putenv the pointer you pass in may be used by the runtime library until it the environment value is reset, so using a local variable is a problem. One way to get around this is to use malloc to allocate the space for the memory passed to putenv.

Ghosty Nov 8th, 2006 12:42 PM

That certainly seems like a better explanation The Dark. I don't really want to use malloc if I don't have to though. How would I use new to create space for the pointer that I'd be passing if I was making a function?

Polyphemus_ Nov 8th, 2006 4:00 PM

Quote:

Originally Posted by The Dark (Post 118617)
Note that this code will only work from within main. If you made a function using this code, the "name" array will go out of scope while the environment is still using it.

The entry point of your program is the main function. In the main functions, you usually call a number of functions. Does this mean your main function gets out of scope? Well, no. When you call function x, your variables from the main function are still on the stack. How else could you return from function x and continue in your main function?
Your local variables in function main will get unavailable once you return from the main function. And what happens when you quit the main function? Your program is finished, and the variable is no longer used anyway.

I'm not sure how this applies when running multiple threads, it may be OS specific (running another thread while main thread has finished) - another member can probably tell you that. I usually block the main thread when it has ended and other threads have not.

The Dark Nov 8th, 2006 9:02 PM

When i said "If you made a function using this code" i meant making the code as shown into a function (e.g. by changing the name from main to something else), not making some of the code into a function, but somehow leaving the variables in the main function. Sorry if that wasn't clear.

Ghosty, you can use new in the same way as malloc, in this case you could use something like:
:

  const char *eName = "PS1";
  const char *eVal = "<duck>";
  char *name = new char [strlen(eName) + 1 + strlen(eVal) + 1];
  strcpy (name, eName);
  strcat (name, "=");
  strcat (name, eVal);
  putenv(name);


I am not sure when you could delete the memory - from the man page I read, it seemed you could only delete it when you set the PS1 variable to something else.


All times are GMT -5. The time now is 1:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC