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

  • Home
  • SEARCH
  • 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 6027773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:38:30+00:00 2026-05-23T04:38:30+00:00

The following is in C++. I have a string that contains the environment variables

  • 0

The following is in C++.
I have a string that contains the environment variables I need to split it at the declaration of each variable & store it in a string:

char* envVars = "=::=::\0system=blah\0othervar=blah\0"

So I am using cstring functions to split the string at the occurence of the null terminator char ‘\0’ but it’s just going into an infinite loop. Why?

SOLUTION Found: look at code comments:

vector <string> GetEvironmentVariables()
{
   vector <string> envVariables;
   char* environVar = GetEnvironmentStrings();
   char* pos        = strchr( environVar, '\0' );

   // As far as I know environVar =::=::\0environVar1=...\0environVar2=...\0" 
   // so the string has many NULL terminators  

   while ( pos != NULL )
   {
       char* buffer;
       strncpy( buffer, environVar, strlen(pos) );   // on the 1st iteration: buffer SHOULD = "=::=::\0", 2nd buffer SHOULD = "environVar=...\0"
       envVariables.push_back( string(buffer) );
       environVar = pos;                            // SOLUTUION: I need to move over the '\0' pos points to so: environVar = ++pos;
       pos        = strchr( environVar, '\0' );

       printf("Var: %s \n", envVariables.back().c_str() ); 
       printf("env: %s \n", environVar);
       system("PAUSE");
       // prints out this:
       // Var: cRek (same junk each iteration)
       // env: 
       // Press any key to continue....
   }

   FreeEnvironmentStrings( environVar ); 
   return envVariables;       
}
  • 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-23T04:38:30+00:00Added an answer on May 23, 2026 at 4:38 am

    I would have expected this to exit immediately, but actually the man page says:

    The terminating null character is considered to be part of the string; therefore if c is '\0', the functions locate the terminating '\0'.

    Of course, the result of pos = strchr(environVar, '\0'); is that *pos == '\0' and strlen(pos) == 0. So you always copy exactly zero characters. Not useful.

    You also set environVar = pos;, without skipping over the NUL character. So the next call to strchr returns environVar and no more progress is ever made.


    You’ve also forgotten to initialize buffer, you’re passing a wild pointer to strncpy which will corrupt a random part of memory. This bug will probably rear its ugly head as soon as you fix the fact that the length parameter is always zero.

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

Sidebar

Related Questions

I have the following C# which simply replaces parts of the input string that
I have the following code that sets a cookie: string locale = ((DropDownList)this.LoginUser.FindControl(locale)).SelectedValue; HttpCookie
I have a C# application that includes the following code: string file = relativePath.txt;
I have a string that contains an int. How can I parse the int
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I have an SSIS package that does the following: Selects the connection strings from
I have following string String str = replace :) :) with some other string;
Suppose I have following string: String asd = this is test ass this is
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
I have the following string and I would like to remove <bpt *>*</bpt> and

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.