Singly linked lists tutorial, problem with malloc

This is a discussion on Singly linked lists tutorial, problem with malloc within the C Programming forums, part of the General Programming Boards category; I'm having a problem with the tutorial on linked lists ( Cprogramming.com Tutorial: Linked Lists ). When I try to ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Question Singly linked lists tutorial, problem with malloc

    I'm having a problem with the tutorial on linked lists (Cprogramming.com Tutorial: Linked Lists).
    When I try to build this code, I come up with an error.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
      int x;
      struct node *next;
    };
    
    int main()
    {
        /* This won't change, or we would lose the list in memory */
        struct node *root;       
        /* This will point to each node as it traverses the list */
        struct node *conductor;  
    
        root = malloc( sizeof(struct node) );  
        root->next = 0;   
        root->x = 12;
        conductor = root; 
        if ( conductor != 0 ) {
            while ( conductor->next != 0)
            {
                conductor = conductor->next;
            }
        }
        /* Creates a node at the end of the list */
        conductor->next = malloc( sizeof(struct node) );  
    
        conductor = conductor->next; 
    
        if ( conductor == 0 )
        {
            printf( "Out of memory" );
            return 0;
        }
        /* initialize the new memory */
        conductor->next = 0;         
        conductor->x = 42;
    
        return 0;
    }
    The error is in lines 16 and 27, whenever malloc is used:
    invalid conversion from `void*' to `node*'

    I figured out that I could fix it by sticking (node*) in front of malloc. So my question is, should I have to do this? Is there something wrong with the tutorial or something wrong with the compiler? I'm using codeblocks 8.02 in windows xp with MinGW 5.1.4 (I'm using the latest stable versions of codeblocks and mingw)

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you're compiling in C++ mode. use .c, not .cpp or .c++, and use gcc, not g++.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > The error is in lines 16 and 27, whenever malloc is used:
    > invalid conversion from `void*' to `node*'
    It means you're using a C++ compiler to compile your C code.

    Name your source files so they end with .c, and make sure you're using gcc rather than g++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    My file does end in .c, and I'm using the GNU GCC compiler.
    I uninstalled MinGW and then reinstalled so that the g++ compiler option was unchecked. It turns out that codeblocks is still trying to compile using g++, because now it says "execution of 'ming32-g++.exe' has failed."
    I clicked on Settings->Compiler and debugger... then clicked on the "toolchain executables" tab. It does detect that there is a gcc compiler (ming32-gcc.exe) but I can't figure out how to get it to use that instead.
    I realise that this question should probably be posted on the Code Blocks forum, but if anyone here knows the solution that would be great.
    Attached Images Attached Images  

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Protolocke View Post
    I figured out that I could fix it by sticking (node*) in front of malloc. So my question is, should I have to do this? Is there something wrong with the tutorial or something wrong with the compiler? I'm using codeblocks 8.02 in windows xp with MinGW 5.1.4 (I'm using the latest stable versions of codeblocks and mingw)
    Yes you would have to change that. The reason being malloc is defined to return void* by default. So what you should be doing is that instead of this

    root = malloc( sizeof(struct node) );

    you should be doing this

    root = (struct node *) malloc( sizeof(struct node) );

    Now you are telling it to typecast it to the desired data type. And that should solve the problem.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > root = (struct node *) malloc( sizeof(struct node) );
    > Now you are telling it to typecast it to the desired data type. And that should solve the problem.
    Read the FAQ.
    This is an unnecessary bodge covering up deeper problems in the build setup.

    @OP
    What about compiler settings, and project settings?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Oops.....my fault. Thanks Salem for pointing that out. But till now i had always been casting malloc thinking it was mandatory and now i know its not.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    There's nothing in the compiler settings that will help, and I can't find project settings. Thanks for the help though guys

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by roaan View Post
    Oops.....my fault. Thanks Salem for pointing that out. But till now i had always been casting malloc thinking it was mandatory and now i know its not.
    Before ANSI C introduced the void * generic pointer, these casts were required because older compilers used to return a char pointer. But under new standards these casts are no longer necessary as a void pointer can be assigned to any pointer. These casts are still required in C++ however.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Has anyone solved 'Protolocke' original question. I have exactly the same problem and same versions. The problem is that CodeBlock uses Cpp.exe or G++.exe and not gcc.exe, this can be proved by just firing up a Command prompt and issuing

    gcc -c filenam.c, which provides a clean compile, whereas the CodeBlock GUI invokes C++ and produces a raft of irrelevant syntax errors. Any help would be appreciated.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm currently working with ver. 8.02 and don't have this problem -- it uses the program files you would expect, mostly -- a sample log looks like:
    Code:
    mingw32-gcc.exe -Wall    -c H:\temp.c -o H:\temp.o
    mingw32-g++.exe  -o H:\temp.exe H:\temp.o   
    Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 0 warnings
    ISTR an earlier version not obeying the settings and always using C++, but I don't have a way to check that right this minute.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    Thanks, I suppose if I could see which script it was invoking it would help track down the problem. In my log I just see

    Compiling: ..\FileName.C
    error messages.....etc

    Perhaps I need to specify the scripts for the compiler to use.


    Quote Originally Posted by tabstop View Post
    I'm currently working with ver. 8.02 and don't have this problem -- it uses the program files you would expect, mostly -- a sample log looks like:
    Code:
    mingw32-gcc.exe -Wall    -c H:\temp.c -o H:\temp.o
    mingw32-g++.exe  -o H:\temp.exe H:\temp.o   
    Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 0 warnings
    ISTR an earlier version not obeying the settings and always using C++, but I don't have a way to check that right this minute.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Under "compiler and debugger settings", the last tab on that screen is "Other settings" -- the first choice on that list is "Compiler logging", and you can change that option to have it give you the actual command line being run.

    Now the version of code::blocks I have at home has this issue, although the one at school does not. I don't see the difference between the options at the moment, but I'll keep looking.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Code Blocks Supporting C

    Thanks very much. At least I know now I haven't gone mad ;-)

    Btw, I have even tried renaming the G++, CPP files to no avail, and the fault appears to be also common in another IDE (BloodShed) in exhibiting the same problem.

    I have tried every possible option, even submitting my own command strings, but still the problem persists - I have a suspision that it could be related to other problems related to the 'stickyness' of some of the option dialogues. BTW, I have been able to compile under MSFT VS 2008, so a further proof point that there is something screwy in Code::Blocks.


    Quote Originally Posted by tabstop View Post
    Under "compiler and debugger settings", the last tab on that screen is "Other settings" -- the first choice on that list is "Compiler logging", and you can change that option to have it give you the actual command line being run.

    Now the version of code::blocks I have at home has this issue, although the one at school does not. I don't see the difference between the options at the moment, but I'll keep looking.
    Last edited by adel_acameron; 04-24-2010 at 09:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  2. Linked Lists tutorial
    By BunkFace in forum C Programming
    Replies: 2
    Last Post: 01-04-2006, 02:59 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Problem with pointers and linked lists
    By bsimbeck in forum C Programming
    Replies: 2
    Last Post: 02-21-2003, 10:05 AM
  5. A problem with my linked lists test.
    By valar_king in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:03 PM

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