-1

I have a query about using the memcpy() function.I have written the below program, it compiles but doesn't print an output. The .exe opens and the crashes. I am using Code Blocks as my IDE using GNU GCC compiler.

int main()
{
    char a[]= "This is my test";
    char *b;
    memcpy(b,a,strlen(a)+1);
    printf("After copy =%s\n",b);
    return(0);
}

However, if I change the array *b to b[50] it works!! I don't understand why.

Please provide your suggestions!

Thanks!

Kashif Nawaz
  • 95
  • 1
  • 1
  • 13
  • 1
    memcpy attempts to write to memory `b` points to - which is unknown. `b` is unitialised - it could be anywhere, and there is absolutely no reason to think your program is allowed to write to that memory location. – keltar Jun 06 '14 at 10:12
  • As Graham Borland said, your char *b isn't initialized, it's just a single char*, you need to allocate a memory for it by using malloc() and specify it's size, then it'll work. – Survaf93 Jun 06 '14 at 10:12
  • 2
    @Survaf93 `b` is not a single char, it's a pointer. – Graham Borland Jun 06 '14 at 10:14
  • It's a single char* that's what I meant :P – Survaf93 Jun 06 '14 at 10:15

2 Answers2

4

Your pointer b is uninitialized. It is pointing to some random location in memory. So when you copy stuff into the memory which b is pointing to, bad things are likely to happen.

You need to initialize it; perhaps allocate some memory for it with malloc().

char *b = malloc(strlen(a) + 1);

And then free it when you're finished.

free(b);
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • Wow!! You guys are really gud!! Thanks for the most perfect answer!! I will mark it answered as soon as I am allowed to!! – Kashif Nawaz Jun 06 '14 at 10:15
  • Now since I know the working of this memcpy() function, I would like to use this for the ARM compiler.I am trying to copy the RXed bytes from UART to my SRAM in the stellaris device. I am using the below code but it keeps giving errors! char mycharacter; mycharacter = ROM_UARTCharGetNonBlocking(UART0_BASE); memcpy(SRAM_BASE, mycharacter, size_t (mycharacter); – Kashif Nawaz Jun 06 '14 at 10:19
2

You are lucky it did not crash when you used pointer - it should have.

When you copy memory, destination must be allocated first. If you use char b[50], you allocate 50 bytes for b on stack. If you use char *b, you did not allocate anything yet, and typically should do this using something like malloc : b = malloc(50);.

With malloc it will work, but then you should not forget to release that memory with free(b);.

If memory was allocated on stack, release happens automatically.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • It's better if he uses malloc(strlen(a)+1); – Survaf93 Jun 06 '14 at 10:14
  • True, but this example is 100% equivalent to `b[50]` – mvp Jun 06 '14 at 10:16
  • For this specific situation it will work but don't you think it's better for him/her to understand the functionality of malloc() and memory allocation ? – Survaf93 Jun 06 '14 at 10:23
  • 1
    Well, I already explained quite a bit about malloc/free here. To understand it really well one needs to read a book or at least `man malloc`, `man free`, `man memcpy` – mvp Jun 06 '14 at 10:27