简体   繁体   中英

Number of components in the pointer array

I have a method declaration like this:

void createProgram(int32_t dev, int32_t* pixelId, int32_t* vertexId)

How can I discover the number of elements in the pixelId and vertexId arrays?

For example, I tried to find out the contained number of elements with:

sizeof(pixelId)/sizeof(int32_t)

but this isn't the solution, sizeof(pixelId) returns the size of the pointer.

Is there another solution?

Thanks

Based on your title, I think you want to find out how many elements are being passed in the pixelId and vertexId parameters. In short, you can't - all a pointer does is say that at that point in memory there are int32_t 's, not how many. You either have to:

  1. Specify the size of the array(s) in an extra parameter
  2. Use a collection encapsulation class like std::vector to pass them
  3. Close the array with a 'magic' number, that cannot normally occur in the array, to indicate its end. Strings for example are usually zero-terminated char* 's, which is valid since a zero-byte is not a valid ASCII or Unicode character.

Without using one of these 3 methods a pointer is just a memory address and an indication of the datatype it's pointing to, not how many elements, if any at all, are there.

You tried:

sizeof(pixelId)/sizeof(int32_t)

Since pixelId is a int32_t* , it's a memory pointer, which is either 4 or 8 bytes based on whether you're on a 32- or 64-bit OS. sizeof(int32_t) is 4 bytes by definition, so this calculation will either return 2 or 1 , but not an answer related to what is being pointed to by the pointer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM