Problem when copying memory

戏子无情 提交于 2019-12-02 14:23:39

I would suspect issues with alignment and/or padding, what are the type declarations of tuple1 and tuple2?

How do you know their exact sizes? Code that hardcodes things is suspect, there should be some use of sizeof rather than magic number literals.

Also, you shouldn't cast the return value of calloc(), in C.

Begin your experiment with something smaller, which is easier to debug:

void* tuple1 = calloc(2, 1);
char* content1 = "ab";
memcpy(tuple1, content1, 2);

void* tuple2 = calloc(4, 1);
char* content2 = "cdef";;
memcpy(tuple2, content2, 4);

char *data = data = (char*) calloc (6, 1);
memcpy(data, tuple1, 2);
memcpy(data+2, tuple2, 4);

printf("%.*s\n", 6, data); // should print: abcdef
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!