Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1063355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:46:22+00:00 2026-05-16T18:46:22+00:00

I am trying to use putenv() on UNIX by concatenating str1 and str2 before

  • 0

I am trying to use putenv() on UNIX by concatenating str1 and str2 before that.
I want to add a variable, or modify a variable, in the environment, so I am calling putenv() (or I could call setenv() identically).

Basically, I receive str1 and str2, I create str1=str2 and pass it in putenv() as a parameter.

The code I am showing works but when I uncomment the free() call, then it does not: the variable do not get added/modified for the environment.

size_t size = strlen(str1) + strlen(str2) + 2; // 2 is for the '\0' and the '='
char *tmp = (char *) malloc(sizeof(char) * size);
char *p;
int pos = 0;

// Copy first word
p = str1;
while (*p != NULL) {
    tmp[pos++] = *p++;
}

// Add the '='
tmp[pos++] = '=';

// Copy second word
p = str2;
while (*p != NULL) {
    tmp[pos++] = *p++;
} 

// Add null character
tmp[pos] = '\0';

int ret = putenv(tmp);
if (ret != 0) {
    perror("putenv failed");
}

//free(tmp); // This line is the problem when not commented

I apologize for the code redundancy, I know that the two while loops are identical. The issue that I have is that if I uncomment the free statement, then calling “env” to print the environment, the putenv will not have added the value.

I am not sure why is that. Right now to have it work, I am having a memory leak which I do not like. When I used an array, rather than a pointer, it would produce the same issue as uncommenting free.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:46:22+00:00Added an answer on May 16, 2026 at 6:46 pm

    putenv() requires the string that is set into the environment to exist, because it doesn’t copy the string; it uses a pointer to the string provided for the argument.

    This is noted in the OpenGroup description of putenv() in a rather vague way:

    http://www.opengroup.org/onlinepubs/009695399/functions/putenv.html

    “A potential error is to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment.”

    Two possible solutions for your problem are:

    1 – To get what you want using putenv(), you could use a static string, or otherwise use a string that does not go out of scope until the environment variable is unneeded.

    2 – Alternatively, use setenv(const char *envname, const char *envval, int overwrite), which is easier to use than putenv, allocates memory and copies the string, and does not require you to concatenate strings like you are doing right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set a variable that should be accessible from outside PHP. Ideally
I am trying use Date Picker and on Select i want to display the
I'm trying use jQuery to modify the HTML of the WordPress [caption] shortcode, whose
I'm trying use eco for client-side templating. I have multiple .eco templates that I'd
I have a regex that I'm trying use to validate against strings. Trying to
I have a 3rd party DLL that I am trying to use in a
I'm trying use double type in openCL, but doesn't work anyway, i want use
Trying to use Java's regexp I want to match /app, /app/.* , but not
I am trying use the jQuery table sorter plugin for a table that is
I am trying use the mysql connector in c++ in ubuntu. It appears that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.