Like Tree2Likes
  • 1 Post By ledow
  • 1 Post By Malcolm McLean

Algorithms with C

This is a discussion on Algorithms with C within the C Programming forums, part of the General Programming Boards category; Hello! I recently bought the book "Algorithms with C". At the very beginning of the book, I find the following ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11

    Algorithms with C

    Hello!

    I recently bought the book "Algorithms with C". At the very beginning of the book, I find the following code example:

    Code:
    #include <stdio.h>
    
    int 
    g(int **iptr) 
    {
        if ((*iptr = (int *)malloc(sizeof(int))) == NULL)
            return -1;
    
        return 0;
    }
    I read somewhere that casting malloc is generally a very bad idea. Is this an exception to the rule? I won't invest time in this book if it turns out that it starts with a bad mistake.

    Thank you for any input!

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There is no special reason to cast the return of malloc in this case. But I wouldn't take the cast that serious to dump the book because of it.
    Kurt

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Casting malloc is not the end of the world, even if it's not "ideal" practice. It just leaves you open to a particular problem if you later go back and change the type of a variable without also catching the casts around mallocs associated with it (but don't forget that you'll have to change the malloc as well, most likely). It's just a way that could possibly mask a compiler warning that you would get if it wasn't there. It's not going to kill you, just give you a problem for a novice who isn't watching their variables types carefully or just search-and-replacing blindly might miss (and then wonder why it doesn't work).

    Lots of people do it, and lots of books teach it, but that's true of a lot of things that aren't "ideal". And, to be honest, if you're doing any kind of debugging or memory-watching whatsoever, it shouldn't be an issue and there'll be a lot worse you could pick up on.

    The trick to learning anything is not to follow books blindly, but use them as a reference. It's more powerful to use a learning resource and know its weaknesses than blindly trust it, or abandon anything that has even the tiniest imperfection. Look at it this way: You spotted it. That's a step along the path greater than the book alone could ever allow. But it doesn't mean that you'll spot everything, or that the book won't be right in everything else.
    AndiPersti likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    For reference:

    FAQ > Casting malloc - Cprogramming.com

    (And I find the "stdlib.h not incuded" explanation a bit dubious - if that's the case, you have a lot more serious problems than a miscast malloc!).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by ledow
    (And I find the "stdlib.h not incuded" explanation a bit dubious - if that's the case, you have a lot more serious problems than a miscast malloc!).
    The problem in that case is not a miscast malloc (for indeed the cast could be absolutely correct, technically speaking), but the fact that the cast would then hide a likely warning that would alert you to the fact that <stdlib.h> was not included. That is explained in the same article.

    The thing here is that ditching a book on C purely because of this stylistic issue would be ridiculous since it is possible for every use of such a cast to be correct, with <stdlib.h> appropriately included. This is even more ridiculous when the focus of the book is on algorithms in C, not just programming in C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    331
    Casting malloc() is a bad idea, but not a very bad idea.
    The reason for doing it is to make the code compile under C++, and sometimes it is convenient to be able to snip and paste code portions between files. Generally, however, you want to keep functions in their own files.
    Even if you don't use C++, the cast does little harm, except to hide a warning which you'll probably pick up elsewhere. But it's better not to use it.
    jamesedmonds likes this.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Norway
    Posts
    11
    Ah! Thanks a lot guys!

    I've been warned against so many dangerous C resources out there and it has made me very skeptical.
    I will continue working with the book then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithms
    By ElastoManiac in forum C++ Programming
    Replies: 7
    Last Post: 04-28-2006, 04:45 AM
  2. Algorithms
    By valar_king in forum C++ Programming
    Replies: 5
    Last Post: 11-17-2002, 03:24 PM
  3. STL algorithms
    By PJYelton in forum C++ Programming
    Replies: 7
    Last Post: 11-12-2002, 02:27 PM
  4. C Algorithms
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 09-28-2001, 02:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21