Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use a pointer as the 1-based index of an array?

The index of array is an integer scalar variable or numerical value lateral, and is rarely seen as an integer pointer although any pointer per se is an address location number (unsigned scalar value), and by C/C++ standard it should point to a pre-allocated block of memory. Many scholars or experts think that if you assign a pointer to a non-pre-allocated block of memory, it may immediately generate some kind of either predictable or unpredictable undefined behavior (UB), even if you never try to dereference it. However, some other experts (see below) think that if you never try to dereference it, it actually does not produce UB at all if you just assign a number to it and use it (but no dereference), for example,

char * ptr = (char*)1;
ptr++;
ptr = +20;

If you read this far, it means you are interested in getting to know the facts beside C/C++ standard. Here, I provide the following link to show that Nvidia edge device AI platform or DriveOS SDK has this pointer defined below:

typedef uintptr_t   NvSciStreamCookie;

The API doc stated that NvSciStreamCookie cookie is any non-zero value, and more importantly it is typically “a 1-based index into an array of structures or …”.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have been using the above pointer for our critical real-time AI application project and some of us are confused by using pointer as 1-based index of array. I personally discussed it with Nvidia expert who wrote/developed the code, and we agreed on that if we never dereference it, it is OK. However, some experts here might think otherwise. If so, please advise. Thank you!

Using typedef uintptr_t NvSciStreamCookie as a 1-based index array seems not reproduce UB.

>Solution :

You have misinterpreted this API. It is not using pointers as array indices, 1-based or otherwise. uintptr_t is an integer type.

It is entirely valid to store arbitrary in-range integers into an integer type. When the docs say that

Typically, the cookie is either a 1-based index into an array of structures or a pointer directly to the structure

the docs do not mean that C has any native support for 1-based array indexing. Rather, since

The cookie is any non-zero value that the application chooses to look up data structures for the packet.

actual array indices wouldn’t work. However, adding 1 to the array indices solves this problem. It is natural to interpret the result as a 1-based array index, but there is no actual C language feature that would treat it as such.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading