我试图在这里略微调整规则和malloc
缓冲区,然后将函数复制到缓冲区.
调用缓冲函数有效,但是当我试图调用其中的另一个函数时,该函数会抛出分段错误.
有什么想法?
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
int foo(int x)
{
printf("%d\n", x);
}
int bar(int x)
{
}
int main()
{
int foo_size = bar - foo;
void* buf_ptr;
buf_ptr = malloc(1024);
memcpy(buf_ptr, foo, foo_size);
mprotect((void*)(((int)buf_ptr) & ~(sysconf(_SC_PAGE_SIZE) - 1)),
sysconf(_SC_PAGE_SIZE),
PROT_READ|PROT_WRITE|PROT_EXEC);
int (*ptr)(int) = buf_ptr;
printf("%d\n", ptr(3));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码将抛出一个段错误,除非我将foo
函数更改为:
int foo(int x)
{
//Anything but calling another function.
x = 4;
return x;
}
Run Code Online (Sandbox Code Playgroud)
注意:
代码成功复制foo
到缓冲区,我知道我做了一些假设,但在我的平台上他们没问题.
#include <stdio.h>
int main() {
int i,n;
int a = 123456789;
void *v = &a;
unsigned char *c = (unsigned char*)v;
for(i=0;i< sizeof a;i++) {
printf("%u ",*(c+i));
}
char *cc = (char*)v;
printf("\n %d", *(cc+1));
char *ccc = (char*)v;
printf("\n %u \n", *(ccc+1));
}
Run Code Online (Sandbox Code Playgroud)
该程序在我的32位Ubuntu机器上生成以下输出.
21 205 91 7
-51
4294967245
Run Code Online (Sandbox Code Playgroud)
前两行输出我能理解=>
请解释最后一行输出.为什么添加三个1的字节,因为(11111111111111111111111111001101) = 4294967245
.
当我声明下面给出的结构时,它会抛出编译错误.
typedef struct{
const char x; //it is throwing compilation error
const char y;
}A;
void main()
{
A *a1;
a1 = (A*)malloc(2);
}
Run Code Online (Sandbox Code Playgroud)
如何使结构的字段(字符x
和y
)保持不变?
我正在尝试按照此链接并使用命令将视频的关键帧间隔更改为 1
ffmpeg -i myvideo.mp4 -vcodec libx264 -x264-params keyint=30:no-scenecut -acodec copy out.mp4
Run Code Online (Sandbox Code Playgroud)
如第一个答案所示。我写的是keyint=30
我的视频,所以。然而,只有第一帧的关键帧是,其余的都保留(如上面命令之前),如命令所示:fps
30
1*30=30
1
0
ffprobe -select_streams v:0 -show_frames out.mp4
...
[FRAME]
media_type=video
stream_index=0
key_frame=1
pkt_pts=0
pkt_pts_time=0.000000
pkt_dts=0
pkt_dts_time=0.000000
best_effort_timestamp=0
best_effort_timestamp_time=0.000000
pkt_duration=512
pkt_duration_time=0.033333
pkt_pos=48
pkt_size=18693
width=560
height=320
pix_fmt=yuv420p
sample_aspect_ratio=N/A
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]
[FRAME]
media_type=video
stream_index=0
key_frame=0
pkt_pts=512
pkt_pts_time=0.033333
pkt_dts=512
pkt_dts_time=0.033333
best_effort_timestamp=512
best_effort_timestamp_time=0.033333
pkt_duration=512
pkt_duration_time=0.033333
pkt_pos=21764
pkt_size=199
width=560
height=320
pix_fmt=yuv420p
sample_aspect_ratio=N/A
pict_type=B
coded_picture_number=3
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]
... …
Run Code Online (Sandbox Code Playgroud) 我试图用来getchar()
读取输入数字的所有数字并将它们存储在一个数组中.但每次我运行程序时,第二个数字都会出错.
这是我的代码:
int ch = 0;
int digits[0];
int i = 0;
while ((ch = getchar()) != '\n') {
digits[i] = ch - '0';
i++;
}
Run Code Online (Sandbox Code Playgroud)
为了表明会出现什么问题,我插入了两个printf
:
while ((ch = getchar()) != '\n') {
printf("%d ", ch);
digits[i] = ch - '0';
printf("%d\n", ch);
i++;
}
Run Code Online (Sandbox Code Playgroud)
例如,当我输入时1100
,我得到:
49 49
49 1
48 48
48 48
当我在一个单独的循环中打印数组时,输出是:
1 10 0 0
当我输入时66666
,我得到:
54 54
54 6
54 54
54 54
54 5
阵列是: …
检查这个C
程序:
#include <stdio.h>
int main(void) {
// your code goes here
char **p = NULL;
printf("%d,%d\n", sizeof(*p), sizeof(**p));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行上面的代码,结果是:
8,1
Run Code Online (Sandbox Code Playgroud)
虽然p
是NULL
,它不会导致程序崩溃sizeof(*p)
和sizeof(**p)
.如何理解这种行为?它是否符合c规范?
我已经问过这个问题了,但这个特定子部分的答案对我来说并不清楚.请参阅以下代码:
float f=2.5;
if (f==2.5)
printf("abc");
else
printf("xyz"); //prints abc
Run Code Online (Sandbox Code Playgroud)
我知道如果我们2.2
取而代之2.5
,它将输出相反的值,因为float被视为double和浮点nos.他们真的不应该等同吗?我想知道为什么有一个例外,数字有他们的最后一个十进制数字5
.
我是linux编程的新手,我想得到一些关于杀死一个开始使用的进程的建议execvp()
.以下是"TestApplication"
作为子进程启动的代码.当用户中断(ctrl + C)时,我想"TestApplication"
和父进程一起杀死它.
有关如何实现这一目标的任何建议.PLS.救命.谢谢.
int main(int argc, char* argv[])
{
signal(SIGINT, KillProcess);
pid_t pid;
pid = fork();
if(pid == -1)
{
printf("Error: Fork process failed");
exit(-1);
}
else if (pid == 0)
{
char *const paramList[] = {"5"," 1", NULL};
execvp("TestApplication", paramList);
}
else
{
// Wait for signal from the TestApplication process when successfully executed
}
return 0;
}
void KillProcess(int sig)
{
// Want to get the process ID of "TestApplication"
// …
Run Code Online (Sandbox Code Playgroud) 休息还可以,但我不明白这部分发生了什么:
for(x = 15; x%i?++ i:++ k,i <x; x--,i ++)
#include <stdio.h>
#include <stdlib.h>
int main() {
int x, i = 2, k = 1;
for (x = 15; x % i ? ++i : ++k, i < x; x--, i++) {
x = -k + i;
i++;
printf("X = %d, I = %d, K = %d\n", x, i, k);
}
switch (x) {
case 2: printf("Display 2.\n");
case 5: printf("Display 5.\n");
case 0: printf("Display 0.\n");
break;
default: printf("Display …
Run Code Online (Sandbox Code Playgroud) 我有一个程序打印出一个非常小的值的矩阵.我的矩阵的一个例子是
0.00000000000000004 0.12300000000000000
0.00000000011111114 0.00000000000038544
Run Code Online (Sandbox Code Playgroud)
我想要做的是比较每个值,zero
并接受它zero
具有特定的准确性,即小数点后9位.换句话说,如果一个数字zeros
的第一个十进制值为9 ,我想将其视为a zero
,否则不是.
我搜索了很多但却一无所获.有任何想法吗?
所以我有一个示例代码,用于为c中的迷你扑克游戏创建一副牌.但我不明白西装和面孔是如何确定的.为什么这些阵列有2个维度?我知道[9]
并且[6]
是数组的列,但我不明白它们的目的.
char suits[4][9]= {"Hearts","Diamonds","Clubs","Spades"};
char faces[13][6]= {"Ace","2","3","4","5","6","7","8","9", "10","Jack",
"Queen","King"};
Run Code Online (Sandbox Code Playgroud)