Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

c++ - How to convert an 8-byte integer to a 4-byte integer

I'm trying to convert an 8 byte integer to a 4 byte integer so that I can draw a cube on the screen. The warning it gives me is:

Warning C26451 Arithmetic overflow: Using operator '*' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '*' to avoid overflow (io.2). somthingC:UsersAdminsource epossomthingsomthing enderer.cpp 79

I cant change the 4 byte int to an 8 byte int because it doesn't work with other parts of my code.

here is the code I'm using the error is on line 79:

#include <Windows.h>
struct Render_State
{
int width;
int hight;
void* memory;
BITMAPINFO bitmap_info;

};

Render_State render_state;

void render_backround(HWND hwnd,int colour)
{
if (WM_PAINT)
{
    
    PAINTSTRUCT ps;

    
    HDC hdc = BeginPaint(hwnd, &ps);

    unsigned int* pixel = (unsigned int*)render_state.memory;
    for (int y = 0; y < render_state.hight; y+=1)
    {
        for (int x = 0; x < render_state.width; x+=1)
        {

            *pixel++ = colour;
            
        }
    }
    // render
    StretchDIBits(
    hdc, 0, 0, render_state.width, render_state.hight, 0, 0, render_state.width, render_state.hight,
    render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
    EndPaint(hwnd, &ps);
    }
}
void clear_screen(HWND hwnd, int colour)
{
if (WM_PAINT)
{

    PAINTSTRUCT ps;


    HDC hdc = BeginPaint(hwnd, &ps);

    unsigned int* pixel = (unsigned int*)render_state.memory;
    for (int y = 0; y < render_state.hight; y += 1)
    {
        for (int x = 0; x < render_state.width; x += 1)
        {

            *pixel++ = colour;

        }
    }
    // render
    

StretchDIBits(hdc,0,0,render_state.width,render_state.hight,
0,0,render_state.width,render_state.hight,
        render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
    EndPaint(hwnd, &ps);
  }
}
void draw_rect(HWND hwnd, int X, int Y, int X2 , int Y2, int colour)
{
if (WM_PAINT)
{

    PAINTSTRUCT ps;


    HDC hdc = BeginPaint(hwnd, &ps);

    
    for (int  y = Y; y <Y2; y += 1)
    {
       // line 79 
        unsigned int* pixel = (unsigned int*)render_state.memory + X + y * render_state.width;
            
        for (int x = X; x < X; x += 1)
        {

            *pixel++ = colour;

        }
        }
    // render
             StretchDIBits(hdc, 0, 0, render_state.width, render_state.hight, 0, 0, 
                           render_state.width, render_state.hight,
         render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY); {}
    EndPaint(hwnd, &ps);
    }
}
question from:https://stackoverflow.com/questions/65885992/how-to-convert-an-8-byte-integer-to-a-4-byte-integer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What your referring to by bit is actually a byte. The reason is that when programming in C++, the smallest size of memory you work with is a byte (it may vary). So its not 4 bit or 8 bit, its actually 4 bytes and 8 bytes.

unsigned int* can be represented as a 8 byte integer in a x64 application. In these type of applications, int is usually 4 bytes in size. What the compiler is saying is that "since your later on casting it to a 8 byte value, why not cast it to a 8 byte value first, and then multiply it?". This is to make sure that there wont be any arithmetic overflow (the value is too big for the int type to hold).

To resolve this, all you need to do is to cast y to 8 bytes. And since your anyway casting it to an unsigned 8 byte value, you can either use size_t or to be more precise, use uint64_t (include "inttypes" or "cstdint" beforehand to use it) and I recommend using static_cast to do so.

Example:
This code will give you the same warning:

// x64 build.

int main()
{
    int x = 25;
    size_t y = x * x;    // <-- 
}

To resolve this, we just need to cast x to size_t.

int main()
{
    int x = 25;
    size_t y = static_cast<size_t>(x) * x;    // <-- 
    // Casting one "x" will be enough as the size will automatically resolve to 8 bytes.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...