我写了一个程序来演示Go中的浮点错误:
func main() {
a := float64(0.2)
a += 0.1
a -= 0.3
var i int
for i = 0; a < 1.0; i++ {
a += a
}
fmt.Printf("After %d iterations, a = %e\n", i, a)
}
Run Code Online (Sandbox Code Playgroud)
它打印:
After 54 iterations, a = 1.000000e+00
Run Code Online (Sandbox Code Playgroud)
这匹配用C编写的相同程序的行为(使用double类型)
但是,如果float32使用if ,程序会陷入无限循环!如果您修改C程序以使用float而不是a double,则打印
After 27 iterations, a = 1.600000e+00
Run Code Online (Sandbox Code Playgroud)
为什么Go程序在使用时没有与C程序相同的输出float32?
我正在尝试编写一个python程序来测试用C编写的服务器.python程序使用subprocess模块启动已编译的服务器:
pid = subprocess.Popen(args.server_file_path).pid
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是如果python程序由于错误而意外终止,则生成的进程将保持运行状态.我需要一种方法来确保如果python程序意外退出,服务器进程也会被终止.
更多细节:
Linux 具有通过在内核和用户之间映射共享缓冲区来有效捕获网络数据包的功能.我试图以不需要root访问权限的方式使用此接口(因为我没有它).
通常,packet_mmap用于直接查看网络上的所有数据包,这需要root访问权限.我的应用程序只需要使用标准的linux UDP套接字接口.我希望纯粹使用packet_mmap来提高效率 - 现在系统调用占用了超过50%的CPU周期.
有没有办法配置packet_mmap,以便可以从用户空间使用?
我正在尝试将我从Mac OS X编写的一些代码移植到Linux上,并且很难找到适合OSX的替代品OSAtomic.h.我找到了gcc __sync*系列,但我不确定它是否与我的旧编译器/内核兼容.我需要在GCC v4.1.2和内核2.6.18上运行代码.
我需要的具体操作是:
有点奇怪的是,locate stdatomic.h在linux机器上运行会找到头文件(在c ++目录中),而在我的OSX机器上运行相同的命令(gcc v4.6.3)则不会返回任何内容.我需要安装什么来获取stdatomic库,它是否适用于gcc v 4.1.2?
作为旁注,我不能使用任何第三方库.
我正在调整一些服务器代码以获得最佳性能,但我得到了一些奇怪的结果.服务器似乎在虚拟机中比在本机运行时运行得更快.两种设置都使用相同的硬件.
服务器是为Linux编写的,测试客户端是100%标准POSIX.两个应用程序都用C语言编
设置1 :(约300k请求/秒)
设置2 :( ~100k请求/秒)
测试人员通过产生256个线程来工作,这些线程都独立连接到服务器并发送命令.服务器处理流水线中的请求,仅生成与可用核心数相等的多个线程.
以下是我迄今为止提出的可能性:
我需要弄清楚导致速度减慢的原因,以及我可以采取哪些措施来解决问题.
编辑其他信息:
我想在linux上编译一些我知道在OSX上编译的代码,但是我遇到了一些问题.
所有文件都有名为.h的头文件,所有文件都在同一目录下.我这样编译:
gcc *.c -std=c99 -lpthread
Run Code Online (Sandbox Code Playgroud)
虽然这段代码在OSX上编译,但我在Ubuntu安装上遇到了一堆奇怪的链接器错误.我错过了一些编译器选项吗?它是一个默认的Ubuntu服务器安装,带有附加软件包gcc并build-essential已安装.
In file included from errorLogger.h:24:0,
from configParser.h:17,
from configParser.c:9:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
configParser.c: In function ‘parseConfigFile’:
configParser.c:114:5: warning: implicit declaration of function ‘getline’ [-Wimplicit-function-declaration]
In file included from errorLogger.h:24:0,
from global.h:18,
from connection.h:19,
from connection.c:10:
signalHandling.h:24:18: error: unknown type name ‘sigset_t’
connection.c: In function ‘createConnectionQueue’:
connection.c:189:28: warning: assignment makes integer from pointer without a cast [enabled by default]
In file included from errorLogger.h:24:0,
from database.h:16,
from …Run Code Online (Sandbox Code Playgroud) 我想增加我的C程序可用的最大文件描述符数,该程序在OSX 10.7上运行.我已将以下代码添加到我的项目中,但它失败了!
struct rlimit limit;
if(getrlimit(RLIMIT_NOFILE, &limit))
{
perror("Failed to get limit");
return -1;
}
printf("%llu, %llu\n", limit.rlim_cur, limit.rlim_max);
limit.rlim_cur *= 4;
printf("%llu, %llu\n", limit.rlim_cur, limit.rlim_max);
if(setrlimit(RLIMIT_NOFILE, &limit))
{
perror("Failed to set limit");
return -1;
}
Run Code Online (Sandbox Code Playgroud)
它打印日志:
4864, 9223372036854775807
19456, 9223372036854775807
Failed to set limit: Invalid argument
Run Code Online (Sandbox Code Playgroud)
最大限制似乎有点太高了.这是怎么回事?
我正在尝试创建一个C宏,给定一个类型名称将附加_info到它,获取该地址并使用它调用一个函数.示例代码(不起作用):
#define new(X) __new(&(X_info))
struct classInfo
{
size_t size;
void* (*constructor)(void* p);
};
void* __new(struct classInfo* info, ...)
{
return info->constructor(malloc(info->size));
}
void* queue_constructor(void* p)
{
return p;
}
typedef struct
{
uint64_t data;
} queue_t;
const struct classInfo queue_t_info = { .size = sizeof(queue_t),
.constructor = &queue_constructor};
int main(int argc, char** argv)
{
queue_t* p = new(queue_t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预处理器似乎不想扩展X,因为它是关于未定义符号的错误X_info.不知道我应该在宏上修改什么来解决这个问题.
我正在尝试编写一个宏来协助C语言中的面向对象编程.当我将类信息存储在一个常量结构中时,我需要创建一个执行以下操作的宏:
_info以获取所需classInfo结构的名称destroyObject使用指向类struct和对象本身的指针调用该函数一个例子:
queue_t* p = NULL;
delete(p);
Run Code Online (Sandbox Code Playgroud)
delete 应该扩展到:
destroyObject(&(queue_t_info), p);
Run Code Online (Sandbox Code Playgroud)
我尝试使用这个宏,但我无法开始工作:
#define delete(X) (destroyObject(&(typeof(*X)##_info), X))
Run Code Online (Sandbox Code Playgroud)
我的部件类型无法正常工作.
我有一个包含偶数个整数的数组.该数组表示标识符和计数的配对.元组已经按标识符排序.我想将这些数组中的一些合并在一起.我想到了几种方法,但它们相当复杂,我觉得用python可能有一种简单的方法.
IE:
[<id>, <count>, <id>, <count>]
Run Code Online (Sandbox Code Playgroud)
输入:
[14, 1, 16, 4, 153, 21]
[14, 2, 16, 3, 18, 9]
Run Code Online (Sandbox Code Playgroud)
输出:
[14, 3, 16, 7, 18, 9, 153, 21]
Run Code Online (Sandbox Code Playgroud) 沿着makefile运行,我得到了这个:
cc client.o MurmurHash3.o libstorage.a -Wall -lreadline -pthread -o client
MurmurHash3.o: In function `MurmurHash3_x64_128':
/home/evantandersen/mount/src/MurmurHash3.c:59: undefined reference to `rotl64'
/home/evantandersen/mount/src/MurmurHash3.c:106: undefined reference to `fmix'
Run Code Online (Sandbox Code Playgroud)
并且,MurmurHash3.c:
inline uint64_t rotl64 ( uint64_t x, int8_t r )
{
return (x << r) | (x >> (64 - r));
}
//-----------------------------------------------------------------------------
// Finalization mix - force all bits of a hash block to avalanche
inline uint64_t fmix ( uint64_t k )
{
k ^= k >> 33;
k *= 0xff51afd7ed558ccd;
k ^= …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习scanf格式的字符串,而我无法让它工作.我正在尝试读取格式的字符串:
"someKey"= "someValue中"
这是我正在使用的代码:
void test()
{
char buffer[2][128];
int amountRead;
char* input = "\"test\"=\"hello\"";
int result = sscanf(input, "\"%128[a-zA-Z0-9]s\"=\"%128[a-zA-Z0-9]s\"%n", buffer[0], buffer[1], &amountRead);
printf("input = %s\nresult = %d\nstr1 = %s\nstr2 = %s\namountread = %d\n", input, result, buffer[0], buffer[1], amountRead);
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它只设法扫描第一个值:
input = "test"="hello"
result = 1
str1 = test
str2 =
amountread = 0
Run Code Online (Sandbox Code Playgroud) 我在malloc()例程中遇到了分段错误.这是来自gdb的stacktrace:
#0 0x00007ffff787e882 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffff787fec6 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffff7882a45 in malloc () from /lib/x86_64-linux-gnu/libc.so.6
#3 0x0000000000403ab0 in xmalloc (size=1024) at global.c:14
#4 0x00000000004020fb in processConnectionQueue (arguments=0x60a4e0)
at connection.c:117
#5 0x00007ffff7bc4e9a in start_thread ()
from /lib/x86_64-linux-gnu/libpthread.so.0
#6 0x00007ffff78f24bd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#7 0x0000000000000000 in ?? ()
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?什么可能导致malloc()段错误?
编辑:这是来自xmalloc()的代码.它非常标准,正如你可以从stacktrace中看到它调用大小为1024的malloc.
void* xmalloc(size_t size)
{
void* result = malloc(size);
if(!result)
{
if(!size)
{
result = malloc(1);
}
if(!result)
{
fprintf(stderr, "Error allocating memory of …Run Code Online (Sandbox Code Playgroud)