Thread: Most Common problems in C

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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!".
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's not really an error to cast mallocs return type, it's just unnecessary in C.
    Last edited by Subsonics; 06-02-2010 at 10:05 AM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    claudiu you must'nt have any hair left, because casting malloc seems to be the dominant style in alot of code I, at least, need to analyse.

    Hell, I even do it, so nobody looks askance at my code.

    The confidence to not cast your mallocs, must be the sign of an advanced coder, I reckon.

    Of course people say, "oh, it's just to make my code clearer", oh yeah, right .. the logic being "if I cast my mallocs, I can claim to write clear code, the rest can remain unclear"

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by stabu View Post
    claudiu you must'nt have any hair left, because casting malloc seems to be the dominant style in alot of code I, at least, need to analyse.

    Hell, I even do it, so nobody looks askance at my code.

    The confidence to not cast your mallocs, must be the sign of an advanced coder, I reckon.

    Of course people say, "oh, it's just to make my code clearer", oh yeah, right .. the logic being "if I cast my mallocs, I can claim to write clear code, the rest can remain unclear"
    As the poster above mentioned casting malloc in C is not a problem of confidence. It is totally useless. In fact, it can even prove dangerous if you don't include stdlib because then the function will be assumed to return an int. If ints and pointers are of different sizes on your machine, you may be in for a run-time surprise. The code you are looking at is probably compiled with a C++ compiler. I feel like most people compiling with gcc or other C compilers cast malloc BECAUSE they don't really understand the reason why they don't need to cast it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    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

  6. #6
    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