Allocate space for null termination character when copying strings in C?

const char * src = "hello";

Call strlen(src); returns size 5 ...

Now I will say that I am doing this:

 char* dest = new char[strlen(src)]; strcpy(dest, src); 

It doesn't seem like it should work, but when I output everything, everything looks right. I don't seem to allocate space for the null terminator at the end ... is this correct? Thanks

+4
source share
8 answers

You are correct that you do not allocate space for the terminator, however . Failure to do this will not necessarily cause your program to crash. You can overwrite the following information about the heap, or your heap manager will round up the allocation to a multiple of 16 bytes or something like that, so you will not see any visible effects of this error.

If you run your program under Valgrind or another heap debugger, you may find this problem earlier.

+13
source

Yes, you must allocate at least strlen (src) +1 characters.

+11
source

It doesn't seem like it should work, but when I output everything, it looks right.

Welcome to the world of Undefined Behavior. When you do this, anything can happen. Your program may crash, your computer may crash, your computer may explode, demons may fly from your nose .

And worst of all, your program can work just fine, imperceptibly looking as if it is working correctly, until one fine day it begins to spill garbage, because it is rewriting confidential data somewhere because somewhere someone allocated too few characters for their arrays, and now you messed up the heap and you get segfault at some point for a million miles, or worse, your program will happily combine with the damaged heap and your functions will work on the damaged numbers credit cards and you by radiate huge misfortune.

Even if it looks like it works, it is not. This is Undefined Behavior. Avoid this, because you can never be sure what he will do, and even when what he does, when you try, everything is in order, on another platform it may not be so.

+7
source

The best description I read (was on stackoverflow) and went like this:

If the speed limit is 50 and you drive 60. You may not get lucky and get a ticket, but one fine day, maybe not today, maybe not tomorrow, but one fine day this policeman will be waiting for you. On this day you will pay, and you will pay dearly.

If someone can find the original, I would rather mention that they were much more eloquent than my explanations.

+3
source

strcpy will copy the zero end of char, as well as all other characters.

So, you copy the length of hello + 1, which is 6, to the size of the buffer, which is 5.

You have a buffer overflow here, and to reload a memory that is not yours, the results will be undefined.

+2
source

Alternatively, you can also use dest = strdup (src), which will allocate enough memory for line + 1 for null terminator (+1 for Giuliano's answer).

+1
source

This is why you should always run valgrind in any C program that seems to work.

+1
source

Yes, everyone covered the main point; you cannot fail. The fact is that the zero limiter is usually 0, and 0 is a fairly common value for sitting at any particular memory address. So it just works. You can test this by taking a memory set, laying a bunch of garbage on it, and then write this line and try to work with it.

In any case, the main problem I see here is that you are talking about C, but you have this line of code:

 char* dest = new char[strlen(src)]; 

This will not compile in any standard C compiler. There is no new keyword in C. This is C ++. In C, you should use one of the memory allocation functions, usually malloc . I know this is like nitpicy, but actually it is not.

+1
source

All Articles