Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 字符缓冲区中的多个缓冲区_C_Pointers_Char_Buffer_Void - Fatal编程技术网

C 字符缓冲区中的多个缓冲区

C 字符缓冲区中的多个缓冲区,c,pointers,char,buffer,void,C,Pointers,Char,Buffer,Void,为什么我不能得到输出“哈罗”?我仍然不确定是否使用指针算术和缓冲区 关于应该是: int main(int argc, char **args) { unsigned char* str = "hallo"; printf("String: %s\n",str); uint8_t aktion, id; uint32_t str_length; aktion = 1; id = 4; str_length = strlen(str)+1;

为什么我不能得到输出“哈罗”?我仍然不确定是否使用指针算术和缓冲区

关于

应该是:

int main(int argc, char **args) {
    unsigned char* str = "hallo";
    printf("String: %s\n",str);
    uint8_t aktion, id;
    uint32_t str_length;
    aktion = 1;
    id = 4;
    str_length = strlen(str)+1;

    unsigned char *buff;    
    buff = (unsigned char*) calloc(1,str_length+6);
    memcpy(buff, &id, sizeof(id));
    memcpy(buff+sizeof(id), &str_length, sizeof(str_length));
    strcpy(buff+sizeof(id)+sizeof(str_length), str);
    printf("Buffer+5: %s\n",buff+5));
    memcpy(buff+sizeof(id)+sizeof(str_length)+str_length, &aktion, sizeof(aktion));
    return 0;
}
str
已经是指针。相比之下,
&str
是指针的地址,而不是您想要的

您还需要在缓冲区中为两个初始变量留出空间。

应该是:

int main(int argc, char **args) {
    unsigned char* str = "hallo";
    printf("String: %s\n",str);
    uint8_t aktion, id;
    uint32_t str_length;
    aktion = 1;
    id = 4;
    str_length = strlen(str)+1;

    unsigned char *buff;    
    buff = (unsigned char*) calloc(1,str_length+6);
    memcpy(buff, &id, sizeof(id));
    memcpy(buff+sizeof(id), &str_length, sizeof(str_length));
    strcpy(buff+sizeof(id)+sizeof(str_length), str);
    printf("Buffer+5: %s\n",buff+5));
    memcpy(buff+sizeof(id)+sizeof(str_length)+str_length, &aktion, sizeof(aktion));
    return 0;
}
strcpy(buff + sizeof id + sizeof str_len, str);
/*                                        ^^^^   no '&'!  */
str
已经是指针。相比之下,
&str
是指针的地址,而不是您想要的

您还需要在缓冲区中为两个初始变量留出空间

strcpy(buff + sizeof id + sizeof str_len, str);
/*                                        ^^^^   no '&'!  */
您不应该在C中强制转换malloc/calloc的返回指针

您的
buff
大小为6

uint32_t str_length;
aktion = 1;
id = 4;
str_length = strlen(str)+1;

unsigned char *buff;    
buff = (unsigned char*) calloc(1,str_length);
你在这里写了1个字节

memcpy(buff, &id, sizeof(uint8_t));
您在此处写入了4个字节:

这意味着为
buff

memcpy(buff+1, &str_length, sizeof(uint32_t));
您正在写入超过为buff分配的字节数。这将导致内存损坏

您不应该在C中强制转换malloc/calloc的返回指针

您的
buff
大小为6

uint32_t str_length;
aktion = 1;
id = 4;
str_length = strlen(str)+1;

unsigned char *buff;    
buff = (unsigned char*) calloc(1,str_length);
你在这里写了1个字节

memcpy(buff, &id, sizeof(uint8_t));
您在此处写入了4个字节:

这意味着为
buff

memcpy(buff+1, &str_length, sizeof(uint32_t));

您正在写入超过为buff分配的字节数。这将导致内存损坏 > CaloC的结果,我假设这是C++。你应该使用
std::string
,不要乱用裸指针。然后你不应该铸造
calloc
的结果。而且,裸
1
5
都很奇怪。如果您已经知道了大小,为什么还要为
sizeof
操心呢?最好的办法是在任何地方都写
sizeof
,不要有幻数,用
sizeof id
等替换
sizeof(uint8_t)
。为什么,你能给我一些背景信息吗?省略了强制转换没有帮助。不,它没有帮助,它只是更好的C。这就是为什么它是一个注释:-)你用“代码> CaloC:<代码>的方式,我假设这是C++。你应该使用
std::string
,不要乱用裸指针。然后你不应该铸造
calloc
的结果。而且,裸
1
5
都很奇怪。如果您已经知道了大小,为什么还要为
sizeof
操心呢?最好的办法是在任何地方都写
sizeof
,不要有幻数,用
sizeof id
等替换
sizeof(uint8_t)
。为什么,你能给我一些背景信息吗?省略演员阵容没有帮助。不,它没有帮助,它只是更好的C。这就是为什么它是一个评论:-)啊好的,缓冲区大小是错误的。改变了,现在它工作了!谢谢,好的,缓冲区大小不对。改变了,现在它工作了!谢谢