Tag: c

gdb可以使函数指针指向另一个位置吗?

我会解释一下: 假设我有兴趣替换某个应用程序使用的rand()函数。 所以我将gdb附加到此进程并使其加载我的自定义共享库(具有自定义的rand()函数): call (int) dlopen(“path_to_library/asdf.so”) 这会将自定义的rand()函数放在进程的内存中。 但是,此时符号rand仍将指向默认的rand()函数。 有没有办法让gdb将符号指向新的rand()函数,强制进程使用我的版本? 我必须说我也不允许使用LD_PRELOAD (linux)或DYLD_INSERT_LIBRARIES (mac os x)方法,因为它们只允许在程序执行开始时注入代码。 我想替换rand()的应用程序,启动几个线程,其中一些启动新进程,我有兴趣在其中一个新进程上注入代码。 正如我上面提到的,GDB非常适合这个目的,因为它允许代码注入特定的进程。

Scanf(“%c%f%d%c”)返回奇怪的值

我的类赋值要求我提示用户在一个输入行中输入四个变量char float int char。 这是整个代码: #include #include #include #include int main(void){ char h = ‘a’; char b, c, d, e; int m, n, o; float y, z, x; short shrt = SHRT_MAX; double inf = HUGE_VAL; printf(“Program: Data Exercises\n”); printf(“%c\n”, h); printf(“%d\n”, h); printf(“%d\n”, shrt); printf(“%f\n”, inf); printf(“Enter char int char float: “); scanf(“%c %d […]

使用argc = 0执行进程

是否可以执行argc = 0的进程? 我需要执行一个程序,但它的argc等于0是非常重要的。有没有办法做到这一点? 我试图在命令行中放置2 ^ 32个参数,使其看起来好像argc = 0但是参数数量有一个最大限制。

struct成员内存布局

如果我有这样的结构: struct S { ANY_TYPE a; ANY_TYPE b; ANY_TYPE c; } s; 我可以安全地假设以下假设在所有平台上始终都是正确的吗? ((char *)&s.a) < ((char *)&s.c) ((char *)&s.a + sizeof(sa) + sizeof(sb)) <= ((char *)&s.c) 在C ++中呢?

execl的返回值

我正在运行execl函数来通过我的代码编译程序。 我想检查编译是否正确。 所以我写了b = execl(“usr/bin/gcc”,”cc”,path,NULL); 然后检查if( b==-1) 。 但即使汇编中存在错误,它也没有进入if语句。 有什么想法吗? 谢谢! int b=0; if ((pid1 = fork())<0) perror("Error forking"); else { if(pid1==0) b= execl("/usr/bin/gcc","cc",path,NULL); else wait(&status); } if(b==-1) printf("\n——–\n");

来自带有预处理程序指令的c代码的AST

如何从gcc C代码构建AST(抽象语法树)以进行一些转换,如下所示,然后再将代码重新生成(生成)到C语法? if(condition_1){ //lines of code 1 } #ifdef expression_1 else if(condition_2){ //lines of code 2 } #endif 成 bool test = condition_1; if(teste){ //lines of code 1 } #ifdef expression_1 if(!(test) && condition_2){ //lines of code 2 } #endif

为什么getch()没有读取输入的最后一个字符?

我正在使用ncurses库在C中编写一个蛇游戏,其中屏幕每秒更新一次。 当玩游戏的人知道,如果用户输入各种键或长按键,则不应存储“缓冲”按键。 换句话说,如果我按住w (向上键)并且stdin接收到20 w s的序列,然后输入d (右键),我希望蛇立即向右移动,并忽略缓冲w s。 我试图使用ncurses函数getch()实现这一点,但由于某种原因,我实现了我刚才描述的不良效果; 即在考虑最后一个按键之前,首先存储和处理任何长按键,因为这个MWE将说明: #include #include #include int main(){ char c = ‘a’; initscr(); cbreak(); noecho(); for(;;){ fflush(stdin); timeout(500); c = getch(); sleep(1); printw(“%c”, c); } return 0; } 有没有什么办法可以修改这段代码,以便getch()忽略任何缓冲的文本? 之前的fflush()似乎没有帮助。

不想立即删除终止子进程,需要成为僵尸

我从SE QUE获得以下信息明确地将SIGCHLD的处置设置为SIG_IGN会导致随后终止的任何子进程立即从系统中删除而不是转换为僵尸。 据我所知,要读取子状态,需要在进程表中使用子进程。 因此有必要在进程表中使用zombie子进程来读取子状态。 所以我想编写信号处理程序,它将删除“将SIGCHLD的处置设置为SIG_IGN” 我使用下面的代码(在fork之前)以避免在终止后立即删除子进程:但我仍然无法获得子状态并且waitpid使用ECHILD返回-1。 #include #include #include #include #include #include static siginfo_t sig_info; static volatile sig_atomic_t sig_num; static void *sig_ctxt; static void catcher(int signum, siginfo_t *info, void *vp) { sig_num = signum; sig_info = *info; sig_ctxt = vp; } static void set_handler(int signum) { struct sigaction sa; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = catcher; […]

在Python中编译C代码的问题

我在Windows 7,64位中使用Anaconda for Python 2.7.10。 我也使用Visual Studio 2010.我安装了Microsoft Visual Studio for Python。 当我尝试在python中编译C代码时(在cmd中): C:\Anaconda\sms-tools-master\software\transformations_interface>python compileModule.py build_ext –inplace 我得到了很多警告和一些错误,最后一部分如下: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\link.exe /DLL / nologo /INCREMENTAL:NO /LIBPATH:C:\Anaconda\libs /LIBPATH:C:\Anaconda\PCbuild\am d64 m.lib /EXPORT:initutilFunctions_C build\temp.win-amd64-2.7\Release\utilFunct ions.obj build\temp.win-amd64-2.7\Release\cutilFunctions.obj /OUT:C:\Anaconda\sm s-tools-master\software\models\utilFunctions_C\utilFunctions_C.pyd /IMPLIB:build \temp.win-amd64-2.7\Release\utilFunctions_C.lib /MANIFESTFILE:build\temp.win-amd 64-2.7\Release\utilFunctions_C.pyd.manifest LINK : fatal error LNK1181: cannot open input file ‘m.lib’ error: command ‘C:\\Program […]

数学能力运算符未按预期工作

short sho1, sho2; printf(“Enter two shorts.\n”); scanf(“%hd %hd”, &sho1, &sho2); printf(“%hd^%hd is %hd.\n”, sho1, sho2, sho1^sho2); 当我输入’2 2’时,我得到这个输出: 2 ^ 2是0。 怎么会? 我在Eclipse中使用MinGW GCC编译器,以防万一。