tags:

views:

109

answers:

6
const char* a;

how do I make sure that string 'a' is null terminated? when a = "abcd" and I do sizeof(a), I get 4. Does that mean its not null-terminated? if it were, I would have gotten 5 ?

+3  A: 

You get 4 because that's the size of a pointer on your system. If you want to get the length of a nul terminated string, you want the strlen function in the C standard library.

jer
`strnlen` for safety.
Nathon
`strnlen` is platform specific, not in the standard.
jer
@Nathon: `strnlen` doesn't really add safety, though, does it? If your function requires a null-terminated string and doesn't get one (or if a function is specified as returning a null-terminated string and doesn't return one), your code is already broken.
James McNellis
@James: There's broken code that crashes, and there's broken code that allows buffer overflows to become remote root exploits. I try to write neither, but prefer to write the former over the latter.
Nathon
+9  A: 

sizeof(a) gives you the size of the pointer, not of the array of characters the pointer points to. It's the same as if you had said sizeof(char*).

You need to use strlen() to compute the length of a null-terminated string (note that the length returned does not include the null terminator, so strlen("abcd") is 4, not 5). Or, you can initialize an array with the string literal:

char a[] = "abcd";
size_t sizeof_a = sizeof(a); // sizeof_a is 5, because 'a' is an array not a pointer

The string literal "abcd" is null terminated; all string literals are null terminated.

James McNellis
The OP was already using `const`, so `const char a[]` is probably a better idea than `char a[]`.
aschepler
To further clarify, `strlen()` will only give the logical legth of data in the char array, not the number of bytes allocated for it. I don't think there is a way to get that number.
T.E.D.
+1  A: 

sizeof(a) is sizeof(const char*), the size of the pointer. It is not affected by the contents of a. For that, you want strlen.

Also, all double-quoted string literals like your "abcd" in source code are automatically null terminated.

aschepler
A: 

sizeof(a) returns the size of the const char *a...not the size of what it is pointing to. You can use strlen(a) to gind the length of the null-terminated string and no, the result of strlen does not include the null-terminator.

semaj
+1  A: 

The problem here is that you are confusing sizeof() which is a compile time operation with the length of a string which is a runtime operation. The reason get 4 back when you run sizeof(a) is that a is a pointer and the typical size of a pointer in C is 4 bytes. In order to get the length of the string use strlen.

For the second question, how to make sure a string is null terminated. The only way to definitively do this is to null terminate the string yourself. Given only a char* there is no way to 100% guarantee it is properly null terminated. Great care must be taken to ensure the the contract between the producer and consumer of the char* is understood as to who terminates the string.

JaredPar
aah....stupid me. But when I do strlen(a) I get 4, and I know a has "abcd" (from gdb).
hari
@hari, 4 is correct because the length of the string "abcd" is indeed 4. However the memory requirements for `a` are `strlen(a)+1` to handle the null terminator. This is one of the parts of C that just takse a bit of getting used to.
JaredPar
That's because the length of "abcd" is 4. strlen does not count the null terminator.
jer
if I am on gdb, how can I make sure that the const char * I am looking at is null-terminated? I am doing: a[0]='a'....a[3]='d' and a[4]='\0'. Does that mean its null-terminated?
hari
A: 

If you are handed a char array which may or may not have null-terminated data in it, there really isn't a good way to check. The best you can do is search for a null character up to a certian specified length (not indefinitely!). But 0 isn't exactly an unusual byte of data to find in an uninitialzed area of memory.

This is one of the many things about C's defacto string standard that many people dislike. Finding the length of a string a client hands you is an O(n) search operation at best, and a segmentation fault at worst.

Another issue of course is that arrays and pointers are interchangable. That means array_name + 2 is the same as &(array_name[2]), and sizeof(array_name) is sizeof(char*), not the length of the array.

T.E.D.