Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic C string (char*) weird behavior
    text
    copied!<p>I got 2 functions:<br/> - <code>stringCopy()</code> which copy the parameter <code>strToCopy</code> into another string dynamically allocated applying a sanitize (see 2nd function)<br/> - <code>_sanitized()</code> which returns a dynamically allocated uppercased version of the parameter and removing non-letters char (such as numeric values &amp; spaces).</p> <p>Considering the following, I got an <code>EXC_BAD_ACCESS</code> because of <code>k</code> growing too much.</p> <pre><code>char* _sanitized(const char* str) { char* uppercasedStr = malloc(sizeof str); int k = 0; // Index de parcours de la chaîne originale int i = k; // Index dans la nouvelle chaîne char evaluatedChar; while ( (evaluatedChar = str[k]) != '\0') { if ('A' &lt;= evaluatedChar &amp;&amp; evaluatedChar &lt;= 'Z') { uppercasedStr[i] = evaluatedChar; i++; } else if ('a' &lt;= evaluatedChar &amp;&amp; evaluatedChar &lt;= 'z') { uppercasedStr[i] = evaluatedChar-32; i++; } k++; } i++; uppercasedStr[i] = '\0'; return uppercasedStr; } char* stringCopy(char* strToCopy) { char* uppercaseStr = _sanitized(strToCopy); char* copiedStr = malloc(sizeof uppercaseStr); int k = 0; while (uppercaseStr[k] != '\0') { copiedStr[k] = uppercaseStr[k]; k++; } k++; copiedStr[k] = '\0'; free(uppercaseStr); return copiedStr; } </code></pre> <p>I also noticed that when I copy char from <code>uppercaseStr</code> into <code>copiedStr</code> it modifies <code>uppercaseStr</code> in the same time which cause the overflow...</p>
 

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