I have the following code:
// ---- third party library code ---- struct Point { int16_t x, y; }; void third_party_library__getPoint(Point*); void third_party_library__setPoint(const Point*); // ---- my library code ---- void move_point(int dx, int dy) { Point pt; third_party_library__getPoint(&pt); pt.x += dx; pt.y += dy; third_party_library__setPoint(&pt); }
The line pt.x += dx;gives a warningconversion from 'int' to 'int16_t', possible loss of data
pt.x += dx;
conversion from 'int' to 'int16_t', possible loss of data
What should I do?
dx
dy
move_point
assert(dx <= 0x7FFF && dx >= -0x8000 && "too large 'dx' value")
For me there are three related, but different points:
and. Overflow deal
, : . int16_t , +=, , (, , , ).
int16_t
+=
.
(3) - , , . , . , "" , .
(A), , , .
(1) (3), , , . "", "" "".
(2) . , .
(4) Inform, Deal Avoid.
? , - , .. . , - , , / .. , , .
dx dy int16_t, , , , , . , , .
, , ( 15, ), "casting".
, , :
pt.x += int16_t(dx);
, - , , , 16-.
int16_t, int16_t.
4, (, ).
, (, )
: dx dy int16_t, , move_point,
, .
dx dy int16_t.
, ().
BTW pt.x += int16_t(dx) "" ; , pt.x = int16_t(pt.x + dx) ( - )
pt.x += int16_t(dx)
pt.x = int16_t(pt.x + dx)
add assert (dx <= 0x7FFF & dx >= -0x8000 && "too large" dx 'value ") , , .
- , , ( , , , , , ). ( ):
assert(int16_t(dx) == dx);
add assert (dx <= 0x7FFF & dx> = -0x8000 && "dx value is too big") and hope that it will hit when I run the debug version.
Well, obviously, you should handle the “potential” error (I would call it a complete error, by the way).
To accept the warning (knowing that only 16-bit numbers will be passed to your function), you can add a cast that expresses what you are doing, throwing out the 16 high-order bits anyway.
All this said, assuming you have a 32 or 64-bit platform.
Source: https://habr.com/ru/post/1782837/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1782832/how-to-test-if-a-piece-of-code-refers-to-a-macro-in-clojure&usg=ALkJrhjcDUcJ1FWDa2k4tt3NzYABnWiVnAMoving the progress bar do not show percentage in C # wpf - c #Can I use WindowBuilder (the Java visual editor Eclipse) without GWT support? - javaHow to pass a local Ruby variable from a method to a sub-method? - variablesHow to read Facebook's date of birth Facebook API - phpDjango i18n: makemessages only possible at the site level? - djangoWhat is the abbreviation for Caliburn validation - caliburnHow to change text through jQuery? - javascriptSummarizing Related Tables Using Active Record - ruby-on-railsRemoving a cascade from a connection table using the @ManyToMany annotation - annotationsAll Articles