title: Thrust CUDA allocating char * to device_vector of objects
body: <p>I am having trouble making a deep copy of a host_vector to device_vector. I think that I am having an issue modifying the value of an element stored in a device_vector. You can find a compilable version at the bottom but the code in question is the following (i put stars on the rows that trigger the segmentation fault):</p>
  
  <pre><code>thrust::host_vector&lt;CharArr&gt; hostToSort(size);
              thrust::host_vector&lt;long long&gt; hostToSortRow(size);
              for(int i =0; i &lt; size; i++){
                  CharArr sortRow;
                  sortRow.value = arrayToSort[i];
                  sortRow.length = strlen(arrayToSort[i]);
                  hostToSort[i] = sortRow;
                  hostToSortRow[i] = arrayToSortRow[i];
              }
              thrust::device_vector&lt;CharArr&gt; deviceArrayToSort =hostToSort;
              //thrust::copy(hostToSort.begin(),hostToSort.end(),deviceArrayToSort.begin());
  //           = ;// (arrayToSort,arrayToSort + size);
              thrust::device_vector&lt;long long&gt; deviceArrayToSortRow = hostToSortRow;//(arrayToSortRow,arrayToSortRow + size);
  
             // thrust::sort(deviceArrayToSort.begin(),deviceArrayToSort.end());
              for(int i = 0; i &lt; size; i++){
  
                  char * hostString = hostToSort[i].value;
                  int sizeString = strlen(hostString);
                  char * deviceString = 0;
  
                  CharArr * deviceCharArr = (&amp;deviceArrayToSort[i]).get();
                  cudaMalloc((void **) deviceString,sizeString);
                  cudaMemcpy(deviceString,hostString,sizeString,cudaMemcpyHostToDevice);
              ****    deviceCharArr-&gt;length = sizeString;
              ****    deviceCharArr-&gt;value = deviceString;
              }
  </code></pre>
  
  <p>What happens is that when we arrive at the actual assignment<br>
      deviceCharArr->value = deviceString
  It throws a Segmentation Fault error. I am very new to CUDA and apologize if there is an obvious answer but I have not been able to find many examples of people allocating char * on devices.</p>
  
  <p>Complete Compilable version is here</p>
  
  <pre><code>#include &lt;thrust/device_vector.h&gt;
  #include &lt;thrust/host_vector.h&gt;
  #include &lt;thrust/reduce.h&gt;
  #include &lt;thrust/device_vector.h&gt;
  #include &lt;thrust/host_vector.h&gt;
  #include &lt;thrust/sort.h&gt;
  
  #include &lt;thrust/reduce.h&gt;
  
  
  typedef struct{
  
      char * value;
      int length;
  } CharArr;
  
  
  struct CharArrayCmp{
      __host__ __device__
        bool operator()(const CharArr &amp; o1, const CharArr &amp; o2) {
            return this-&gt;compare(o1.value,o1.length,o2.value,o2.length);
        }
  
      __host__ __device__ bool compare (const char * src, int lenSrc, const char * dst, int lenDest)
      {
          int end;
          if(lenSrc &gt; lenDest){
              end = lenDest;
          }else{
              end = lenSrc;
          }
          for(int i = 0; i &lt; end; i++){
              if(src[i] &gt; dst[i]){
                  return false;
              }else if(src[i] &lt; dst[i]){
                  return true;
              }
          }
          if(lenSrc &gt;= lenDest){
              return false;
          }
          return true;
      }
  };
  
  
  void sortCharArrayHost(char ** arrayToSort, long long * arrayToSortRow,long long size){
      std::cout &lt;&lt;"about to start LongIndex" &lt;&lt;std::endl;
  
              thrust::host_vector&lt;CharArr&gt; hostToSort(size);
              thrust::host_vector&lt;long long&gt; hostToSortRow(size);
              for(int i =0; i &lt; size; i++){
                  CharArr sortRow;
                  sortRow.value = arrayToSort[i];
                  sortRow.length = strlen(arrayToSort[i]);
                  hostToSort[i] = sortRow;
                  hostToSortRow[i] = arrayToSortRow[i];
              }
              /*thrust::device_vector&lt;CharArr&gt; deviceArrayToSort =hostToSort;
              //thrust::copy(hostToSort.begin(),hostToSort.end(),deviceArrayToSort.begin());
  //           = ;// (arrayToSort,arrayToSort + size);
              thrust::device_vector&lt;long long&gt; deviceArrayToSortRow = hostToSortRow;//(arrayToSortRow,arrayToSortRow + size);
  
             // thrust::sort(deviceArrayToSort.begin(),deviceArrayToSort.end());
              for(int i = 0; i &lt; size; i++){
                  char * deviceString = 0;
                  char * hostString = hostToSort[i].value;
                  int size = strlen(hostString)*sizeof(char);
                  int cudaStatus;
                  CharArr * deviceCharArr = (&amp;deviceArrayToSort[i]).get();
                  cudaStatus = cudaMalloc((void **) deviceString,size);
                  cudaStatus = cudaMemcpy(deviceString,hostString,size,cudaMemcpyHostToDevice);
  
                  (&amp;deviceArrayToSort[i]).get()-&gt;value = "";
              }
  */
  //          thrust::sort_by_key(deviceArrayToSort.begin(),deviceArrayToSort.end(),deviceArrayToSortRow.begin(),CharArrayCmp());
              thrust::sort_by_key(hostToSort.begin(),hostToSort.end(),hostToSortRow.begin(),CharArrayCmp());
  
              //copy the contents back into our original array to sort now sorted
            //  hostToSort = deviceArrayToSort;
              for(int i =0; i &lt; size; i++){
                  arrayToSort[i] = hostToSort[i].value;
              }
  //          thrust::copy(deviceArrayToSortRow.begin(),deviceArrayToSortRow.end(),arrayToSortRow);
              thrust::copy(hostToSortRow.begin(),hostToSortRow.end(),arrayToSortRow);
  
  
  }
  void sortCharArrayDevice(char ** arrayToSort, long long * arrayToSortRow,long long size){
      std::cout &lt;&lt;"about to start LongIndex" &lt;&lt;std::endl;
  
              thrust::host_vector&lt;CharArr&gt; hostToSort(size);
              thrust::host_vector&lt;long long&gt; hostToSortRow(size);
              for(int i =0; i &lt; size; i++){
                  CharArr sortRow;
                  sortRow.value = arrayToSort[i];
                  sortRow.length = strlen(arrayToSort[i]);
                  hostToSort[i] = sortRow;
                  hostToSortRow[i] = arrayToSortRow[i];
              }
              thrust::device_vector&lt;CharArr&gt; deviceArrayToSort =hostToSort;
              //thrust::copy(hostToSort.begin(),hostToSort.end(),deviceArrayToSort.begin());
  //           = ;// (arrayToSort,arrayToSort + size);
              thrust::device_vector&lt;long long&gt; deviceArrayToSortRow = hostToSortRow;//(arrayToSortRow,arrayToSortRow + size);
  
             // thrust::sort(deviceArrayToSort.begin(),deviceArrayToSort.end());
              for(int i = 0; i &lt; size; i++){
  
                  char * hostString = hostToSort[i].value;
                  int sizeString = strlen(hostString);
                  char * deviceString = 0;
  
                  CharArr * deviceCharArr = (&amp;deviceArrayToSort[i]).get();
                  cudaMalloc((void **) deviceString,sizeString);
                  cudaMemcpy(deviceString,hostString,sizeString,cudaMemcpyHostToDevice);
                  deviceCharArr-&gt;length = sizeString;
                  deviceCharArr-&gt;value = deviceString;
              }
  
              thrust::sort_by_key(deviceArrayToSort.begin(),deviceArrayToSort.end(),deviceArrayToSortRow.begin(),CharArrayCmp());
          //copy the contents back into our original array to sort now sorted
              for(int i =0; i &lt; size; i++){
                  arrayToSort[i] = (&amp;deviceArrayToSort[i]).get()-&gt;value;
              }
              thrust::copy(deviceArrayToSortRow.begin(),deviceArrayToSortRow.end(),arrayToSortRow);
  
  
  
  }
  int main()
  {
      char ** charArr = new char*[10];
  
      charArr[0] = "zyxw";
      charArr[1] = "abcd";
      charArr[2] = "defg";
      charArr[3] = "werd";
      charArr[4] = "aasd";
      charArr[5] = "zwedew";
      charArr[6] = "asde";
      charArr[7] = "rurt";
      charArr[8] = "ntddwe";
      charArr[9] = "erbfde";
  
      long long * rows = new long long[10];
      for(int i = 0; i &lt; 10;i++ ){
          rows[i] = i;
      }
  
      sortCharArrayHost(charArr,rows,10);
  
      for(int i = 0; i &lt; 10; i++){
          std::cout&lt;&lt;"Row is "&lt;&lt;rows[i]&lt;&lt;" String is "&lt;&lt;charArr[i]&lt;&lt;std::endl;
  
      }
  
  
      charArr[0] = "zyxw";
      charArr[1] = "abcd";
      charArr[2] = "defg";
      charArr[3] = "werd";
      charArr[4] = "aasd";
      charArr[5] = "zwedew";
      charArr[6] = "asde";
      charArr[7] = "rurt";
      charArr[8] = "ntddwe";
      charArr[9] = "erbfde";
  
  
      for(int i = 0; i &lt; 10;i++ ){
          rows[i] = i;
      }
      sortCharArrayDevice(charArr,rows,10);
  
          for(int i = 0; i &lt; 10; i++){
              std::cout&lt;&lt;"Row is "&lt;&lt;rows[i]&lt;&lt;" String is "&lt;&lt;charArr[i]&lt;&lt;std::endl;
  
          }
  
  }
  </code></pre>