title: How to query string lengths with clGetPlatformInfo (and friends)?
body: <p><strong>Short version: Is there an official/correct way to query for the size of strings like <code>CL_PLATFORM_VENDOR</code>?</strong></p>
  
  <p><strong>Long version:</strong>
  Looking at OpenCL functions like <a href="http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/clGetPlatformInfo.html" rel="nofollow">clGetPlatformInfo</a> I want to find out how much space to allocate for the result.  The function has this signature:</p>
  
  <pre><code>cl_int clGetPlatformInfo(cl_platform_id platform,
      cl_platform_info param_name,
      size_t param_value_size,
      void *param_value,
      size_t *param_value_size_ret)
  </code></pre>
  
  <p>The docs say:</p>
  
  <pre><code>param_value_size
  
  Specifies the size in bytes of memory pointed to by
  param_value. This size in bytes must be greater
  than or equal to size of return type specified in the
  table below.
  </code></pre>
  
  <p>All of the return types are listed as <code>char[]</code>.  I wanted to know how much space to reserve so I called it like this, where I pass <code>0</code> for <code>param_value_size</code> and <code>NULL</code> for <code>param_value</code>, hoping to get the correct size return in <code>param_value_size_ret</code>:</p>
  
  <pre><code>  size_t size = 0;
    l_success = clGetPlatformInfo(platform_id,
                                  CL_PLATFORM_VENDOR, 0, NULL, &amp;size);
  
    if( l_success != CL_SUCCESS)
    {
      printf("Failed getting vendor name size.\n");
      return -1;
    }
    printf("l_success = %d, size = %d\n", l_success, size);
    char* vendor = NULL;
    vendor = malloc(size);
    if( vendor )
    {
      l_success = clGetPlatformInfo(platform_id,
                                    CL_PLATFORM_VENDOR, size, vendor, &amp;size);
      if( l_success != CL_SUCCESS )
      {
        printf("Failed getting vendor name.\n");
        return -1;
      }
      printf("Vendor name is '%s', length is %d\n", vendor, strlen(vendor));
    } else {
      printf("malloc failed.\n");
      return -1;
    }
  </code></pre>
  
  <p>It behaved as I had hoped, it return a size of 19 for the string, "NVIDIA Corporation" (size included null terminator) and <code>strlen</code> return 18.  Is this the "right" way to query for parameter size or am I just getting lucky with my vendor's implementation?  Has anyone seen this idiom fail on some vendor?  </p>
  
  <p><strong>Edit:</strong> The bit that is tripping me up is this, "This size in bytes must be greater than or equal to size of return type", it seems like when I pass <code>0</code> and <code>NULL</code> the call should fail because that's not greater than or equal to the size of the returned <strong>value</strong>.  I'm not sure why they say "return type".</p>