LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI 2013 SP1 - repeated function calls with pointer parameter on an array-variable causes a index shift

Solved!
Go to solution

I have some functions will pointers as parameter and with CVI 2012 SP1 they work as before without problems but with CVI 2013 SP1 they are now erroneous.

 

Here the description what happens - I found a remedy but one have to adopt the old code and I think it is clear that no one "catches" all lines in a "big old code" which are (maybe) affected:

 

I have functions 

     "function_XYZ(int *p_paraArr)"

with "p_paraArr" as pointers on an (int) array.

 

Assume that I have a other function

     "fct_TOP(void)"

in which is an local array variable which is inizialized by

     "int TheArray[25] = {0};"

and inside this "TOP"-function-body I make a function call

    "function_XYZ(TheArray)".

 

There are no complains of the compiler (CVI 2012 or 2013) and the code works (but on CVI 2013 only one time!).

 

But if I put "fct_TOP" in a loop I get a shift in the "TheArray"-memory.  (The loop encloses the "TOP"-function!)

This means the "TheArray"-result got from "function_XYZ(TheArray)" starts at index "1" not on Index "0" - as the first time the function "function_XYZ(TheArray)" was executed.

 

Solution is:

I only replaced

    "function_XYZ(TheArray)"    (<1>)

by

    "function_XYZ( &(TheArray[0]) )"     (<2>)

the whole program works now everytime (in the whole loop) - not only the first time (in the loop).

 

In the second version (<2>) everything is needed for "well working":

The "&" and the brackets "(...)" which encloses the element which sould be pointed to by the "&".

And hopefully you believe me: I tested it a few times it was only that "little" change which solved the problem.

 

It seems so that the CVI 2013 (SP1) makes some sort of internal index-shifting by a repeated execution of the

    "function_XYZ(TheArray)"

but I don't know why or how but I can see in the debug-mode by observing the expected vs. the received array-values !

At first time the internal index (implicit) of "TheArray" is "0", but the following times (during executing the loop) the internal index changes to "1" (seen in the debugger because all expected values were shiftet that way!).

 

So there is an explicit index to the array ("function_XYZ( &(TheArray[0]) )") needed to make it not only the first time of execution of that code clear.

 

There are some good improvements in the CVI 2013 (SP1) and I like that environment more than the 2012-version - but:

       There are some other "changes" too, in the compiler (or linker...?) which are more rigid than "in former times".

The problem by that sort of error is always the "old code"!

You wouldn't expect such a behaviour.

The compiler/linker doesn't complain (a complaint would be good!) the writing but it makes this error (in loops).

 

By the way: My "Build Options" are set on "Extended" (without changes in the "..."-Button-Options) and all check-boxes, except the "OpenMP_support"-check-box are checked! So I think I set the compiler very rigid - maybe there are some "..."-Button-settings to get that rid of that problem , but I didn't found them/it.

 

My request:

   - Please make the compiler more rigid by needing an explicite index

   - or switch to the "old" behaviour with "function_XYZ(TheArray)" points implicite always to the index "0" of the "TheArray"-element.

 

0 Kudos
Message 1 of 6
(4,476 Views)

hi,

    can you please post an example code?

    For me this woks as expected (CVI 2013.0.0(647)):

   

void function_XYZ(int *p_paraArr)
{
	p_paraArr[1]++;
	return;
}
void fct_TOP(void)
{
	int TheArray[4] = {0};
	
	function_XYZ(TheArray);
	DebugPrintf("%i,%i,%i,%i\n",TheArray[0],TheArray[1],TheArray[2],TheArray[3]);
	return;
}

void Test() { DebugPrintf("fct_TOP()\n"); for (int i=0;i<10;i++) { fct_TOP(); } return; }

 fct_TOP()
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0
0,1,0,0

0 Kudos
Message 2 of 6
(4,447 Views)

First:

The content of the index element "0" is by this error always "0" and the expected values are starting at index element "1".

Thus the "shift" happens according to the index value according to the index itself.

In my case the index element "15" was now in "16" and because that value was not the size I expect in the program I faced that problem which revealed as an error.

 

Second:

I can't reproduce today that problem, because I changed my program yesterday in all cases I make a function-call with a pointer parameter.

 

Maybe it's my own fault and buggy programming ... maybe ... but:

      Yesterday I was able to have reproducible a false value comparison by using the call "function_XYZ(TheArray)"

      and to get the correct value comparison by using the call "function_XYZ( &(TheArray[0]) ).

      ...so, maybe it's my program- or programming-style or something like that, but I don't think so (I checked it more than 5 times).

 

Third:

Sorry, but I do not give a code-sample.(I am bad I know but it would need time to do that...)

My program is more complex than the sample test code above.

This is no criticism - realy not! (Thanks for your post, but I have to disappoint you - sorry!)

 

In my program I gain the data in the array from a COM-Port readout. This COM-port action is started in the "function_XYZ" and inside this function there are also other function calls ... so sorry ... no sample code ... I would have to get my backup from 2 days ago and make the program, reduce it to a shorter form check the behaviour ... and so on ... sorry ... no sample code!

 

Maybe one other thing will give you a hint that it's maybe not my buggy code writing (solitary 😉 😞

Some days ago I made a reply on an older forum problem:

http://forums.ni.com/t5/LabWindows-CVI/FATAL-RUN-TIME-ERROR-Dereference-of-out-of-bounds-pointer/td-...

 

Because the carriage returns were eliminated by my former editor-copy I paste in here the "well formated" version of my contribution of that problem

"FATAL RUN-TIME ERROR: Dereference of out-of-bounds pointer...":

[Start of copy-pase-section]

 

I had a relatively similar error.
In one of our programs there is used a structure-type with variables and other structure-types inside, with variables and array-variables inside the inner structures, and arrays of structures of the inner structures. (How complicated 😐 ! But so the former programmers liked it! 😉 )

Let's have the following:
A instance of th "main" structure-type (now called "abc")
and a array of the inner structure (now called "fgh[3]")
and a array-variable "arr[4]" inside the inner structure.
Let this "abc" be a pointer parameter of a function:
    "FunctionXYZ(TheStructureTypeOfABC *abc)".

Inside of "FunctionXYZ" we accessed "arr[i]" from LabWindows 5.5 till 2012 by the following code:
    abc->fgh->arr[i]
The implicit meaning of no index at "fgh" was that the compiler used the Index "0"-element.

Now the 2013 compiler complains
    "Dereference of out-of-bounds pointer: 1 bytes (1 elements) past end of array."

I changed the code the following:
- A new temporary array "tmpArr[4]" was created.
- I put the 4 values into "tmpArr[i]" (i=0,...3).
- Finaly I made "memcpy(abc->fgh->arr, tmpArr, 4);"

I think it would get the same result by putting all the 4 Values into "abc->fgh[0]->arr[i]". But I didn't checked that. Maybe that hint will help others to find the error not in the compiler-marked index-variable (here "arr") but in the (seamingly not marked) 'precursor'-variable(s) (here "fgh").

 

 [End of copy-pase-section]

 

So hopefully you are not to angry that I do not give a code sample.

 

My final message is:

Don't do  "implicit pointering"  (I know this word doesn't exist...)  with array variables or arrays of structures - do  "explicite pointering" !!

  (In LabWindows/CVI 2013 - but you can still do it in LabWindows/CVI 2012 or earlier!)

 

...I hope I will start my weekend in a short while...

 

0 Kudos
Message 3 of 6
(4,422 Views)

Two simple questions:

 

1) when CVI 2013 SP1 went out ? Smiley Surprised

 

2) are you absolutely sure that your pointers are pointing to correctly allocated memory, and are not pointing to deallocated variables (i.e. automatic variables of terminated functions or similar situations) ?

May be your old code worked by chance, because the memory content wasn't altered by other processes/functions.

With the new compiler, variables are allocated in different places, they get corrupted and you read apparently shifted contents.

 

Anyway, if you don't post code sample, nobody will be able to help you.

 

 

 

 

 

 

Carlo A.
Megaris




0 Kudos
Message 4 of 6
(4,400 Views)
Solution
Accepted by topic author FrankieBoy98

Thank you for your posts, comments and suggestions.

Maybe - as I wrote before - it's the programming style or some "self made error" ... maybe ...
      ...but if I replace "function_XYZ(TheArray)" by "function_XYZ( &(TheArray[0]) )" and
         then it works ... why then, and not before, if bothe are the same? ...

 

But as long as I do not post a code sample nobody will accept it - I accept that. So regard this post more as a hint that from LW/CVI 2012 to LW/CVI 2013 more changed than only the LW-GUI or some features: The compiler changed it's "way to do" ... or so.

For that problem I think I will use the solution "use explicit pointers and not implicit".
Which should be a good idea by considering
    http://forums.ni.com/t5/LabWindows-CVI/FATAL-RUN-TIME-ERROR-Dereference-of-out-of-bounds-pointer/td-...

which is mybe also caused only by bad code ... who knows ... but for me it's enough reason to act as I suggest above.

Best Greetings,
F.

0 Kudos
Message 5 of 6
(4,393 Views)

Frankly speaking, it seems you're trying to fix a buggy code without trying to understand how it works.

 

You should wonder why it sometime works, not why it sometime fails...

 

Best regards

 

 

 

 

 

Carlo A.
Megaris




0 Kudos
Message 6 of 6
(4,387 Views)