在未签名的char上的位XOR正在终止程序而没有错误

发布于 2025-01-31 06:06:08 字数 681 浏览 4 评论 0原文

我正在尝试在C ++中创建一个64位的整数,我知道这已经存在于C标题stdint.h中,但我认为这可能是一个有趣的挑战。

无论如何,我正在尝试对三个无符号字符进行位XOR操作,并且该程序在没有警告的情况下停止,它只是停止一秒钟,然后停止:

unsigned char* a = (unsigned char*) 1;
unsigned char* b = (unsigned char*) 2;
unsigned char* c = (unsigned char*) 3;

unsigned char* result = (unsigned char*) malloc(sizeof(unsigned char));

std::cout << "Trying" << std::endl;
*result = *a ^ *b ^ *c;
std::cout << "Done!" << std::endl;

输出为:

PS C:\Users\super\Desktop> ./test.exe
Trying
PS C:\Users\super\Desktop>

我使用Windows 10,如果有帮助,请让我知道您是否需要其他信息,并感谢您的任何帮助,您可以给我:)

I'm trying to create a 64 bit integer as class in C++, I know this already exists in the C header stdint.h but I thought it could be a fun challenge.

Anyway, I am trying to perform a bitwise XOR operation on three unsigned chars, and the program keeps stopping without warning, it just pauses for a split second and then stops:

unsigned char* a = (unsigned char*) 1;
unsigned char* b = (unsigned char*) 2;
unsigned char* c = (unsigned char*) 3;

unsigned char* result = (unsigned char*) malloc(sizeof(unsigned char));

std::cout << "Trying" << std::endl;
*result = *a ^ *b ^ *c;
std::cout << "Done!" << std::endl;

The output being:

PS C:\Users\super\Desktop> ./test.exe
Trying
PS C:\Users\super\Desktop>

I am using Windows 10 if that helps, let me know if you need any other information, and thanks for any help you can give me :)

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

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

发布评论

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

评论(2

榆西 2025-02-07 06:06:08

您正在试图解释无效的指针。您想摆脱很多* s。

unsigned char a = 1;
unsigned char b = 2;
unsigned char c = 3;

unsigned char* result = (unsigned char*) malloc(sizeof(unsigned char));

std::cout << "Trying" << std::endl;
*result = a ^ b ^ c;
std::cout << "Done!" << std::endl;

为此,为什么要在堆上分配单个字节(sizeof(unsigned char)1)?您也可以将另一个unsigned char变量变量。

编辑注意:您还不需要使用std :: endl :: endl

You're trying to dereference invalid pointers. You want to get rid of a lot of those *s.

unsigned char a = 1;
unsigned char b = 2;
unsigned char c = 3;

unsigned char* result = (unsigned char*) malloc(sizeof(unsigned char));

std::cout << "Trying" << std::endl;
*result = a ^ b ^ c;
std::cout << "Done!" << std::endl;

Why are you allocating a single byte (sizeof(unsigned char) is 1 by definition) on the heap for the result? You could just make that another unsigned char variable, too.

Editorial note: you also don't need to use std::endl.

执手闯天涯 2025-02-07 06:06:08

您的代码崩溃了,因为您正在删除指针a, b ,c没有指向可以从可以读取的有效内存地址。

carl的答案向您展示了如何重写此代码以完全不使用指针来,,<代码> b ,c

但是,如果出于某种原因,您实际上确实需要将整数传递为指针的类型(例如,因为您正在通过需要此的C API传递它们),那么您需要 type-cast 指针重新回到整数中,而不是解雇 Pointers,例如:

unsigned char* a = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(1));
unsigned char* b = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(2));
unsigned char* c = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(3));

unsigned char* result = new unsigned char;

std::cout << "Trying" << std::endl;
*result = static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(a))
        ^ static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(b))
        ^ static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(c));
std::cout << "Done!" << std::endl;

...

delete result;

Your code is crashing because you are dereferencing pointers a,b,c which are not pointing at valid memory addresses that can be read from.

Carl's answer shows you how to rewrite this code to not use pointers at all for a,b,c.

But, if for some reason, you actually did need to pass around integers type-casted as pointers (for instance, because you are passing them through a C API that requires this), then you need to type-cast the pointers back into integers, not dereference the pointers, eg:

unsigned char* a = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(1));
unsigned char* b = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(2));
unsigned char* c = reinterpret_cast<unsigned char*>(static_cast<std::uintptr_t>(3));

unsigned char* result = new unsigned char;

std::cout << "Trying" << std::endl;
*result = static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(a))
        ^ static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(b))
        ^ static_cast<unsigned char>(reinterpret_cast<std::uintptr_t>(c));
std::cout << "Done!" << std::endl;

...

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