Could you explain following code?
str = (char *) malloc (sizeof(char) * (num+1));
malloc
doing here? num + 1
used?malloc() is a library function that allows C to allocate memory dynamically from the heap. The heap is an area of memory where something is stored. malloc() is part of stdlib. h and to be able to use it you need to use #include <stdlib.
The function malloc() in C++ is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. A malloc() in C++ is a function that allocates memory at the runtime, hence, malloc() is a dynamic memory allocation technique. It returns a null pointer if fails.
malloc() allocates a memory block of given size (in bytes) and returns a pointer to the beginning of the block. malloc() doesn't initialize the allocated memory.
The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.
Functions malloc, calloc, realloc and free are used to allocate /deallocate memory on heap in C/C++ language. These functions should be used with great caution to avoid memory leaks and dangling pointers.
The malloc() function returns a pointer to the reserved space. The storage space to which the return value points is suitably aligned for storage of any type of object. The return value is NULL if not enough storage is available, or if size was specified as zero.
malloc
is a function that allocates a chunk of memory on the heap and returns a pointer to it. It's similar to the new
operator in many languages. In this case, it's creating a block of memory that can survive for an arbitrary length of time and have an arbitrary size. That in itself is fairly in-depth stuff, which is somewhat difficult to explain and warrants a separate question.
The num + 1
compensates for the null terminator on the end of strings. Strings often need to know how long they are, and the C tradition is to allocate room for an additional character on the end of the string, which will always be the special character \0
. That allows functions that deal with the string to determine the size of the string automatically. For example, if you wanted to do something to every character of a string without knowing how long the string is, you could do something like this:
const char *ptr = str;
while (*ptr != '\0') {
process(*ptr);
ptr++;
}
malloc
is for memory allocation.
num + 1
is to allow for the null-terminator - \0
.
Preamble: I can't believe it! I was baffled by this kind of expression when I was taught C basics (no pun intended). This is why I go into extreme detail in the "parsing the code" section.
The first problem is parsing the code
str = (char *) malloc (sizeof(char) * (num+1));
When working with C/C++, parsing this kind of expression is mandatory, so we will break it down into its components. The first thing we see here is something like:
variable = (expression) function (expression) ;
The first time I saw it, I was just "Hey, I can't believe there is a programming language where you can call a function by putting its parameters both at the left and the right of the function call !".
In truth, this line should be read like:
variable = function_a (function_b (expression)) ;
where :
expression is sizeof(char) * (num+1)
function_b is malloc
function_a is a cast operator
As already explained elsewhere, the C-style cast operator is more like
(function_a) expression
than the more natural
function_a(expression)
Which explains the strangeness of the whole line of code.
Note that in C++, you can use both notations, but you should instead use the static_cast, const_cast, reinterpret_cast or dynamic_cast instead of the above notations. Using a C++ cast, the above line of code would be:
str = static_cast<char *> ( malloc (sizeof(char) * (num+1)) ) ;
sizeof is an operator. You can think it like a function working on types. You pass a type as a parameter, and it will give you its size in bytes.
So, if you write:
size_t i = sizeof(char) ;
size_t j = sizeof(int) ;
You'll probably have (on a 32-bits Linux) a value of 1 for i, and 4 for j. Its use in malloc is like saying "I want enough room to put 25 cars of 4 meters long" instead of "I want at least 100 meters".
Malloc's parameter is a size_t, that is, an unsigned integer. You give it the size in bytes, and if successful, it returns you the address of allocated memory large enough for you to use as an array. For example:
int * p = (int *) malloc (25 * sizeof(int)) ;
Then p points to a memory where you can put 25 integers side by side, as if inside an array whose indices go from zero to the size minux one. For example:
p[0] = 42 ; // Ok, because it's the 1st item of the array
p[24] = 42 ; // Ok, because it's the 25th item of the array
p[25] = 42 ; // CORRUPTION ERROR, because you are trying to
// use the 26th item of a 25 items array !
Note: You have pointer arithmetics, too, but this goes beyond the scope of the question.
C-style strings are somewhat different from other languages strings. Each character of a string can be of any value BUT NOT ZERO. Because zero (also noted \0) marks the end of a c string.
Put it another way: You never know the size of a c-string, but by searching the \0 character, you can know where it ends (which is one reasons of buffer overflows and stack corruption, by the way).
For example, the string "Hello" seems to have 5 characters:
"Hello" seems to be an array containing 'H', 'e', 'l', 'l' and 'o'.
But in truth, it has 6 characters, the last one being the character ZERO, which is noted using the escape character \0. Thus:
"Hello" is an array containing 'H', 'e', 'l', 'l', 'o' and 0.
This explains that when you want to allocate enough room for a string of "num" characters, you allocate instead "num + 1" characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With