-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!

4
  • 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. Commented Jun 6, 2014 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. Commented Jun 6, 2014 at 10:12
  • 2
    @Survaf93 b is not a single char, it's a pointer. Commented Jun 6, 2014 at 10:14
  • It's a single char* that's what I meant :P Commented Jun 6, 2014 at 10:15

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow!! You guys are really gud!! Thanks for the most perfect answer!! I will mark it answered as soon as I am allowed to!!
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);
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.

4 Comments

It's better if he uses malloc(strlen(a)+1);
True, but this example is 100% equivalent to b[50]
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 ?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.