小编Dav*_*vey的帖子

std::unique_ptr&& 函数参数如何工作?

我正在用 C++ 编写一个简单的游戏引擎。

我接触 C++ 的时间很晚,因为我的大部分空闲时间都花在了 C 上,所以 、unique_ptr以及shared_ptr所有这些所有权魔力对我来说几乎无法理解。

我偶然发现了一件我根本无法理解的事情。让我们看一下以下片段:

#include <memory>
#include "vertexArray.hpp" // here VertexArray class is defined, it's implementation isn't important I think

class Mesh {
public:
    Mesh()= default;
    Mesh(std::unique_ptr<VertexArray>&& vertexArray) {
        // this->vertexArray = vertexArray; <- this doesn't work
        this->vertexArray = std::move(vertexArray); // Why is the move necessary?
    }
private:
    std::unique_ptr<VertexArray> vertexArray;
};

int main() {
    auto vArray = std::make_unique<VertexArray>(/* vertex buffer, index buffer, etc */);
    Mesh mesh = Mesh(std::move(vArray)); // <- …
Run Code Online (Sandbox Code Playgroud)

c++ memory pointers ownership unique-ptr

3
推荐指数
1
解决办法
97
查看次数

X11为什么我不能画任何文字?


我正在努力学习X11.这对我来说很难,因为我没有Linux上的窗口应用程序的经验.
我写了一些简单的代码,我无法解决这个不可见的文本问题.一切都很好,当我试图用DrawRectangle函数绘制矩形时,它正在工作.
这是代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/Xlib.h>
int main()
{
Display* myDisplay;
Window myWindow;
int myScreen;
GC myGC;
XEvent myEvent;
unsigned long black, white;
char* hello = "Hello world!";
XFontStruct* myFont;

if((myDisplay = XOpenDisplay(NULL)) == NULL)
{
    puts("Error in conneting to X Server!");
    return -1;
}
myScreen = DefaultScreen(myDisplay);
black = BlackPixel(myDisplay, myScreen);
white = WhitePixel(myDisplay, myScreen);

myWindow = XCreateSimpleWindow(myDisplay, RootWindow(myDisplay, myScreen), 0, 0, 640, 320, 5, black, white);

XSelectInput(myDisplay, myWindow, ExposureMask);

XClearWindow(myDisplay, myWindow);
XMapWindow(myDisplay, myWindow); …
Run Code Online (Sandbox Code Playgroud)

c linux x11 text

2
推荐指数
1
解决办法
547
查看次数

C"双重免费或损坏"错误

这是产生double free or corruption错误的短代码.

SDL_Surface *surface;
SDL_Surface *surface2;
surface = NULL;
surface2 = SDL_LoadBMP("someImg.bmp");
surface = surface2;
SDL_FreeSurface(surface);
SDL_FreeSurface(surface2);
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么我先解放前不能释放第二面.

c memory free allocation corruption

1
推荐指数
1
解决办法
913
查看次数

标签 统计

c ×2

memory ×2

allocation ×1

c++ ×1

corruption ×1

free ×1

linux ×1

ownership ×1

pointers ×1

text ×1

unique-ptr ×1

x11 ×1