What is malloc doing in this code?

后端 未结 11 882
眼角桃花
眼角桃花 2021-01-01 04:59

Could you explain following code?

str = (char *) malloc (sizeof(char) * (num+1));
  1. What is malloc doing here?
相关标签:
11条回答
  • 2021-01-01 05:14

    Malloc is a call to allocate memory.

    The above code is going to allocate space for num + 1 characters. Likely there is a string with num characters in, and the author of the code has added space for the null terminator.

    After the call str will point to the start of that block of memory which has been allocated.

    0 讨论(0)
  • 2021-01-01 05:17
    sizeof(char)
    

    is safe. One shouldn't assume a single byte per character.

    My question is what are you doing programming if you don't know what malloc does?

    man malloc
    

    on a Linux system. On Windows. who knows? Probably 17 mouse clicks.

    0 讨论(0)
  • 2021-01-01 05:19

    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.

    0 讨论(0)
  • 2021-01-01 05:22

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

    0 讨论(0)
  • 2021-01-01 05:22

    Malloc in this case is allocating num+1 times sizeof(char) bytes. This is standard practice when you want to allocate an array of elements. The char in sizeof(char) is typically replaced with the type of array being allocated.

    Strictly speaking in this example though the sizeof(char) is not necessary. It's guaranteed to be of size 1 by the C standard and hence just multiplying by 1.

    0 讨论(0)
提交回复
热议问题