我正在做一个os,并引导到一个用c语言制作的64位内核中。我已经创建了一个打印函数,这个函数正在工作,并且我试图创建一个函数来将十六进制值转换成字符串,这样我就可以打印它们了。我的代码导致启动循环,但是当我编译完全相同的代码以在linux中正常运行时,它工作得非常完美。有关守则:
int logarithm(double value, int base, int* output) {
int i = 0;
while (value > 1) {
value /= base;
i++;
}
*output = i;
}
我试图为std::snprintf提供最小的缓冲区,以便将uint64_t转换为std::string,并在转换后的字符串中发现大值的错误。
#include <cstdio>
#include <inttypes.h>
#include <limits>
#include <string>
#include <cstdint>
// assume this is maximum string size to represent any unsigned integer
// with 1 extra byte for string
以下代码在使用(本机64位)g++-10的64位Linux系统上编译警告:
#include <cstdint>
class A {
public:
explicit A(unsigned long long int x) { };
explicit A(uint64_t x) { };
};
int main() {}
但是,在32位系统(相同的Linux发行版,相同版本,但它是32位计算机)上,我得到了以下错误:
t.cc:6:14: error: ‘A::A(uint64_t)’ cannot be overloaded with ‘A::A(long
我收到了以下错误:
error: no matching function for call to ‘ab::ab(uint64_t)’
ab看起来如下:
struct ab {uint32_t a; uint32_t b;} __attribute__((packed));
为什么我不能把uint64_t投给ab?
os: Linux 5.7.9-arch1-1 x86_64
compiler: g++ (GCC) 10.1.0
我正在编写一个Linux内核模块,我需要想出一个接受两个整数作为输入的散列函数。因为代码是在内核空间中运行的,所以我没有可用的标准库。
基本上,我需要一个散列函数,其中:
hash(a, b) = c
hash(b, a) = c
其中a和b的可接受输入是无符号32位整数。哈希函数应返回一个无符号的64位整数。冲突(即,散列(a,b) =c和散列(d,f) =c)也是不希望的,因为这些值将在二进制搜索树中使用。搜索的结果是一个可能结果的链表,然后在实际比较a和b的位置上迭代。因此,一些冲突是可以接受的,但冲突越少,所需的迭代就越少,运行速度也就越快。
性能也非常重要,当我编写防火墙应用程序时,
我有一个二进制的,大端的,包含如下消息的消息:
struct Msg
{
// 4 byte int
// 6 byte int
// 2 byte int
};
我希望读取6字节的整数。
我以前没有遇到过不是1、2、4或8字节的成员。
我不关心可移植性,你可以假设这是在一个使用了GCC编译器的Linux系统上。
要阅读这篇文章,我应该这样做:
struct Msg
{
uint32_t a;
uint16_t b; // Part of 6 byte field
uint32_t c; // Part of 6 byte field
我正试图在Mac上编译一个在Linux下没有任何问题的项目。我在这两个操作系统上都使用GCC。除了我设法修复的其他问题外,在OSX上编译时还会出现以下错误:
error: ‘_SC_PHYS_PAGES’ was not declared in this scope
long pages = sysconf(_SC_PHYS_PAGES);
^
unistd.h包含在发生此错误的文件中。
如何以代码仍然在Linux下编译的方式来解决这个问题?