C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-05-2009, 07:05 AM   #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)
Protolocke is offline   Reply With Quote
Old 09-05-2009, 07:16 AM   #2
Registered User
 
Join Date: Oct 2001
Posts: 2,110
you're compiling in C++ mode. use .c, not .cpp or .c++, and use gcc, not g++.
robwhit is offline   Reply With Quote
Old 09-05-2009, 07:17 AM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-05-2009, 08:03 AM   #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
 
Protolocke is offline   Reply With Quote
Old 09-05-2009, 08:15 AM   #5
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
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.
roaan is offline   Reply With Quote
Old 09-05-2009, 08:25 AM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 09-05-2009, 08:35 AM   #7
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 300
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.
roaan is offline   Reply With Quote
Old 09-05-2009, 09:55 AM   #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
Protolocke is offline   Reply With Quote
Old 09-05-2009, 09:49 PM   #9
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
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.......

for( ; ; )
printf("If you can't make it good, at least make it look good");

PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
BEN10 is offline   Reply With Quote
Reply

Tags
casting, conversion, linked lists, malloc, tutorial

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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