Tag: gnu

我能得到的最简单的getopt程序?

在关于如何使用getopt() 链接上做了一些阅读之后,我试图得到一个小例子。 我想要的是: ./prog -v # show me prog version ./prog -f filename # just show me the filename I entered from the command line 这是我到目前为止写的: #include #include #include int main(int argc, *argv[]) { char VER[] = “0.1.1”; int opt; opt = getopt(argc, argv, “vf:”); char *filename; while (opt != -1) { switch(opt) { case […]

gnu gdb malloc返回不可访问的指针

运行一些代码后,gdb调试会话中的malloc返回不可访问的地址。 首先在主要function开始时rest。 一切都好。 Breakpoint 9, main (argc=5, argv=0x7fffffffe418) at src/ose/sdv/ose_sdv/linux/main.c:557 557 char *cfgfile = NULL; (gdb) call malloc(4) $50 = 23293968 (gdb) x 23293968 0x1637010: 0x00000000 (gdb) c 在运行一些行后,它开始返回无法访问的内存地址,该地址从0xffffffff~开始 Program received signal SIGINT, Interrupt.0x00007ffff70c1f4d in read () from /lib64/libc.so.6 (gdb) call malloc(4) $52 = -1811110576 (gdb) x -1811110576 0xffffffff940ca550: Cannot access memory at address 0xffffffff940ca550 […]

在Linux中是否存在_set_purecall_handler()的等效项?

我想用自己的方法覆盖纯虚拟调用的标准处理程序( __cxa_pure_virtual() )。 Windows的答案是’_set_purecall_handler()’。 Linux / GNU中是否有类似的工具?

在OS X上使用crypt_r

我想在Mac OS X 10.8.2上使用crypt_r函数 #define _GNU_SOURCE #include 产生 crypt.h: No such file or directory 我在哪里可以获得crypt.h文件? 还是我错了? 编辑问题 – 具体例子 #include #include int main(){ struct crypt_data * data = (struct crypt_data *) malloc(sizeof(struct crypt_data)); char * testhash; testhash = crypt_r(“string”, “sa”, data); free(data); return 0; } 产生 gcc test.c -Wall test.c: In function ‘main’: test.c:5: error: […]

opendir:打开的文件太多了

我写这段代码打印/home/keep with absolution path中的所有文件: #include #include #include #include #include #include #include void catDIR(const char *re_path); int main(int argc, char *argv[]) { char *top_path = “/home/keep”; catDIR(top_path); return 0; } void catDIR(const char *re_path) { DIR *dp; struct stat file_info; struct dirent *entry; if ((dp = opendir(re_path)) == NULL) { perror(“opendir”); return; } while (entry = […]

argp和getopt有什么区别?

我认为标题是自我解释的。 我正在制作一个节目,我想知道我应该使用哪两个以及为什么。

如何在GNU C中使用mcheck进行堆一致性检查?

我试图了解堆一致性检查在GNU C库中的工作原理。 我指的是http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html#Heap-Consistency-Checking 这是我编写的示例程序。 我按照手册中的建议,如果我在gdb中运行并调用mcheck(0)我将调用我的自定义abortfn 。 但它没有被召集。 我在这里想念的是什么? 包括必要的标题。 void *abortfn(enum mcheck_status status) { switch(status) { case MCHECK_DISABLED: printf(“MEMCHECK DISABLED\n”); break; default: printf(“MEMCHECK ENABLED\n”); } } int main() { printf(“This is mcheck testing code\n”); int *a = (int *) malloc(sizeof(int)); *a = 10; printf(“A: %d\n”, *a); free(a); return 0; }

如何编写内联gnu扩展程序集的短块来交换两个整数变量的值?

为了娱乐,我正在使用带有32位Linux目标的x86的AT&T语法学习gnu扩展汇编。 我花了最后三个小时编写了两个可能的解决方案来解决我交换两个整数变量a和b的值的挑战,我的解决方案都没有完全解决我的问题。 首先,让我们更详细地看一下我的TODO障碍: int main() { int a = 2, b = 1; printf(“a is %d, b is %d\n”, a, b); // TODO: swap a and b using extended assembly, and do not modify the program in any other way printf(“a is %d, b is %d\n”, a, b); } 在阅读本HOWTO之后 ,我编写了以下内联扩展汇编代码。 这是我第一次尝试交换整数: asm volatile(“movl %0, […]

init-declarator-list和GNU GCC属性语法

我正在改进一个内部C语言野牛/基于flex的解析器,其中包括引入正确的__ attribute__支持。 因为我找不到任何描述GNU GCC __ attribute__想法的官方BNF风格的语法(除了http://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html文档),我从C中提取点点滴滴通过网络找到的各种实现中的++ x11标准和注释。 我几乎完成了它(至少在解析上面引用的GCC文档中包含的示例时),但是一个特定的例子令我头疼,外部资源没有提示解决方案。 示例如下: __attribute__((noreturn)) void d0 (void), __attribute__((format(printf, 1, 2))) d1 (const char *, …), d2 (void); 附件说明: 属性说明符列表可以使用单个说明符和限定符列表,在多个标识符的声明中以逗号分隔的声明符列表中的声明符(不是第一个)之前出现。 此类属性说明符仅适用于它们出现在其声明符之前的标识符。 因此,引导我解决这个问题: init-declarator-list: init-declarator init-declarator-list , attribute-specifier-seq[opt] init-declarator 我知道它有效,但如果这是解决上述案例的正确方法,我想寻求validation/支持。 谢谢, 沃伊切赫 编辑:这个链接(虽然有点过时)给出了一个像我一样的解决方案: http : //plg.uwaterloo.ca/~cforall/gcc.y奇怪的是,我之前没有偶然发现它,只是在我做的时候搜索__ extension__关键字。

如何使用GNU hcreate_r

#include #include #include #include char *data[] = { “alpha”, “bravo”, “charlie”, “delta”, “echo”, “foxtrot”, “golf”, “hotel”, “india”, “juliet”, “kilo”, “lima”, “mike”, “november”, “oscar”, “papa”, “quebec”, “romeo”, “sierra”, “tango”, “uniform”, “victor”, “whisky”, “x-ray”, “yankee”, “zulu” }; int main(void) { ENTRY e, **ep; struct hsearch_data *htab; int i; int resultOfHcreate_r; resultOfHcreate_r=hcreate_r(30,htab); assert(resultOfHcreate_r!=0); hdestroy_r(htab); exit(EXIT_SUCCESS); } hcreate_r错误 如何使用这个hcreate_r […]