mingw32 compiled program segfaults

This is a discussion on mingw32 compiled program segfaults within the C Programming forums, part of the General Programming Boards category; hi, Here is my code:- Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int info; struct node *next,*prev; }; typedef struct ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40

    mingw32 compiled program segfaults

    hi,

    Here is my code:-

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    struct node
    {
    	int info;
    	struct node *next,*prev;
    };
    
    typedef struct node *nodeptr;
    
    nodeptr getnode(void);
    nodeptr create_list(nodeptr);
    void display_list(nodeptr);
    nodeptr sort_list(nodeptr);
    
    int main(void)
    {
    	nodeptr head=NULL;
    	
    	head=create_list(head);
    	display_list(head);
    	getch();
    
    	head=sort_list(head);
    	display_list(head);
    	
    	getch();
    	
    	return 0;
    	
    }
    
    nodeptr getnode(void)
    {
    	nodeptr p;
    	p=(nodeptr)malloc(sizeof(nodeptr));
    	printf("\n#:");
    	scanf("%d",&p->info);
    	
    	p->next=NULL;
    	p->prev=NULL;	
    	return p;
    }
    
    nodeptr create_list(nodeptr head)
    {
    	int nodes=0;
    	nodeptr p,curr;
    
    	printf("\nEnter how many nodes do you desire in the list:");
    	scanf("%d",&nodes);
    
    	for(;nodes>0;nodes--)
    	{
    		p=getnode();
    		if(!head)
    		{
    			p->next=head;
    			head=p;
    		}
    		else
    		{
    			for(curr=head;curr->next!=NULL;curr=curr->next)
    			;
    			p->prev=curr;
    			curr->next=p;
    
    		} 
    
    	}
    	return head;
    
    }
    
    nodeptr sort_list(nodeptr head)
    {
    
    	nodeptr p,q;
    	int swap=0;
    	
    	
    
    	for(p=head;p!=NULL;p=p->next)
    	{
    		for(q=head;q!=NULL;q=q->next)
    		{
    			if(p->info < q->info)
    			{
    				swap=p->info;
    				p->info=q->info;
    				q->info=swap;
    			}
    		}
    	}
    	return head;
    }
    
    	
    	
    
    void display_list(nodeptr head)
    {
    	nodeptr curr;
    	
    	for(curr=head;curr!=NULL;curr=curr->next)
    		printf("\n%d",curr->info);
    	
    }
    I know it a bit long but since I dont know the problem area , I had to post the entire program.

    The above program is a simple doubly linked list which gets sorted and then is printed on the screen.

    When I compile the above code in MS VC++ 6.0 and in Turbo C++ 3.0, it runs perfectly, but when i use Dev-Cpp(4.9.9.2) which inherently uses Mingw32(3.4.2), my program accepts only 3 numbers in the list!. This is a very strange bug, if i try to accept more than 3 nodes, a segmentation fault occurs and the program crashes.

    Dev-Cpp/Mingw32 users kindly help me cause i dont know wheather my code is at fault or if i am not compiling correctly or what the problem is.

    By the way, I am using Windows XP SP1.

    Thanks,
    Sahil

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    	p=(nodeptr)malloc(sizeof(nodeptr));
    has to be
    Code:
    	p=(nodeptr)malloc(sizeof(struct node));
    That it runs on some compilers is just good luck ( runs on mine as well ).
    Kurt

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    > p=(nodeptr)malloc(sizeof(nodeptr));
    And drop the cast as well, this is C

    Also, write it like this
    p = malloc( sizeof(*p) );
    and the whole problem of figuring out the type goes away (the compiler does it for you)
    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
    Aug 2005
    Location
    New Delhi
    Posts
    40

    thank you

    Thanks, I understood my mistake.



    Sahil

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40

    i have another doubt

    Code:
    head=create_list(head);
    i use this statement whenever I initialize my list, why doesnt this work:

    Code:
    create_list(head);
    I tried writing this and watching how head gets initialized, In the first statement head gets properly initialized and it's value is returned back to main as expected.

    In the second statement, head again gets properly intialized in create_list, but when control is returned to main, head gets *reintialized* to NULL, why?

    isnt head a pointer, which should get automatically passed by reference? what am I missing out over here?

    Thanks,
    Sahil

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sahil_m
    isnt head a pointer, which should get automatically passed by reference?
    if you use the statement
    Code:
    create_list(head);
    a copy of head is passed to create_list. If you change the location of to where head points you change the copy. In main you still have head point to wherever it pointed before the call to create_list().
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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