Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数组的所有点设置为0对重新分配动态内存有不同的影响_C++_Arrays - Fatal编程技术网

C++ 将数组的所有点设置为0对重新分配动态内存有不同的影响

C++ 将数组的所有点设置为0对重新分配动态内存有不同的影响,c++,arrays,C++,Arrays,我正在尝试做一个东西,它可以在像素网格上画线,然后在位图上画线。通过使用“new”重新分配内存,我几乎可以得到想要的效果,尽管数组太大,无法删除[],所以最终导致内存泄漏 我的问题是,为了我的生命,我无法找出为什么要这样做: for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++) { previewedPixels[i] = 0; } 这是代码的其余部分,为了便于理解,注释了重新分配内存并导致内存泄漏的行。请原

我正在尝试做一个东西,它可以在像素网格上画线,然后在位图上画线。通过使用“new”重新分配内存,我几乎可以得到想要的效果,尽管数组太大,无法删除[],所以最终导致内存泄漏

我的问题是,为了我的生命,我无法找出为什么要这样做:

for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++)
{
    previewedPixels[i] = 0;
}
这是代码的其余部分,为了便于理解,注释了重新分配内存并导致内存泄漏的行。请原谅我弄得一团糟

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <vector>
#include <iostream>

#define WIDTH 800
#define HEIGHT 600

class element
{
public:
    unsigned char value;
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

const int WIDTHxHEIGHT = WIDTH * HEIGHT;
std::vector< std::vector<short> >XandY(WIDTHxHEIGHT * 2, std::vector<short>(2, 0));                                     //This is the x and y values for the arrays below
sf::Uint8 *mainPixels      = new sf::Uint8[WIDTH * HEIGHT * 4];
sf::Uint8 *previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];    //Previewed pixel co-        ordinates
sf::Uint8 *pixels          = new sf::Uint8[WIDTH * HEIGHT * 4];  //Put the preview and      main pixel grids together on this
element   *currentElement;  //Value of the element currently selected
/*****Declare Elements Here*****/

element metal;

void elementInit()
{
    metal.value    =   1;
    metal.blue     =  90;
    metal.red      =  90;
    metal.green    =  90;
}

inline void wipe()
{
    for (register unsigned int i = 0; i < WIDTH + HEIGHT * 4; i++)
    {
        previewedPixels[i] = 0;
    }
}

inline void draw(short x, short y, unsigned char r, unsigned char g, unsigned char b,     bool preview)
{
    if (preview)
    {
        previewedPixels[(x + WIDTH * y) * 4 + 0] = r;
        previewedPixels[(x + WIDTH * y) * 4 + 1] = g;
        previewedPixels[(x + WIDTH * y) * 4 + 2] = b;
        previewedPixels[(x + WIDTH * y) * 4 + 3] = 255;
    }
    else
    {
        mainPixels[(x + WIDTH * y) * 4 + 0] = r;
        mainPixels[(x + WIDTH * y) * 4 + 1] = g;
        mainPixels[(x + WIDTH * y) * 4 + 2] = b;
        mainPixels[(x + WIDTH * y) * 4 + 3] = 255;
    }
}

void display(void *UserData)
{

}

void lineDraw(short x1, short y1, short x2, short y2, bool preview)
{
    short xDiff = std::abs(x1 - x2);  //Difference between the two 'x' co-ordinates
    short yDiff = std::abs(y1 - y2);   //and the two y co-ordinates
    unsigned char red, green, blue;
    red   = currentElement->blue;
    green = currentElement->green;
    blue  = currentElement->blue;

    if (xDiff > yDiff && x1 < x2)   //Which quadrant it's in. This one is horizontal     going right
    {
        if (preview)        //If it is a preview then then put it onto a different array     of mainPixels
        {
            for (short i = x1; i <= x2; i++)
                draw(i, y1, red, green, blue, true);
        }
        else                //If it's not a preview then just go ahaid on the normal     array
        {
            for (short i = x1; i <= x2; i++)
                draw(i, y1, red, green, blue, false);
        }
    }
    else if (xDiff > yDiff && x1 > x2)  //Horizontal going left
    {
        if (preview)
        {
            for (short i = x1; i >= x2; i--)
                draw(i, y1, red, green, blue, true);
        }
        else
        {
            for (short i = x1; i >= x2; i--)
                draw(i, y1, red, green, blue, false);
        }
    }
    else if (xDiff < yDiff && y1 > y2)  //Going down
    {
        if (preview)
        {
            for (short i = y1; i >= y2; i--)
                draw(x1, i, red, green, blue, true);
        }
        else
        {
            for (short i = y1; i >= y2; i--)
                draw(x1, i, red, green, blue, false);
        }
    }
    else if (xDiff < yDiff && y1 < y2)  //Going Up
    {
        if (preview)
        {
            for (short i = y1; i <= y2; i++)
                draw(x1, i, red, green, blue, true);
        }
        else
        {
            for (short i = y1; i <= y2; i++)
                draw(x1, i, red, green, blue, false);
        }
    }
}


int main()
{
    //Inititialization stuff
    for (unsigned int i = 0; i < WIDTHxHEIGHT * 2; i++)
    {
        XandY[i][0] = i % WIDTH;
        XandY[i][1] = i / WIDTH;
    }
    elementInit();  //Initialize the Elements with their values
    currentElement = &metal;  //By default, select metal
    sf::RenderWindow App(sf::VideoMode(WIDTH, HEIGHT, 32), "ElectroToy");
    sf::Thread graphics(&display, &App);    //Create the thread that will deal with the     graphics
    graphics.Launch();      //Launch Graphics thread
    sf::Image screen(WIDTH, HEIGHT);
    sf::Sprite sprite, prevSprite;  //The first one is the main picture, the other is     the one for the preview mainPixels
    sf::Event Event;
    const sf::Input & Input = App.GetInput();
    short mouseX = Input.GetMouseX();
    short mouseY = Input.GetMouseY();
    short oldMouseX = mouseX;
    short oldMouseY = mouseY;
    bool running = true;
    //End of Initialization stuff
    while (running)                 //main loop
    {
        mouseX = Input.GetMouseX();
        mouseY = Input.GetMouseY();
    bool leftMouseDown = Input.IsMouseButtonDown(sf::Mouse::Left);
    bool rightMouse = Input.IsMouseButtonDown(sf::Mouse::Right);
    bool LShift = Input.IsKeyDown(sf::Key::LShift);
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
                running = false;
            }
        }
        if (leftMouseDown)
        {
            oldMouseX = mouseX;     //Remember the X,Y co-ordinates when the button was     pressed
            oldMouseY = mouseY;
            while (leftMouseDown)
            {
                while (App.GetEvent(Event));    //recieve events so it doesn't get stuck     in an infinite loop
                leftMouseDown = Input.IsMouseButtonDown(sf::Mouse::Left);   //update mouse button status
                mouseX = Input.GetMouseX();
                mouseY = Input.GetMouseY();
                lineDraw(oldMouseX, oldMouseY, mouseX, mouseY, true);

                for (register unsigned int i = 0; i < WIDTHxHEIGHT * 4; i++)  //Combine     the two pixel arrays to create what will go on the screen
                {
                    pixels[i] = mainPixels[i];
                    pixels[i] = previewedPixels[i];
                }
                wipe();
                //previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];
                screen.LoadFromPixels(WIDTH, HEIGHT, pixels);
                sprite.SetImage(screen);
                App.Clear();
                App.Draw(sprite);
                App.Display();
            }
            lineDraw(oldMouseX, oldMouseY, mouseX, mouseY, false);
        }
        oldMouseX = mouseX;
        oldMouseY = mouseY;
        screen.LoadFromPixels(WIDTH, HEIGHT, mainPixels);
        sprite.SetImage(screen);
        App.Clear();
        App.Draw(sprite);
        App.Display();
    }
}
那么

是吗?

是吗?

编译器几十年来一直忽略register关键字。无论如何,我建议memset:

memset(previewedPixels, 0, WIDTH * HEIGHT * 4);
几十年来,编译器一直忽略register关键字。无论如何,我建议memset:

memset(previewedPixels, 0, WIDTH * HEIGHT * 4);

当我第一次发现这一点时,我也感到困惑。显然delete[]不能删除任何大于65536字节的内容。当我第一次发现这个问题时,我也很困惑。显然,delete[]不能删除任何大于65536字节的内容。看不到自己的打字错误是很常见的。只有当你不邀请第二双眼睛去看的时候,你才会觉得自己很愚蠢。看不到自己的打字错误是很常见的。只有当你不邀请第二双眼睛去寻找它们时,你才会觉得自己很愚蠢。
previewedPixels = new sf::Uint8[WIDTH * HEIGHT * 4];
memset(previewedPixels, 0, WIDTH * HEIGHT * 4);