Search code examples
c++vlfeat

Purpose of void const * argument in function


I'm trying to use a library function from VLFeat that calls for data to be a const void *. I don't really understand how to create the data and then pass it in.

This is the function call:

void vl_kmeans_init_centers_with_rand_data  (VlKMeans * self, void const * data,
    vl_size dimension, vl_size numData, vl_size numCenters)

The data argument is the one that throws me. I tried building a random data matrix to test the kmeans clustering function, but I can't figure out how to use the data. In other words, this function requires this argument. But for it to be useful, I have to understand what how to cast/create/load the data to make it work. That means, I need to understand the purpose of the const void * type in the argument.

Any help would be highly appreciated.

Note: I do understand what const means, but, for example, I can't figure out how I can iteratively build const data (i.e. fill a matrix with dual for loops)

Thanks!


Solution

  • const, despite the best efforts from the standardization commitee to confuse you, does not mean "constant". It means "read-only".

    By declaring its parameter as a pointer-to-const, this function states that your data won't be copied on its way in, but the function's code will only be able to read it, not modify it.

    So just build your matrix the regular way (non-const, since you need to complete it with loops), and pass it in, confident that it will return unchanged.

    Unless... the function's code casts away the const. Which should get the guy who wrote it fired in no time.