ExylonFiber
Well-Known Member
- Joined
- Apr 25, 2020
- Messages
- 26
- Thread Author
-
- #1
I cannot figure out why +1 or -1 is added to a cast or a buffer such as,
as listed on the MSDN page:
I searched google and could not find the answer as to why this practice is implemented in programming pertaining to buffers and structures in particular.
Code:
firmwareRequest = (PFIRMWARE_REQUEST_BLOCK)(srbControl + 1);
as listed on the MSDN page:
Upgrading Firmware for an NVMe Device (Legacy Approach) - Windows drivers
Legacy approach for firmware updates on NVMe storage devices using IOCTL_SCSI_MINIPORT with SRB I/O control structures.
docs.microsoft.com
I searched google and could not find the answer as to why this practice is implemented in programming pertaining to buffers and structures in particular.
Solution
The practice of adding
Here,
+1 or -1 to a pointer in C or C++ is often seen when working with arrays or structures to access elements adjacent in memory. Let's break down the expression you provided:
C:
firmwareRequest = (PFIRMWARE_REQUEST_BLOCK)(srbControl + 1);
srbControl and firmwareRequest are pointers, and they are cast to a specific structure type PFIRMWARE_REQUEST_BLOCK. Adding +1 to srbControl increments the memory address by the size of the type it points to. This operation effectively moves the pointer to the next block of memory, allowing firmwareRequest to point to the next element or structure in the memory. Similarly, if you had -1 instead, it would...- Joined
- Mar 14, 2023
- Messages
- 100,453
The practice of adding
Here,
+1 or -1 to a pointer in C or C++ is often seen when working with arrays or structures to access elements adjacent in memory. Let's break down the expression you provided:
C:
firmwareRequest = (PFIRMWARE_REQUEST_BLOCK)(srbControl + 1);
srbControl and firmwareRequest are pointers, and they are cast to a specific structure type PFIRMWARE_REQUEST_BLOCK. Adding +1 to srbControl increments the memory address by the size of the type it points to. This operation effectively moves the pointer to the next block of memory, allowing firmwareRequest to point to the next element or structure in the memory. Similarly, if you had -1 instead, it would decrement the address by the size of the type, moving the pointer to the previous memory block. These kinds of pointer manipulations are common when dealing with structs in C programming to access the next or previous element in an array or a sequence of structures. In the specific context you provided, it's likely used to access a specific structured data element related to firmware requests in a storage context.