Reputation: 45
i am learning c/c++ recently.but i don't understand the difference between
int a(chat* str,int len)
{
cout<<str<<len;
}
and
int a(char* str)
{
cout<<str<<strlen(str);
}
Upvotes: 1
Views: 661
Reputation: 657
Above answers are to the point. So I'm going to discuss other perspective behind of practise of passing length along with char *
.
As others said, not always, the string pointed by char *
end up with \0
. Only when the string ends with \0
strlen()
would actually work. There are certain use-cases for example binary coding, where data is represented as string. In such case, char *
would not end with \0
. Besides, there can be certain use-cases to read / write only up to certain length / size. In such case, it is always necessary to test whether the input length is within the range of length of total string. So as a common case, length has been passed explicitly, which can be used in any way as desired by the caller.
Upvotes: 1
Reputation: 56547
When you pass char*
without a length, how would you know how many elements to process? char*
means a pointer to a character. When you pass a pointer, you have no idea (and cannot find out) how much memory (if any) was allocated for the pointer.
That's why C-strings use are null-terminated (they end with a '\0'
character), so you can detect their length by iterating the pointer. Hence, if you want to use a pointer without giving the length of its allocated memory, you need to obey some conventions. But in general, e.g. when passing a buffer, you shouldn't expect any end-signalling character, so in this case you need to pass the length, otherwise may end up reading/writing out of bounds.
For your particular example, you're fine with passing only a pointer provided you use your function only on C-strings, since strlen(str)
uses this convention of counting until encountering a '\0'
.
Buffer overflows are one the most messy and nightmarish programming errors, which can result in serious security issues. That's why you should try (whenever possible) to use std::string
from the C++ standard library instead of C-style char*
strings.
Upvotes: 6
Reputation: 2088
A C-String should always contain a termination character, we call it null character. It's technically 0
(not the number 0
, but ASCII 0
)
When we create a char* and initialize it with some text, it automatically adds the '\0'
to the end.
char* c = "Hello";
This will create an array of char with six elements. Yes, six elements.
c = {'H', 'e', 'l', 'l', 'o', '\0'}
When you print c
, it will search till it finds that '\0'
. What if someone replaces it.
c[5] = '!';
Then the system can't determine the end of the text. Then it will keep on reading the memory (which does not belong to that variable, or may be even the program) until it hits a null
char.
That is the main reason to pass the size (or number or chars to read) to a function.
On the other hand, if you need to read some data from a stream, you can use a buffer. In that case, you should specify how many bytes to read, in that way you will not cause buffer overflows.
Upvotes: 1