-2

So there is a problem I am headbanging over two nights in a row:

(tuple1 and tuple2 are void pointers passed to this function)

char *data;

data = (char*) calloc (76, 1);
memcpy(data, tuple1, 32);
memcpy(data+32, tuple2, 44);

The idea is to allocate memory equal to the sum of the sizes of tuple1 and tuple2 (tuple1 is 32 bytes and tuple2 is 44) and then copy the 32 bytes of tuple1 and paste them at the address of data and after that copy the 44 bytes of tuple2 and paste them 32 bytes after the address of data.

The thing is if I copy only tuple1 or only tuple2 it is really copied where it is supposed to be (I am printing data with way too long function to put here), but when I do the two memory copies the first memcpy() works fine but the second doesn't.

Can anyone help me with this serious problem?

unwind
  • 391,730
  • 64
  • 469
  • 606
Ivan Kolev
  • 1
  • 1
  • 1
  • What you describe should have, by itself, worked. However, there are any number of reasons why code you didn't show could cause it not to work. Please post a complete program that, when compiled and run, shows the problem; also describe what it does and what you expected it to do instead. – zwol Apr 05 '11 at 15:56
  • Prove it. Show a complete program which displays the behavior you describe (when one or other of the memcpy lines is commented out). Hopefully in the process of cutting your real code down to that demonstration, you'll notice when it suddenly starts working again, and that will tell you what was wrong. – Steve Jessop Apr 05 '11 at 15:57
  • How do you *know* the second copy didn't work? What evidence do you have? – abelenky Apr 05 '11 at 15:58
  • What platform is this? On something other than x86, alignment issues could definitely come into play. – abelenky Apr 05 '11 at 16:00
  • I would bet on the code that prints the data having a bug. The code you've got should copy memory if tuple1 and tuple2 are both pointers. – vhallac Apr 05 '11 at 16:10
  • Are you sure there are no nul bytes in the data, causing the print to stop? – Bo Persson Apr 05 '11 at 17:30

2 Answers2

1

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

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
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • If you're going to pretend some data is a string, don't forget to account for the inexistence of the null terminator. Hint: use `printf("%.*s\n", 6, data);` – pmg Apr 05 '11 at 16:11
  • So, I found something interesting. I have allocated 84 bytes for data. I am doing the first memory copy memcpy(data, tuple1, 44); which copies 44 bytes from the address of tuple1 to the address of data and when I try to read data it turns out that it had copied the bytes of tuple1 on the first 44 bytes of data and then it had copied again the 44 bytes of tuple1 until it has filled the bytes 84 bytes allocated for data. Why is that?! And how can I prevent it? – Ivan Kolev Apr 05 '11 at 20:56