Question

I have been using C for quite sometime, and I have this trivial problem that I want to query about.

Say i want to create a character array that stores upto 1000 characters. Now, when I am using malloc for the same, then do I specify the size of array as 1001 character [ 1000 characters + null] or just 1000?

Also, say I came across this problem, then how could I have found the answer to this solution on my own, maybe by using some test programs. I understand the size of string is calculated without the null character, but when I am allocating the memory for the same, do I take into account the null character too?

Était-ce utile?

La solution

If you need that block for storing null-terminated string then yes, you need to explictly ask malloc() to allocate an extra byte for storing the null-terminator, malloc() will not do it for you otherwise. If you intend to store the string length somewhere else and so you don't need the null terminator you can get away without allocating the extra byte. Of course it's up to you whether you need null-termination for strings, just don't forget that C library string handling functions only work with null-terminated strings.

Autres conseils

malloc and family allocate memory in chunks of bytes. So if you do malloc(1000) you get 1000 bytes. malloc will not care if you allocated those 1000 bytes to hold a string or any other data type.

Since strings in C consist of one byte per character and ideally have to be null terminated you need to make sure you have enough memory to hold that. So the answer is: Yes, you need to allocate 1001 bytes if you wish to hold a string of 1000 characters plus null terminator.

Advanced tip: Also keep in mind that depending on how you use it you may or may not need to null terminate a string.

If you for instance know the exact length of your string you can specify that when using it with printf

printf("%*s", length, string);

will print exactly length characters from the buffer pointed at string.

It's up to you to provide the null-terminating character.

malloc allocates memory for you but it doesn't set it to anything.

If you strcpy to the allocated memory then you will have a null-terminator provided for you.

Alternatively, use calloc as it will set all elements to 0, which is in effect the null-terminator. Then if you do, say, memcpy, you wouldn't have to worry about terminating the string properly.

You do indeed need to allocate the memory for the null terminator.

Conceptually the null terminator is just a convenient way of marking the end of a string. The C standard library exploits this convention when modelling a string. For example, strlen computes the length of a string by examining the memory from the input location (probably a char*) until it reaches a null terminator; but the null terminator itself is not included in the length. But it's still part of the memory consumed by the string.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top