缓冲区开头的memcpy垃圾 [英] memcpy junk at beginning of buffer
问题描述
我试图从填充的简单结构中记忆缓冲区。
当我记忆然后打印生成的缓冲区时,我看到7个位置
之前有垃圾我的数据开始了。我的数据结构是:
struct command_pkt {
char command_num [3];
char命令[100];
};
typedef command_pkt命令;
相关部分如下:
>
sprintf(tmp,"%s%s",ip,host);
COMMAND * com;
strcpy(com- > command_num," 1");
strcpy(com-> command,tmp);
int len = sizeof(COMMAND);
unsigned char buf [200];
if(len> 200){
printf(" ERROR - len> buf \ n");
返回-1;
}
memset(& buf [0],0,200);
memcpy(& buf [1],(unsigned char *)& com,len);
printf(" COM buf:<% s> \ n",buf);
为什么在我的缓冲区开头有垃圾的任何想法?
Im trying to memcpy a buffer from a filled in simple structure.
When I memcpy and then print the resulting buffer, I see 7 locations
that have junk before my data starts. My data structure is:
struct command_pkt {
char command_num[3];
char command[100];
};
typedef command_pkt COMMAND;
The relevant portion is the following:
sprintf(tmp,"%s %s",ip,host);
COMMAND *com;
strcpy(com->command_num,"1");
strcpy(com->command,tmp);
int len = sizeof(COMMAND);
unsigned char buf[200];
if (len > 200) {
printf("ERROR - len > buf\n");
return -1;
}
memset(&buf[0],0,200);
memcpy(&buf[1],(unsigned char *)&com,len);
printf("COM buf: <%s>\n",buf);
Any ideas why there is junk at the beginning of my buffer?
推荐答案
Jeff< je ** @ rahul.net>写道:
Jeff <je**@rahul.net> wrote:
相关部分如下:
COMMAND * com;
strcpy(com-> command_num," 1");
strcpy(com-> command,tmp);
为什么在我的缓冲区开头有垃圾的任何想法?
The relevant portion is the following: COMMAND *com;
strcpy(com->command_num,"1");
strcpy(com->command,tmp); Any ideas why there is junk at the beginning of my buffer?
我想象com没有指向分配空间的事实是
部分问题。
com = malloc(sizeof(COMMAND)); / *错误检查* /
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
I imagine the fact that com does not point at allocated space is a
part of your problem.
com=malloc( sizeof(COMMAND) ); /* with error checking as appropriate */
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
" Jeff" < JE ** @ rahul.net>在消息中写道
news:11 ********************* @ g47g2000cwa.googlegro ups.com ...
"Jeff" <je**@rahul.net> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
当我记忆并打印生成的缓冲区时,我看到7个位置在我的数据开始之前有垃圾。我的数据结构是:
struct command_pkt {
char command_num [3];
char命令[100];
};
typedef command_pkt命令;
相关部分如下:
sprintf(tmp,"%s%s",ip,host);
COMMAND * com;
strcpy(com-> command_num," 1");
strcpy(com-> command,tmp);
int len = sizeof(COMMAND);
unsigned char buf [200];
if(len> 200){
printf(" ERROR - len> buf \ n");
返回-1;
}
memset(& buf [0],0,200);
memcpy(& buf [1],(unsigned char *)& ; com,len);
printf(" COM buf:<%s> \ n",buf);
任何想法,为什么有垃圾我的缓冲区的开头?
我到目前为止,然后停止了。
sprintf(tmp,"%s%s",ip,host);
命令* com;
strcpy(com-> command_num," 1");
strcpy(com-> command,tmp);
Im trying to memcpy a buffer from a filled in simple structure.
When I memcpy and then print the resulting buffer, I see 7 locations
that have junk before my data starts. My data structure is:
struct command_pkt {
char command_num[3];
char command[100];
};
typedef command_pkt COMMAND;
The relevant portion is the following:
sprintf(tmp,"%s %s",ip,host);
COMMAND *com;
strcpy(com->command_num,"1");
strcpy(com->command,tmp);
int len = sizeof(COMMAND);
unsigned char buf[200];
if (len > 200) {
printf("ERROR - len > buf\n");
return -1;
}
memset(&buf[0],0,200);
memcpy(&buf[1],(unsigned char *)&com,len);
printf("COM buf: <%s>\n",buf);
Any ideas why there is junk at the beginning of my buffer?
I got this far, then stopped.
sprintf(tmp,"%s %s",ip,host);
COMMAND *com;
strcpy(com->command_num,"1");
strcpy(com->command,tmp);
如果这真的来自你的代码,实际指向的是什么,
即,你没有为COMMAND分配内存,并且指向了那个
记忆。
If this is really taken from your code, what''s com actually pointing to,
i.e., you not allocated memory for a COMMAND, and pointed com to that
memory.
Christopher Benson-Manica< at *** @ nospam.cyberspace.org>写道:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
COMMAND * com;
COMMAND *com;
com = malloc(sizeof(COMMAND)); / *错误检查* /
com=malloc( sizeof(COMMAND) ); /* with error checking as appropriate */
或者更确切地说,
com = malloc(sizeof * com); /* 看到不同? * /
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
Or rather,
com=malloc( sizeof *com ); /* see the difference? */
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于缓冲区开头的memcpy垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!