Thread: Most Common problems in C

  1. #16
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    For casting return value of malloc(), it's not necessary.
    There's no point to cast unless you are going to use C++ compiler.
    But, I'd prefer
    Code:
           foo_t *p;      
          p = malloc( sizeof(foo_t) );
           // prefer
           p = malloc( sizeof(*p) );
    
          #define NELEM(array)       ( sizeof(array) / sizeof( foo_t) )
          // prefer
          #define NELEM(array)       ( sizeof(array) / sizeof( (array)[0]) )
    Last edited by Bayint Naung; 06-02-2010 at 08:19 PM. Reason: Oh! wrong order!!!

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bayint Naung View Post
    But, I'd prefer
    Code:
           foo_t *p;
           p = malloc( sizeof(*p) );
           // prefer
           p = malloc( sizeof(foo_t) ); /* 1 */
    
          #define NELEM(array)       ( sizeof(array) / sizeof( foo_t) )
          // prefer
          #define NELEM(array)       ( sizeof(array) / sizeof( (array)[0]) ) /* 2 */
    1. While it may be your preference, the former method is less accident prone, and is easier to maintain (ie: you don't have to ever change it). If you go back and substitute foo_t for bar_t, you have to chase down all of your malloc calls, and change them too. If you use the first method, you don't have to.

    2. Neither of those methods work for arrays declared outside the current scope.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by claudiu View Post
    Well, fueled by MK27's relentless optimism I decided it's time to drop the skepticism and add my own thing here. One thing I see here everyday and is driving me nutz, although not a mistake per say is the casting of malloc().

    Another thing, which is not necessarily related to C is the confusion between an IDE and a compiler. Most people will come and say... "ohh I compilez my codez with Eclipse and needz helpz cuz deadline iz tomorrow!".
    I still remember one post.
    " I compile this codel from online tutorial and my compiler gives error.
    1.
    Code:
    for(int i = 0; i < n; i++) {        
    ...
    }
    The problem is most newbies don't know it's only supported in C99.
    The worst thing is most will manage to write code that will compile.
    2.
    Code:
    if( 0 < n < 10 ) {
    But I find that gcc (if enable warning) will give warning.
    1. compile with gcc -Wall foo.c
    bar.c:100: error: ‘for’ loop initial declarations are only allowed in C99 mode
    bar.c:100: note: use option -std=c99 or -std=gnu99 to compile your code

    2. gcc -Wall foo.c
    bar.c:106: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning

  4. #19
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    1. While it may be your preference, the former method is less accident prone, and is easier to maintain (ie: you don't have to ever change it). If you go back and substitute foo_t for bar_t, you have to chase down all of your malloc calls, and change them too. If you use the first method, you don't have to.

    2. Neither of those methods work for arrays declared outside the current scope.
    1. it's a misteaks!
    2. YES

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. common functions
    By FoodDude in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 02:13 PM
  3. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  4. Lowest Common Factor
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2002, 09:30 AM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM