Re: memcpy junk at beginning of buffer
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 29 Dec 2005 15:29:47 -0600
On 29 Dec 2005 12:10:54 -0800, "Jeff" <jeep@xxxxxxxxx> wrote in
comp.lang.c:
> 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 line above is not legal C. There is no such thing as a
'command_pkt'. Either your actual code has:
typedef struct command_pkt COMMAND;
....or you are not compiling with a C compiler.
In any case, it's not a particularly good idea to create aliases for
structure types, and an extremely bad idea to define them with ALL
UPPER CASE LETTERS, which should be reserved for macros and, possibly,
enumeration constants.
> The relevant portion is the following:
>
> sprintf(tmp,"%s %s",ip,host);
Where are tmp, ip, and host defined and given values?
> COMMAND *com;
Here you create an uninitialized pointer, which yo do not have the
right to dereference, let alone write through.
> strcpy(com->command_num,"1");
> strcpy(com->command,tmp);
Undefined behavior, writing through an uninitialized pointer.
> int len = sizeof(COMMAND);
The sizeof operator yields a value of type size_t. Given your
definition of the structure, this value will fit into an int, but why
not use the actual type?
> unsigned char buf[200];
Are you using a C99 conforming compiler, or are you using a different
language, as I suspected above. Both the definition of 'len' and of
'buf' are not valid under any version of the C standard prior to 1999.
> if (len > 200) {
> printf("ERROR - len > buf\n");
> return -1;
> }
> memset(&buf[0],0,200);
This would be more gracefully written as:
memset(buf, 0, 200);
> memcpy(&buf[1],(unsigned char *)&com,len);
This would be more gracefully written as:
memset(buff + 1, com, len);
....note no cast is needed on 'com', any type of pointer to object may
be automatically converted to a pointer to void. Even in the other,
not-C, language that I expect you are using.
> printf("COM buf: <%s>\n",buf);
>
> Any ideas why there is junk at the beginning of my buffer?
There is something seriously wrong if the output has anything other
than white space after the ':' and the newline. buf[0] contains the
string terminator, '\0'.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- memcpy junk at beginning of buffer
- From: Jeff
- memcpy junk at beginning of buffer
- Prev by Date: Re: warning : no new line at end of file
- Next by Date: Re: memcpy junk at beginning of buffer
- Previous by thread: Re: memcpy junk at beginning of buffer
- Next by thread: Re: memcpy junk at beginning of buffer
- Index(es):
Relevant Pages
|