Process Environment Details (iv)---getenv, putenv, and SETENV functions

Source: Internet
Author: User

In the typical memory distribution of C programs in Linux, the sections of text, data, BSS, heap, and stack mentioned earlier are described in the Notes "C-memory distribution of C-language programs in the Wheat Academy". But in fact, the memory distribution of a C-language process is more than just these areas, as well as command-line parameters, environment variables, and shared libraries and mmap mapping areas. As shown------------------------------environment variable Areacommand-line argumentsStackshared libraries and mmap map areasHeapBSS SegmentData SegmentText Segment-------------------------------=====================================================The environment variables are usually given in the form of Name=value, as is the case with all the environment variables printed with the Environ pointer. You can use the GETENV function to obtain a value by giving the name. function Prototypes:function Parameters:
    • Name: The name of the environment variable you want to get
return Value:
    • The call successfully returns a pointer to value
    • Call failed to return NULL
=====================================================The putenv function is used to add or modify environment variables to the environment table. function Prototypes:function Parameters:
    • String: Pointer to an environment variable where the environment variable must be given as "Name=value"
return Value:
    • Call returned successfully 0
    • Returns a value other than 0 when the call fails
If the environment table does not have the name environment variable, the environment variable is added, and if the environment table already has the name of the environment variable, the previous value is deleted before being modified to the new value. =====================================================the setenv function is similar to the PUTENV function and can be used to add or modify environment variables. function Prototypes:function Parameters:
    • Name: Environment variable Name
    • Value: Environment variable values
    • Overwrite: Override option, when name exists in the environment table, if the value of overwrite is 0, the value of name is not modified, and if the value of overwrite is not 0, the value of name is modified.
return Value:
    • Call returned successfully 0
    • Return non 0 if call fails
=====================================================The putenv function and the setenv function are similar in function, but the two functions are different in terms of implementation, with the following differences:putenv function:
    • The putenv function fills the parameter string directly into the environment table and no longer allocates memory for the string "Name=value". If it is a string defined in a function, then when the function is called, the content that the string points to may be freed and the value of the name environment variable cannot be found.
setenv function:
    • Unlike the setenv function and the putenv function, it copies the contents of name and value to one copy and allocates memory for it, forming a string of "Name=value" and writing its address to the environment table. So there is no case of the above putenv, even if the function returns, the contents of name and value are released, and there is still a copy in.

Process Environment Details (iv)---getenv, putenv, and SETENV functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.