C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 30th, 2012, 09:18 PM
Apoorv1234 Apoorv1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Apoorv1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
No output showing in Dev C++ compiler..help please

Hi All,
I'm entering a very basic code of appending nodes in a link list in ascending order. But the compiler shows a black screen as output.
Please Help. Here's the code...



#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node{
int data;
struct node *link;
};

void addinasc(struct node **, int);
void display(struct node *);
void count(struct node *);

int main()
{
struct node *p;
p=NULL;

addinasc(&p , 10);
addinasc(&p , 7);
addinasc(&p , 74);
addinasc(&p , 91);
addinasc(&p , 56);
addinasc(&p , 24);
addinasc(&p , 35);
addinasc(&p , -5);

count(p);
display(p);

/*reverse(&p);

display(p);*/
getch();
return 0;
}

void addinasc(struct node **q, int num)
{
struct node *temp , *r;
temp = *q;

r=(struct node*)malloc(sizeof(struct node));
r->data = num;

if(*q==NULL || (*q)->data>num)
{
r->link = *q;
*q = r;
}
else
{
while(temp!=NULL)
if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
{
r->link = temp->link;
temp->link = r;
return;
}
temp = temp->link;
}
}

void count(struct node *q)
{
int count=0;
while(q!=NULL)
{
count++;
q=q->link;
}
printf("\n\nNo. of elements in the linked list : %d\n" , count);
}

void display(struct node *q)
{
while(q!=NULL)
{
printf("\t%d", q->data);
q=q->link;
}
}

Reply With Quote
  #2  
Old August 31st, 2012, 02:06 AM
gurukrupa_1989 gurukrupa_1989 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 25 gurukrupa_1989 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 57 m 16 sec
Reputation Power: 0
Had you given the input for the function, to display the link list ? check for that

Reply With Quote
  #3  
Old August 31st, 2012, 09:44 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
You've discovered the infinite loop!

I amended your addinasc function to display a message every time it's called:
Code:
void addinasc(struct node **q, int num) {
  struct node *temp , *r;
  puts("addinasc"); /*THIS LINE INSERTED **************/
  temp = *q;


And when I ran the program I got this output, which I had to kill because it runs forever:

$ a=./c && make -k $a && $a
make: `c' is up to date.
addinasc
addinasc
addinasc


I'd say that
addinasc(&p , 74);
failed. Look there.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #4  
Old September 1st, 2012, 02:13 AM
Apoorv1234 Apoorv1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Apoorv1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
Corrected Code

Again i forgot to enclose the code within while loop in the { }.
Anyways, I have done that now and I am posting the code again.
Please help . Cant understand why no output is showing.

Again, I'm using Dev C++ IDE.


Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node{
       int data;
       struct node *link;
       };
       
void addinasc(struct node **, int);
void display(struct node *);
void count(struct node *);
       
int main()
{
    struct node *p;
    p=NULL;
    
    addinasc(&p,10);       
    addinasc(&p,7); 
    addinasc(&p,74);
    addinasc(&p,91);
    addinasc(&p,56);
    addinasc(&p,24);
    addinasc(&p,35);
    addinasc(&p,-5);    
    
    count(p);
    display(p);
    
    /*reverse(&p); 
    
    display(p);*/
    getch();
    return 0;
}

void addinasc(struct node **q, int num)
{
     struct node *temp , *r;
     temp = *q;
     
     r=(struct node*)malloc(sizeof(struct node));
     r->data = num;
     
     if(*q==NULL || (*q)->data>num)
     {
                 r->link = *q;
                 *q = r;
     }
     else
     {
         while(temp!=NULL)
         {
                if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
                {
                           r->link = temp->link;
                           temp->link = r;
                           return;
                }
                temp = temp->link;
         }
     }
}

void count(struct node *q)
{
    int count=0;
    while(q!=NULL)
    {
                   count++;
                   q=q->link;
    }
    printf("\n\nNo. of elements in the linked list : %d\n" , count);             
}

void display(struct node *q)
{
     while(q!=NULL)
     {
                   printf("\t%d", q->data);
                   q=q->link;
     }
}

Reply With Quote
  #5  
Old September 1st, 2012, 02:57 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 49 m 18 sec
Reputation Power: 1974
Er, b49P23TIvg informed you that you were trapped within an infinite loop. What do you not understand about infinite loop?

In my undergraduate days, we were on an IBM/370 system that emulated the IBM/360 OS. The vast majority of error messages we received told us almost absolutely nothing useful ("Probable Cause: Programmer error." "Corrective Action: Correct the programming error and resubmit". Except for the ones that told us that our time allocation had timed out, which told us that we had been trapped within an infinite loop.

An infinite loop is one in which there is no escape. For example, the code
Code:
  while (1)
    { }

would be an infinite loop. You would be forever trapped within the loop, unless some kind of condition allowed you to jump out of it (eg, something to trigger a break). As long as you're trapped within an infinite loop, there is no way out and you will remain there forever. Which means that any code
that lies after that infinite loop will never ever execute.

Again, b49P23TIvg demonstrated that your code, addinasc in particular, is trapped within an infinite loop. You need to correct that.

Reply With Quote
  #6  
Old September 1st, 2012, 03:07 AM
Apoorv1234 Apoorv1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Apoorv1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
I do know what an infinite loop is.
But what i'm not able to fathom is how and where i have entered that infinite loop. Infact i've cross checked the code from a book also and it's also pretty similar.

Please post a solution to the problem(probably some corrected lines of code) if possible. Would help a lot

Reply With Quote
  #7  
Old September 1st, 2012, 03:12 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 14 sec
Reputation Power: 1800
I would suggest that the simplest way to find out what is happening is to run and step the code in teh debugger. However Dev-C++'s debugger sucks to the point of uselessness, so I strongly suggest that you abandon it - it has not been maintained since 2005 and is distributed with an equally old version of MinGW/GCC. Use VC++ 2010 Express Edition instead. Free, more fully featured, and has a best-in-class debugger.

Your code compiles without complaint in VC++ 2010 with one minor change; replace getch() with _getch().

When run it crashes, but the debugger helpfully stops and shows you exactly where it has crashed. At this line
Code:
if(temp->data<=num && (temp->link->data > num || temp->link == NULL))
in addinasc().. At the call addinasc(&p, 74), the value of temp->link at the point it crashes is NULL - you are dereferencing a NULL pointer. You need to figure out either why it is NULL if it shouldn't be or if NULL is acceptable, you need to add a check for it before attempting to dereference it.

Really - use a tool with a working debugger and use it!. The analysis above took me no longer than the time it took to compile and run the code. Debugging by forum is impractical and a waste of everyone's time including yours. I only took the time here to illustrate how effective a debugger can be in the hope that you would see the light.

Last edited by clifford : September 1st, 2012 at 03:14 AM.

Reply With Quote
  #8  
Old September 1st, 2012, 03:21 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 14 sec
Reputation Power: 1800
Looking at that line again, I see that you do check for temp->link == NULL, but only after you have attempted to inspect temp->link->data, which will fail if link is NULL.

If you swap the test as follows:
Code:
if( temp->data <= num && (temp->link == NULL || temp->link->data > num ) )


The short-circuit evaluation of || will prevent the dereferencing of the NULL pointer. That is to say when temp->link == NULL is true, the evaluation of || stops without evaluating the right-hand side expression, because the left-hand side is already true therefore the whole expression is known to be true already.

Last edited by clifford : September 1st, 2012 at 03:24 AM.

Reply With Quote
  #9  
Old September 1st, 2012, 03:29 AM
Apoorv1234 Apoorv1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 9 Apoorv1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 54 sec
Reputation Power: 0
Problem Solved

Thanks a lot Clifford..
The problem is resolved and I got to learn a new thing.
Thanks so much

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > No output showing in Dev C++ compiler..help please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap