Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:29:21+00:00 2026-05-25T20:29:21+00:00

Short version: Is there an official/correct way to query for the size of strings

  • 0

Short version: Is there an official/correct way to query for the size of strings like CL_PLATFORM_VENDOR?

Long version:
Looking at OpenCL functions like clGetPlatformInfo I want to find out how much space to allocate for the result. The function has this signature:

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)

The docs say:

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.

All of the return types are listed as char[]. I wanted to know how much space to reserve so I called it like this, where I pass 0 for param_value_size and NULL for param_value, hoping to get the correct size return in param_value_size_ret:

  size_t size = 0;
  l_success = clGetPlatformInfo(platform_id,
                                CL_PLATFORM_VENDOR, 0, NULL, &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, &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;
  }

It behaved as I had hoped, it return a size of 19 for the string, “NVIDIA Corporation” (size included null terminator) and strlen 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?

Edit: 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 0 and NULL the call should fail because that’s not greater than or equal to the size of the returned value. I’m not sure why they say “return type”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:29:22+00:00Added an answer on May 25, 2026 at 8:29 pm

    Yes, it is the right way.

    In the same doc you’ve mentioned:

    If param_value is NULL, it is ignored.

    And below:

    Returns CL_SUCCESS if the function is executed successfully. Otherwise, it returns the following:

    …

    CL_INVALID_VALUE if param_name is not one of the supported values or if size in bytes specified by param_value_size is less than size of return type and param_value is not a NULL value.

    So even if it is not stated explicitly, if param_value is NULL no error should be produced, so the code is supposed to work properly.

    Here is a piece of code from Khronos OpenCL C++ bindings (specs). They too do it this way, and I think it counts as “official”:

    // Specialized GetInfoHelper for STRING_CLASS params
    template <typename Func>
    struct GetInfoHelper<Func, STRING_CLASS>
    {
        static cl_int get(Func f, cl_uint name, STRING_CLASS* param)
        {
            ::size_t required;
            cl_int err = f(name, 0, NULL, &required);
            if (err != CL_SUCCESS) {
                return err;
            }
    
            char* value = (char*) alloca(required);
            err = f(name, required, value, NULL);
            if (err != CL_SUCCESS) {
                return err;
            }
    
            *param = value;
            return CL_SUCCESS;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short Version: Is there a/what is the suggested way to return error details to
short version: Is there a good time based sampling profiler for Linux? long version:
The short version: is there a way to to write an and or an
Short version: In pure Java EE 6, is there something like Spring's Authentication Processing
Short version: Is there a simple, built-in way to identify the calling view in
Short version: Is there a simple way to get the value of a primary
Short version: Is there a way to check-in a change to a git repository
Short Version Is there a way to force (or provide a hint to) Microsoft
Is there a cleaner way to get the short version hash of HEAD from
Short version: Is there way to achieve in Python the same effect achieved by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.