netbeans C++ 中的信号是什么?调试

发布于 2024-12-02 21:59:55 字数 1462 浏览 1 评论 0原文

大家好,我正在尝试使用 netbeans 在 C++ 中调试此应用程序(运行它使我在关闭它后运行失败),此时它给我一个信号错误

*c = (char)any number;

其中任何数字是 1-7 之间的整数,

它告诉我信号捕获了 SIGSEGV 吗?信号错误?

这是什么

代码将一些内容写入二进制文件,就像这样

void clean_up(Dot &myDot, Uint32 &bg) {
    SDL_FreeSurface(DotS);

    ofstream f(SAVE_FILE_PATH, ios::binary | ios::out);

    f.clear();

//    char *buffer;

//    buffer[0] = *(char*)(&myDot.get_location().x + 0);
//    buffer[1] = *(char*)(&myDot.get_location().x + 1);
//    buffer[2] = *(char*)(&myDot.get_location().x + 2);
//    buffer[3] = *(char*)(&myDot.get_location().x + 3);
//    buffer[4] = *(char*)(&myDot.get_location().y + 0);
//    buffer[5] = *(char*)(&myDot.get_location().y + 1);
//    buffer[6] = *(char*)(&myDot.get_location().y + 2);
//    buffer[7] = *(char*)(&myDot.get_location().y + 3);

    f.write((char*)&myDot.get_location().x, sizeof(myDot.get_location().x));

    f.write((char*)&myDot.get_location().y, sizeof(myDot.get_location().y));

    char *c;

    if (bg == C0)
        *c = (char)1;
    else if (bg == C1)
        *c = (char)2;
    else if (bg == C2)
        *c = (char)3;
    else if (bg == C3)
        *c = (char)4;
    else if (bg == C4)
        *c = (char)5;
    else if (bg == C5)
        *c = (char)6;
    else if (bg == C6)
        *c = (char)7;

    f.write(c, 1);

    f.close();

    SDL_Quit();
}

,请有人告诉我为什么注释部分也给了我一个信号

Hi guys i'm trying to debug this application in C++ using netbeans (running it gives me run failed after closing it) it gives me a signal error at this point

*c = (char)any number;

where any number is an integer from 1-7

it tells me signal caught SIGSEGV ? with signal error ?

what is it

the code writes some stuff to a binary file it goes like this

void clean_up(Dot &myDot, Uint32 &bg) {
    SDL_FreeSurface(DotS);

    ofstream f(SAVE_FILE_PATH, ios::binary | ios::out);

    f.clear();

//    char *buffer;

//    buffer[0] = *(char*)(&myDot.get_location().x + 0);
//    buffer[1] = *(char*)(&myDot.get_location().x + 1);
//    buffer[2] = *(char*)(&myDot.get_location().x + 2);
//    buffer[3] = *(char*)(&myDot.get_location().x + 3);
//    buffer[4] = *(char*)(&myDot.get_location().y + 0);
//    buffer[5] = *(char*)(&myDot.get_location().y + 1);
//    buffer[6] = *(char*)(&myDot.get_location().y + 2);
//    buffer[7] = *(char*)(&myDot.get_location().y + 3);

    f.write((char*)&myDot.get_location().x, sizeof(myDot.get_location().x));

    f.write((char*)&myDot.get_location().y, sizeof(myDot.get_location().y));

    char *c;

    if (bg == C0)
        *c = (char)1;
    else if (bg == C1)
        *c = (char)2;
    else if (bg == C2)
        *c = (char)3;
    else if (bg == C3)
        *c = (char)4;
    else if (bg == C4)
        *c = (char)5;
    else if (bg == C5)
        *c = (char)6;
    else if (bg == C6)
        *c = (char)7;

    f.write(c, 1);

    f.close();

    SDL_Quit();
}

and please can someone tell me why the commented part gives me a signal too

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-12-09 21:59:55

你还没有给c分配内存。它是一个指向某个随机位置的指针(或 NULL,取决于内存的初始化方式)。然后你尝试写信给它。

它真的需要是一个指针吗?您可以只使用 char c;,然后使用 c = (char)1; 等。然后您需要将 fwrite 调用更改为 f.write (&c, 1); (或使用 fputc)。

编辑:代码中注释掉的部分看起来有相同的潜在问题。您尚未为缓冲区分配任何内存。如果示例占了大部分,那么您只有 8 个项目,然后将其声明为 char buffer[8] 而不是指针。

You haven't allocated memory to c. It's a pointer to some random location (or NULL, depending on how memory is being initialized). You then try to write to it.

Does it really need to be a pointer? Could you just use char c;, and then c = (char)1; etc. You'd then want to change your fwrite call to f.write(&c, 1); (or use fputc).

EDIT: The commented out part of your code looks like it has the same underlying problem. You haven't allocated any memory for buffer. If the example is the most of it, you're only ever having 8 items, then declare it as char buffer[8] instead of as a pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文