首先,我知道有类似的问题.但是,我希望有一个更普遍的简单问题,真正原始的C数据类型.所以这就是.
在 main.c我调用一个函数来填充这些字符串:
int
main (int argc, char *argv[]){
char *host = NULL ;
char *database ;
char *collection_name;
char *filename = "";
char *fields = NULL;
char *query = NULL;
...
get_options(argc, argv, &host, &database, &collection_name, &filename,
&fields, &query, &aggregation);
Run Code Online (Sandbox Code Playgroud)
内部get_options:
if (*filename == NULL ) {
*filename = (char*)realloc(*filename, strlen(*collection_name)*sizeof(char)+4);
strcpy(*filename, *collection_name);
strcat(*filename, ".tde"); # line 69
}
Run Code Online (Sandbox Code Playgroud)
我的程序工作正常,但Valgrind告诉我,我做错了:
==8608== Memcheck, a memory error detector
==8608== Copyright (C) 2002-2011, and GNU GPL'd, by …Run Code Online (Sandbox Code Playgroud) 我有一些返回的C++代码std::function.我想从一些C代码中调用它.这可能吗?作为一个例子,我有以下代码:
typedef std::function<int(int)> AdderFunction;
AdderFunction makeAdder(int amount) {
return [amount] (int n) {
return n + amount;
};
}
extern "C" {
AdderFunction makeCAdder(int amount) {
return makeAdder(amount);
}
}
Run Code Online (Sandbox Code Playgroud)
与clang++ -std=c++11 test.cpp它导致以下警告:
'makeCAdder' has C-linkage specified, but returns user-defined type 'AdderFunction' (aka 'function<int (int)>') which is incompatible with C
Run Code Online (Sandbox Code Playgroud)
我明白为什么会这样,但想知道是否有一种模式可以实现这一目标?
我有相当于以下代码:
const int* const n = new int;
printf("input: ");
scanf("%d", n);
delete n;
Run Code Online (Sandbox Code Playgroud)
现在,因为n是指向CONSTANT整数的指针,所以这不应该工作(我期待编译器错误).但是,这似乎可以正常工作,甚至可以将输入值存储到*n中.
我想知道,为什么这不会给我一个错误; 它为什么有效?扫描不能改变*n的值吗?
我有一个问题要重写一个网址.我要这个 :
http://www.foo.com/test.php?u=s1&id=12345&img=12
Run Code Online (Sandbox Code Playgroud)
至
http://app.foo.com/12345-s1-12.test
Run Code Online (Sandbox Code Playgroud)
第一个参数u是一个字符串,参数id和img是整数.
我开始用这样的东西:
RewriteCond %{REQUEST_URI} ^/test.php?u=(.*)&id=(.*)&img=(.*)/ [NC]
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L]
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
编辑:
仍然不起作用,但我很接近!
RewriteCond %{REQUEST_URI} ^/test.php [NC]
RewriteCond %{QUERY_STRING} ^u=(.*)&id=(.*)&img=(.*)
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L]
Run Code Online (Sandbox Code Playgroud)
现在它给了我链接:
http://app.foo.com/12345-s1-12.test?u=s1&id=12345&img=12
Run Code Online (Sandbox Code Playgroud)
代替 :
http://app.foo.com/12345-s1-12.test
Run Code Online (Sandbox Code Playgroud)
:(
我正在学习在Ubuntu 12.10上第一次使用libusb v1.0.0.这是我用来尝试理解如何使用此API的一些小测试代码:
#include <libusb-1.0/libusb.h>
...
libusb_device **list;
libusb_get_device_list(ctx, &list); // Returns 11 USB devices which is correct.
for (size_t idx = 0; list[idx] != NULL; idx ++)
{
libusb_device *dev = list[idx];
libusb_device_descriptor desc = {0};
int rc = libusb_get_device_descriptor(dev, &desc);
Run Code Online (Sandbox Code Playgroud)
此时,rc == 0,意味着它应该已经成功完成.来源:*libusb_get_device_descriptor()*的文档.
但结构desc总是空的.没有任何字段被设置.如果我将上面的最后两行更改为:
libusb_device_descriptor desc = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int rc = libusb_get_device_descriptor(dev, &desc);
Run Code Online (Sandbox Code Playgroud)
...然后当libusb_get_device_descriptor()返回时,我看到desc保持不变,向我确认我没有得到我对此API的期望.
我还尝试以a.outroot 身份运行,以防万一这需要提升权限.进行谷歌搜索libusb_get_device_descriptor并没有让我到任何地方.
我跑来试试这段代码的相关命令:
sudo …Run Code Online (Sandbox Code Playgroud) 目前我使用迭代器来搜索向量并测试其元素.我使用了访问元素
std::vector<int>::iterator it;
if (*it == 0);
Run Code Online (Sandbox Code Playgroud)
我可以使用相同的指针算术样式逻辑来测试下一个元素(不改变我的迭代器)吗?
我首先需要看看它是否会将迭代器推出界限
if (it != myvec.end())
Run Code Online (Sandbox Code Playgroud)
然后测试当前元素和下一个元素
if (*it == 1 && *(it + 1) == 1)
Run Code Online (Sandbox Code Playgroud)
这会像我期望的那样使用指针吗?
这是一段简短的代码片段,exit(3)在发生故障时会有两次调用.这些调用是否释放malloc分配的内存?谷歌搜索曾经说过它,甚至更多次,它没有......
我应该添加free()吗?
另外:哪个更好if (!word)(它也适用于例如.word == 0,这与word == NULL不同,所以我猜它是错的)或者if (word == NULL)?
char *word = NULL, *temp = NULL;
word = (char *)malloc(sizeof(char) * size);
if (!word) { /* or maybe rather it should be (word == NULL) */
perror("malloc fail");
if (fclose(fp)) {
perror("fclose fail");
exit(3); /* exit without free ? */
}
exit(3); /* exit without free ? */
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我已经做了一段时间并且遇到了很多麻烦.我想生成一个从-1到1的随机值进行计算.我不能使用%运算符,因为它仅适用于整数.我也试过用,fmod()但我也遇到了困难.
我试图使用的是......
double random_value;
random_value = fmod((double) rand(),2) + (-1);
Run Code Online (Sandbox Code Playgroud)
看起来它似乎不正确.我也试着用时间播种srand,但我认为我在那里做错了,因为它一直在抛出这个错误:
"error: expected declaration specifiers or '...' before time"
Run Code Online (Sandbox Code Playgroud)
码:
srand((unsigned) time(&t));
Run Code Online (Sandbox Code Playgroud)
任何有关这些问题的帮助都会受到赞赏.
我遇到了"消化"valgrind输出的问题.这是一个片段:
==15145== Invalid write of size 8
==15145== at 0x40168E: split_node_at_letter (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x4018E7: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401BD5: main (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== Address 0x52237d8 is 8 bytes before a block of size 16 alloc'd
==15145== at 0x4C29BCF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==15145== by 0x401063: add_to_trie_word_list (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x40173B: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x40183D: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401906: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145== by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii) …Run Code Online (Sandbox Code Playgroud) 我无法找到任何对使用fputc()时创建流的指定行为的引用fopen("/some/path", "r").
我搜索了C11草案n1570 pdf寻找没有运气的任何参考,fopen()功能规范谈到将未知字符作为模式参数传递,这是未定义的行为.但它没有说明创建的流上的后续IO.
这是fwrite()功能规范
7.21.8.2
fwrite功能概要
Run Code Online (Sandbox Code Playgroud)#include <stdio.h> size_t fwrite(const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream);描述
- 的
fwrite函数写入,从阵列所指向的ptr,最多nmemb,其大小由指定的元素size,到流指向stream.对于每个对象,对fputc函数进行大小调用,从unsigned char完全覆盖对象的数组中获取值(按顺序).流的文件位置指示符(如果已定义)按成功写入的字符数提前.如果发生错误,则流的文件位置指示符的结果值是不确定的.返回
- 该
fwrite函数返回成功写入的元素数,这将少于nmemb仅在遇到写入错误时.如果为size或nmemb为零,则fwrite返回零,并且流的状态保持不变.
这需要我们的fputc()功能,所以
7.21.7.3
fputc功能概要
Run Code Online (Sandbox Code Playgroud)#include <stdio.h> int fputc(int c, FILE *stream);描述
- 该
fputc …