开发者

Warning -- Suspicious Truncation in arithmetic expression combining with pointer

开发者 https://www.devze.com 2023-04-12 02:37 出处:网络
I am doing static code analysis (Using Gimpel PC- Lint) of my source code. In code analysis, i am getting a warning \'Suspicious Truncation in arithmetic expression combining with pointer\'.

I am doing static code analysis (Using Gimpel PC- Lint) of my source code. In code analysis, i am getting a warning 'Suspicious Truncation in arithmetic expression combining with pointer'.

Here is the what analysis report says::

source\interp.cpp(142): error 679: (Warning -- Suspicious Truncation in arithmetic expression combining with pointer)

    py[ulIndex] = y[ulIndex+1] - y[ulIndex];

Here py, y are dynamic array of data type double with same size but still the warning is coming up in code analysis for the above line.

Can anyone help me to figu开发者_如何学Cre out this?

Thanks in advance.


It may be because your adding an int to an unsigned long with ulIndex+1 Try

py[ulIndex] = y[ulIndex+UL1]-y[ulIndex];

Or it could depend on how you've defined py and y arrays.


It could be because you are using an unsigned expression as an array index (or in the resulting pointer arithmetic), which IIRC should be a signed expression for most C implementations.

The C standards do not define whether the index expression should be signed or unsigned, but most (all?) current C implementations used signed indexes.

EDIT:

There is a message reference for your checker here. I think that the checker warns on the implicit cast from unsigned int to int, which may incur information loss. Try adding an explicit cast to int and see if the message changes/goes away.


The correct type for an array index is size_t. You're getting a warning because size_t(ulIndex+1) may be zero, instead of size_t(ulIndex)+1

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号