LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI Error: Over Array Bounds

When the attached C-File is compiled and executed in Debug mode CVI fails with an Over Array Bounds error while dereferencing a valid char * pointer. The function 'LogEnetData ()' is passed a pointer to over a thousand bytes of valid data and yet CVI declares an error when the loop index is 2.  I believe that this violates standard C behaviour.
 
When I single-stepped through the code and monitored the value of pData after it had been incremented, I got the following results:
 
          i                              pData
-------------------           -----------------
          0                        0x0012FA6D                        This is OK
          1                       "Over Array Bounds"           
0 Kudos
Message 1 of 2
(2,853 Views)
This is happening because you are passing to the function the address of what is essentially a short integer (the CsciSource field). The user protection tracks this as a pointer to a 2-byte storage, so once you increment it past the first two bytes, it thinks that it's no longer valid memory.

This is only an issue at debug time, and you can get around it in multiple ways, such as disabling run-time checking (globally, in the Build Options dialog, or just around certain pieces of code, using pragmas). You can also force CVI to not carry size info for this pointer by passing it this way, for example:

    LogEnetData (fp, Message.MsgSize, (unsigned char *)&Message + sizeof (Message.MsgSize));

Now having said this, keep in mind that what this code is doing is not very robust. It is not very wise to treat a structure as a chunk of flat memory, since there is usually padding involved in between certain fields (depending on the field sizes) and this can cause your memory offsets to be thrown off when you access it via the field, and then as flat memory.

Luis
NI

0 Kudos
Message 2 of 2
(2,846 Views)