我一直在阅读关于bash脚本和程序测试的文章,但我仍然无法使这段代码工作.
基本上它是一个简单的程序,要求用户提供north east south或west.我启动程序然后它立即要求输入.我只是不能让bash脚本给它任何输入.我试过用echo和expect.
任何帮助表示赞赏.
以下是用于获取玩家输入的函数:
int process_input(Map *game)
{
printf("\n> ");
char ch = getchar();
char a = getchar(); //eat enter
int damage = rand() % 4;
switch(ch) {
case -1:
printf("you suck\n");
return 0;
break;
case 'n':
game->proto.move(game, NORTH);
break;
case 's':
game->_(move)(game, SOUTH);
break;
case 'e':
game->_(move)(game, EAST);
break;
case 'w':
game->_(move)(game, WEST);
break;
case 'a':
game->_(attack)(game, damage);
break;
case 'l':
printf("You can go:\n");
if(game->location->north) printf("NORTH\n");
if(game->location->south) printf("SOUTH\n");
if(game->location->east) …Run Code Online (Sandbox Code Playgroud) 我在Linux上使用ALSA和音频应用程序,我发现很棒的文档解释了如何使用它: 1和这一个.虽然我有一些问题需要理解这部分设置:
/* Set number of periods. Periods used to be called fragments. */
if (snd_pcm_hw_params_set_periods(pcm_handle, hwparams, periods, 0) < 0) {
fprintf(stderr, "Error setting periods.\n");
return(-1);
}
Run Code Online (Sandbox Code Playgroud)
什么意思是设置我使用PLAYBACK模式时的一段时间,并且:
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if (snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (periodsize * periods)>>2) < 0) {
fprintf(stderr, "Error setting buffersize.\n");
return(-1);
}
Run Code Online (Sandbox Code Playgroud)
这里有关于延迟的相同问题,我该如何理解?
有人能给我一个关于第二行代码中发生的事情的完整解释吗?
我知道包含shellcode的缓冲区的地址被转换为一个执行的函数指针.但我对所涉及的所有括号和步骤感到困惑,所以我需要更详细的解释.
unsigned char buf[] = "\x90\x90\x90\x90\x90\x90\x90\x90";
((void(*)())buf)();
Run Code Online (Sandbox Code Playgroud)
我试着用这种方式解释一下:
buf //address of the buffer containing code
void(*)() //"type" function pointer returning void, no parameters
(void(*)()) buf //cast buf to said type
( (void(*)()) buf )() //take the outcome of the cast and execute it by appending ()
Run Code Online (Sandbox Code Playgroud)
它是否正确?
编辑: 我知道DEP会阻止执行,即使它会执行,程序也会崩溃,因为它会在NOP之后执行"随机垃圾".我的问题只是函数调用的语法.
如果C编译器填充结构以便将字段与其原始对齐对齐,然后初始化该结构,则填充是否初始化为零?
例如以下结构:
typedef struct foo_t_ {
int a;
char b;
int c;
char d;
} foo_t;
Run Code Online (Sandbox Code Playgroud)
在许多系统上,这个(设计不佳)的结构将具有sizeof(foo_t)16个,总共6个字节的填充,每个字符后3个字节.
如果我们初始化结构,如:
foo_t foo = { .a = 1, .b = '2' };
Run Code Online (Sandbox Code Playgroud)
然后字段foo.a将设置为1并将foo.b设置为字符"2".未指定的字段(`foo.c'和'foo.d')将自动设置为0.问题是,6个字节的填充会发生什么?那还会自动设置为0吗?还是未定义的行为?
用例是我将计算数据结构的哈希值:
foo_t foo = { .a = 1, .b = '2' };
foo_t bar = { .a = 1, .b = '2' };
uint32_t hash_foo = calc_hash(&foo, sizeof(foo));
uint32_t hash_bar = calc_hash(&bar, sizeof(bar));
Run Code Online (Sandbox Code Playgroud)
我想确定hash_foo并且hash_bar是一样的.我可以通过首先使用memset()清除结构然后初始化它来保证这一点,但是使用C初始化代替它似乎更清晰.
在实践中,我的系统上的GCC也确实清除了填充,但我不知道这是否有保证.
我想我们都同意,通过以一维方式解引用(可能是偏移的)指向其第一个元素的指针来访问真正的多维数组被认为是惯用的C,例如:
void clearBottomRightElement(int *array, int M, int N)
{
array[M*N-1] = 0; // Pretend the array is one-dimensional
}
int mtx[5][3];
...
clearBottomRightElement(&mtx[0][0], 5, 3);
Run Code Online (Sandbox Code Playgroud)
然而,我的语言律师需要说服这实际上是明确定义的C!特别是:
是否标准保证编译器不会把填充例如,在中间mtx[0][2]和mtx[1][0]?
通常,索引关闭数组的末尾(除了结尾之外)是未定义的(C99,6.5.6/8).所以以下内容显然是未定义的:
struct {
int row[3]; // The object in question is an int[3]
int other[10];
} foo;
int *p = &foo.row[7]; // ERROR: A crude attempt to get &foo.other[4];
Run Code Online (Sandbox Code Playgroud)
因此,根据相同的规则,人们会期望以下内容未定义:
int mtx[5][3];
int (*row)[3] = &mtx[0]; // The object in question is still an int[3]
int *p = &(*row)[7]; …Run Code Online (Sandbox Code Playgroud)我有这段代码:
function MyFunction()
{
$.ajax({
type: "POST",
url: "ajax.php",
dataType: "json",
data: "foo=bar",
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert(arguments.callee);
},
success: function(jsonObject)
{
//do something
}
});
}
Run Code Online (Sandbox Code Playgroud)
我想要的是de error scoope里面的警告显示了函数名,在本例中是"MyFunction",而是我得到的是错误:function.
我怎样才能做到这一点?
我试图编译被划分成3个模块,对应于3个源文件的程序:a.c,b.c,和z.c.z.c包含main()函数,它调用函数a.c和b.c.此外,函数a.c调用函数b.c,反之亦然.最后,有一个全局变量count,由三个模块使用,并在一个单独的头文件中定义global.h.
源文件的代码如下:
a.c
#include "global.h"
#include "b.h"
#include "a.h"
int functAb() {
functB();
functA();
return 0;
}
int functA() {
count++;
printf("A:%d\n", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
b.c
#include "global.h"
#include "a.h"
#include "b.h"
int functBa() {
functA();
functB();
return 0;
}
int functB() {
count++;
printf("B:%d\n", count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
z.c
#include "a.h"
#include "b.h"
#include "global.h"
int …Run Code Online (Sandbox Code Playgroud) 我不确定在初始化后以下列方式在char数组中会出现什么:
char buf[5]={0,};
Run Code Online (Sandbox Code Playgroud)
这相当于
char buf[5]={0,0,0,0,0};
Run Code Online (Sandbox Code Playgroud) 我在很多场合看到过用const类型限定符来定义函数:
const int foo (int arg)
Run Code Online (Sandbox Code Playgroud)
这有什么意义?函数的返回值无论如何都无法改变..
我在讲座中被教过,free()两次拨打指针真是非常糟糕.我知道NULL在释放它之后立即设置指针是一种很好的做法.
但是,我仍然没有听到任何解释为什么会这样.根据我的理解,方式malloc()有效,它应该在技术上跟踪它已经分配的指针并让你使用.那么为什么它不知道它接收到的指针是否free()已被释放?
当你打电话free()给以前已经被释放的位置时,我很乐意理解,内部会发生什么.