Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is malloc doing in this code?

Tags:

c++

c

malloc

Could you explain following code?

str = (char *) malloc (sizeof(char) * (num+1));
  1. What is malloc doing here?
  2. Why is num + 1 used?
like image 528
freddiefujiwara Avatar asked Jul 31 '09 16:07

freddiefujiwara


People also ask

What does the malloc function do?

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.

What is malloc () in C ++?

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.

What does malloc do in Java?

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.

What does malloc stand for?

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.

What does malloc () calloc () realloc () free () do?

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.

What is the malloc function and what does it return?

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.


3 Answers

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++;
}
like image 166
John Calsbeek Avatar answered Oct 23 '22 22:10

John Calsbeek


malloc is for memory allocation. num + 1 is to allow for the null-terminator - \0.

like image 20
Daniel A. White Avatar answered Oct 23 '22 23:10

Daniel A. White


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.

Parsing the Code

The first problem is parsing the code

Welcome into the Twilight Zone

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 !".

Parsing this line of code?

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

The C cast operator is somewhat less than natural

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.

Does C++ have something more understandable?

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))  ) ;

My sizeof is larger than yours

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".

There's Something About Malloc

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.

num + 1?

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.

like image 20
paercebal Avatar answered Oct 23 '22 23:10

paercebal