title: Dynamically allocate memory for an int array converted to string in C
body: <p>I have an integer array that is dynamically allocated by the following:</p>
  
  <pre><code>int *ranArr(int size) {
      int *arr= malloc(size *sizeof(int));
  
      for(int i = 0; i &lt; size; i++) {
          arr[i] = genRan(size);
      }
  
      return arr;
  }
  </code></pre>
  
  <p>I want to dynamically allocate memory for a string that I will convert my integer array into and store it in the string:</p>
  
  <pre><code>        int *arrayCopy = ranArr(size);
          int arrayCopyLen = size;
          char *arrayString= (char*) malloc(size * sizeof(char));
          int index = 0;
          for(int i = 0; i&lt; arrayCopyLen ; i++) {
              index += sprintf(&amp;arrayString[index], "%d ,", arrayCopy[i]);
          }
          int arrayStringLen = strlen(arrayString)+1;
  </code></pre>
  
  <p>This works for array sizes around 1000 but when I set the size to 2000 or so it breaks and I get a segmentation fault. </p>