|
|
strcpy(str1, str0)
or strncpy() (safer but not exactly what your codedoes)str1 -= 4;
here.
|
|
strcpy(str1, str0) or strncpy() |
Why you are always suggesting functions from the libraries |
1) Access out of bounds here: str1[LEN] access in invalid |
Remove line 19 (out of bounds again) and you will copy whole content of one character array to another |
Line 27: correct, but you loses pointer to beginning of array. |
Line 54: OK |
Whole program: LEAK. You did not delete str1 array. |
Why is it invalid? |
char str1[LEN]
contains LEN characters with valid indexes [0, LEN-1]. Like int x[2] will have 2 integers with indexes 0 and 1.I could also do: str1[LEN] = '\0', it's the same! |
char arr[5]
then arr[5]
is out of boundspointer arithmetic? Never heard about that? |
Line 54: OK ???? |
Do you think that was what I have in mind? |
zwulu wrote: |
---|
Talk about the possible leaks |
extern const char* str2;
contains LEN characters with valid indexes [0, LEN-1]. |
Read before. If you declare char arr[5] then arr[5] is out of bounds |
Which is easy to make mistake with. It is not an actual complain, just note that it is unsafe. |
It is the same as block starting at line 28 |
Yes I do. You have a leak here. |
Just imagine that str2 was declared as extern const char* str2; |
str1[LEN -1] = str0[LEN - 1];
Should we still use extern? |
Really? Didn't see it |
while(*str1) cout << *str1++;
while(*str1) cout <<*str1++;
I am not studying C++, because it's safe! |
|
|
I mean: str1[LEN -1] = str0[LEN - 1]; |
while(*str1) cout << *str1++; while(*str1) cout <<*str1++; |
|
|
Ok, but you still have out of bound access in (1) and in (2) this line is not needed because loop itself handles this case. |
3) undefined behavior: using c, which changes, twice in unsequenced order. correct way: strcpy(str1, str0) or strncpy() (safer but not exactly what your codedoes) Also you are copying c-string which contained inside array, not the whole array (That is not matters in your case) |
|
|
The first thing that happens is assigning the values at position 0 from one array to another and then c increments. |
'\0'
symbol: operator= returns reference to value it had assigned and it checked by loop after assigment takes place. So this means loop will end after '\0'
character was assigned.'\0'
in the middle it would copy whole array anyway.'\0'
character. If there was '\0'
in the middle of the array it would copy only that part leaving everything after unchanged.
|
|
str1[len - 1] = '\0';
|
|
//This copies until the first '\0' character, without including it. //so str1 doesn't have the null terminated character yet, therefore we put it at the end of the array anyway: // At least, in this case, str1 will have just 1 '\0' and no undefined behaviour, that I can see, could happen |
|
|
str1[i] = '\0';
or use one of alternative approaches I posted: either BSD implementation of strcpy (in post directly) or glibc one (one of the links). And use strcpy directly after you done implementing it yourself.
|
|
|
|
but including trailing 0 is not the behavior people expects from length function. |
why do you need to fill all excess memory with 0? |