在C中,是否可以在sprintf函数中使用递归?出于某种原因,当我这样做时,我遇到了分段错误:
inline char *TreeNode_toString(const TreeNode *node)
{
char *out;
if(TreeNode_isExternal(node)) // If the node has no children...
{
sprintf(out, "%s:%.2f", node->name, node->distance);
}
else // The node is strictly binary, so it will have two non-null children
{
char *l = TreeNode_toString(node->l); // l = left child
char *r = TreeNode_toString(node->r); // r = right child
sprintf(out, "(%s,%s):%.2f", l, r, node->distance);
}
return out;
}
Run Code Online (Sandbox Code Playgroud) 我在C中听说过很多关于性能的事情; 与正常分配相比,转换速度慢,函数调用很慢,二进制操作比正常操作快得多,等等......
我确信其中一些内容是特定于架构的,编译器优化可能会产生巨大的差异,但我希望看到一个图表来大致了解我应该做什么以及我应该避免编写高性能程式.有这样的图表(或网站,书籍,任何东西)?
我有几个用C语言编写的文件,我希望它们与C++兼容,所以对于我使用的C头文件;
#ifdef __cplusplus
extern "C" {
#endif
Run Code Online (Sandbox Code Playgroud)
在文件的开头,当然
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
...在末尾.但它似乎会产生'inline'关键字的问题.我的解决方案是简单地删除C++的inline关键字,但我想它可能会对C++程序产生不良影响(这些函数被称为gazillions of times).
有更好的解决方案吗?
我有一个libxml2的简单示例,但它返回以下错误:
$ gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c
/tmp/cc6OKSKJ.o: In function `main':
ex1.c:(.text+0x60): undefined reference to `xmlReadFile'
ex1.c:(.text+0x70): undefined reference to `xmlDocGetRootElement'
collect2: ld returned 1 exit status
$ xml2-config --libs
-lxml2
$ xml2-config --cflags
-I/usr/include/libxml2
Run Code Online (Sandbox Code Playgroud)
我在Lubuntu 11.10 x86_64上,我有我需要的所有软件包(我认为):libxml2,libxml2-dev,libxml2-dbg ...这是示例的代码:
// gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
int main(int argc, char **argv)
{
xmlDoc *document;
xmlNode *root, *first_child, *node;
char *filename;
if (argc < 2)
{
fprintf(stderr, "Usage: %s filename.xml\n", argv[0]);
return …Run Code Online (Sandbox Code Playgroud) 我有一个JavaScript(节点)功能来获取网页内容并通过回调进行处理:
'using strict';
var http = require('http');
function download(url, callback) {
http.get(url, function(res) {
var content = '';
res.on('data', function (chunk) {
content += chunk;
});
res.on('end', function() {
callback(content);
});
}).on('error', function() {
callback(null);
});
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么我不能简单地在'end'上返回结果。显然,当发出“ end”事件时,“ content”变量包含一个包含网页内容的字符串,否则无法将其提交给回调函数,所以为什么我不能像这样返回它:
function download2(url) {
http.get(url, function(res) {
var content = '';
res.on('data', function(chunk) {
content += chunk;
});
res.on('end', function() {
return content;
});
}).on('error', function() {
return null;
});
}
Run Code Online (Sandbox Code Playgroud)
download2始终返回未定义。为什么?
我知道如何将整数添加到字符串中,但我不确定我是否在有效的事情中这样做.我有一个类,我经常要返回一个字符串加一个整数(每次不同的整数),在Java中我会做类似的事情
public class MyClass {
final static String S = "MYSTRING";
private int id = 0;
public String getString() {
return S + (id++);
}
}
Run Code Online (Sandbox Code Playgroud)
但是在C++中我必须这样做;
class MyClass {
private:
std::string S; // For some reason I can't do const std::string S = "MYSTRING";
int id;
public:
MyClass() {
S = "MYSTRING";
id = 0;
}
std::string getString() {
std::ostringstream oss;
oss << S << id++;
return oss.str();
}
}
Run Code Online (Sandbox Code Playgroud)
一个额外的约束:我不想(实际上,不能)使用Boost或任何其他库,我将不得不使用标准库.
所以事情就是这样; 代码工作,但在C++中我必须创建一堆ostringstream对象,所以它似乎效率低下.公平地说,也许Java也是这样做的,我只是没注意到它,我说它效率低,主要是因为我对字符串知之甚少.
有没有更有效的方法来做到这一点?
一个简单的问题,但我在书中找不到答案。我想读取一个二进制文件来为随机数生成器提供种子,但我不想每次调用该函数时都使用相同的种子为我的生成器提供种子,因此我需要为我在文件中的位置保留一个变量(不是问题),我需要知道如何从文件中的特定点开始读取文件(不知道如何)。代码:
void rng_init(RNG* rng) {
// ...
FILE *input = fopen("random.bin", "rb");
unsigned int seed[32];
fread(seed, sizeof(unsigned int), 32, input);
// seed 'rng'...
fclose(input);
}
Run Code Online (Sandbox Code Playgroud)