Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I always do this:</p> <pre><code>tok = malloc( sizeof( char ) * ( strlen(tc) + 1 ) ); if( tok == NULL ) { /* Malloc failed, deal with it */ } </code></pre> <p>Some people do <code>tok = (type) malloc( ... )</code> but you should cast the result because apparently it covers up some nasty errors. I will do some research and see if I can find out exactly what they are.</p> <p><strong>Edit:</strong></p> <p>Casting malloc can hide a missing <code>#include &lt;stdlib.h&gt;</code></p> <p>I found this link which contained a very good explanation:</p> <blockquote> <p><a href="http://cboard.cprogramming.com/faq-board/25799-faq-casting-malloc.html" rel="noreferrer">http://cboard.cprogramming.com/faq-board/25799-faq-casting-malloc.html</a></p> <p>"So when you say this (char*)malloc(10)</p> <p>You're saying that you take whatever malloc returns, convert it to a char*, and assign that to the variable in question.</p> <p>This is all well and good if malloc is prototyped properly (by including stdlib.h), where it's defined as returning void*.</p> <p>The problem comes in when you fail to include stdlib.h, and the compiler initially assumes that malloc returns an int. The real problem is, you DONT get any warning from the compiler.</p> <p>You merrily then convert that int to a char* (via the cast). On machines where sizeof(char*) is different from sizeof(int), the code is seriously broken.</p> <p>Now if you just have char *var = malloc( 10 ); And you miss out the include , you will get a warning from the compiler."</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload