Hi, someone suggested I post this here. I'm writing a shell and my environment variables seem to get corrupted for strange reasons. Here is one example of code. The correlation seems to be with the free(path) line the and dirPtr = ... line. If I don't remove one or the other, the 2nd printing of NEWDIR is (null) or nonexistent since apparently NEWDIR dies. Any ideas?

int main()
{

char *dirPtr;

char *path;
path = (char *)malloc(12);

strcpy(path, "NEWDIR=/usr");

putenv(path);

free(path);

std::cout << getenv("NEWDIR") << std::endl;
dirPtr = (char *)malloc(5);
std::cout << getenv("NEWDIR") << std::endl;

free(dirPtr);
return 0;

}