Reputation: 1
I'm having trouble with memcpy() and I don't have a clue on where I went wrong.
Code can be seen here: http://pastebin.com/tebksExR
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tmp__ {
unsigned int num;
unsigned short id;
unsigned short type;
} tmp_str;
int
main(int argc, char **argv)
{
tmp_str hdr;
char *str = NULL;
str = calloc(18, sizeof(char));
memset(&hdr, 0, sizeof(hdr));
hdr.num = 0;
hdr.id = 0;
hdr.type = 21845;
memcpy((void *) str, (void *) &hdr, sizeof(hdr));
printf("STR: %s\n", str);
free(str);
return 0;
}
On executing it, all I see is just "STR". Nothing is seen on the memory region pointed by str too.
(gdb) b 23
Breakpoint 1 at 0x8048494: file memcpy.c, line 23.
(gdb) run
Starting program: /home/a.out
Breakpoint 1, main (argc=1, argv=0xbffff234) at memcpy.c:23
23 memcpy((void *) str, (void *) &hdr, sizeof(hdr));
(gdb) n
24 printf("STR: %s\n", str);
(gdb) n
STR:
26 free(str);
(gdb) info locals
hdr = {num = 0, id = 0, type = 21845}
str = 0x804b008 ""
Where did I go wrong?
Thanks!
Upvotes: 0
Views: 1227
Reputation: 46375
As was pointed out, since the first byte of str
is '\0'
. a simple printf("%s",str);
statement will do nothing (it will stop at the first byte which indicates "end of string"). Instead, you could try
int ii;
printf("STR in hex:\n");
for(ii = 0; ii < sizeof(hdr); ii++) {
printf("%02x ", str[ii]);
}
printf("\n");
This will now print every byte in str
as a hex number; you will see that the copy went OK.
If you would like, you could replace this with
int ii;
printf("STR in hex:\n");
for(ii = 0; ii < sizeof(hdr); ii++) {
printf("%c", str[ii]);
}
printf("\n");
And you will see the actual characters (but some of them may be "unprintable" and might have unexpected side effects in your terminal).
Upvotes: 2
Reputation: 165
Your data is getting moved. If you declared:
tmp_str* str2 = (tmp_str*)str;
then it would display properly in the debugger. You're just looking at the data wrong-- your code is executing properly.
Upvotes: 2
Reputation: 1510
Well, you set hdr.num = 0;
, so the first byte of copied chunk of memory is equal to 0
, so the first character of your string is NULL, which marks it's ending, so nothing is printed.
Upvotes: 1
Reputation: 8861
str
is a string (char *
terminated by '\0'
) and hdr
is a structure, which doesn't even have a string field. If you want to convert hdr
to a human-readable string, you'll have to use printf
or sprintf
with the proper conversion specifiers.
Upvotes: 1
Reputation: 280563
When you specify %s
, printf
expects a null-terminated string. It can't tell that you allocated 18 bytes and you want the contents of those bytes printed. It looks at *str
, sees a null byte, and stops looking.
Upvotes: 2