Program Overflow: Troubleshooting Tips

In summary, the program is not working properly after taking values as there is a possibility of stack overflow due to an infinite recursion loop. This could be caused by a loop in the tree structure or by not properly terminating the loop in the insert function. Additionally, the code may also result in a crash due to writing to or reading from a null pointer.
  • #1
ma12
2
0
my program is not working properly after taking values it gets overflow

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

void insert(struct node**, int);
struct node *ptr;

struct node
{
int data;
struct node *right;
struct node *left;
};


void main()
{
int will , i , num;
ptr = NULL;
ptr -> data = NULL;
printf("Enter the Number");
scanf("%d", &will);

for(i=0; i<will; i++)

{
printf("Enter the Item",&num);
insert(&ptr, num);
}
getche();
}



void insert(struct node **p, int num)
{

if( (*p) == NULL)
{
printf("Leaf node created");
(*p) -> left = NULL;
(*p) -> right = NULL;
(*p) -> data = num;
return;
}
else
{
if( num==(*p) -> data)
printf("Entered repeated values rejected");
return;
}

if(num < (*p) -> data)
{
printf("directed to left ");
insert ( &((*p) -> left) ,num);
}
else
{
printf("Directed to right");
insert( &((*p) -> right), num);
}


return;
}
 
Technology news on Phys.org
  • #2
Overflow of what, in which line.
 
  • #3
in void insert

if(num < (*p) -> data)
{
printf("directed to left ");
insert ( &((*p) -> left) ,num);
}
else
{
printf("Directed to right");
insert( &((*p) -> right), num);
}
 
  • #4
What does p point to?

And if you think the answer is "struct node", check out your code.
 
  • #5
I don't see any mallocs() to allocate space for the structures.
 
  • #6
Overflow probably means stack overflow. This usually means you got into an infinite recursion loop. In your code this is possible if you got into a situation where *p is never equal to NULL in insert(). If this happened insert() would keep calling itself forever, every call to insert() pushes a frame on the stack, eventually the stack overflows. One reason this might have happened is if you built a tree incorrectly such that there is a loop in it.

Also roger is right, if you ever somehow DID get to your "leaf node created" termination condition you'd instantly crash. You establish *p is NULL, then you assign to *p->left. If you write to or read from a null pointer you will crash (but the crash probably would not describe itself as an "overflow").
 

Related to Program Overflow: Troubleshooting Tips

What is Program Overflow?

Program Overflow is a common error that occurs when a computer program tries to store more data in a memory location than it can hold. This can happen due to a programming error or when the program is trying to process too much data at once.

What are the common causes of Program Overflow?

Some common causes of Program Overflow include using an inadequate data type for a variable, attempting to store too much data in a memory location, or not properly handling user input.

How can I troubleshoot Program Overflow?

To troubleshoot Program Overflow, you can start by checking for any programming errors in your code. You can also try using a larger data type for your variables or optimizing your program to handle large amounts of data more efficiently.

Can hardware issues cause Program Overflow?

Yes, hardware issues such as insufficient memory or a faulty processor can also cause Program Overflow. It is important to ensure that your hardware meets the requirements for running your program.

How can I prevent Program Overflow?

To prevent Program Overflow, it is important to thoroughly test and debug your code before running it. Make sure to use appropriate data types for your variables and handle user input carefully. It can also be helpful to optimize your code for efficiency to avoid overwhelming the memory.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
766
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top