Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing to strcpy() to get parts of a char* causes a program crash
    primarykey
    data
    text
    <p>I am trying to grab bits from a char* then store it in a map. I use strcpy &amp; strncpy to do this but my program crashes when I go to use strncpy (it compiles but crashes at runtime).</p> <p>Why is my program crashing &amp; how can I successfully grab bits from the char* and store them in a map?</p> <p><strong>EDIT: after taking advice I have initialised the char* but it still crashes?</strong></p> <pre><code>// I did the following char* variable = ""; char* value = ""; map &lt;string, string&gt; GetEvironmentVariablesEx() { map &lt;string, string&gt; envVariables; char* environVar = GetEnvironmentStrings(); char* pos = strchr( environVar, '\0' ); // This is what environVar contains: environVar = "=::=::\0APPDATA=c:/users/user1/desktop\0OS=windowsNT\0\0"; // note the string is double null terminated // Skip over the "=::=::\0" of the environVar string if ( pos != NULL ) { environVar = ++pos; pos = strchr( environVar, '\0' ); } else return envVariables; while ( true ) { char* delim = strchr( environVar, '=' ); char* variable; char* value; if ( delim == NULL ) { printf("Environment variable string is badly formatted"); break; } // The crash occurs at the below line: the crash occurs at runtime strncpy( variable, environVar, strlen(delim) ); strcpy( value, environVar+strlen(delim) ); printf( "Variable is: %s = %s \n", variable, value ); envVariables.insert( pair&lt;string, string&gt;(string(variable), string(value)) ); environVar = ++pos; // find the "\0\0" that identifies the end of environVar if ( pos != NULL &amp;&amp; *pos == 0 ) { break; } pos = strchr( environVar, '\0' ); } FreeEnvironmentStrings( environVar ); return envVariables; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload