Program Overflow: Troubleshooting Tips

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a programming issue related to a binary tree implementation in C, specifically focusing on a stack overflow problem that arises during the insertion of nodes. Participants explore the causes of the overflow and the structure of the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the overflow occurs in the `insert` function, particularly during recursive calls based on the comparison of `num` and `(*p) -> data`.
  • Another participant questions what `p` points to in the code, suggesting that there may be an issue with how the pointer is being used.
  • A different participant highlights the absence of `malloc` calls to allocate memory for the `struct node`, indicating a potential source of the problem.
  • One participant suggests that the overflow might be due to stack overflow caused by infinite recursion, which could occur if the tree is incorrectly built, leading to a situation where `*p` is never `NULL`.
  • Concerns are raised about the code attempting to assign values to `*p->left` when `*p` is `NULL`, which would lead to a crash rather than an overflow.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the cause of the overflow, with no consensus on a single solution or understanding of the problem.

Contextual Notes

There are limitations in the code regarding memory allocation and pointer management, which may contribute to the issues discussed. The discussion does not resolve these limitations.

ma12
Messages
2
Reaction score
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
Overflow of what, in which line.
 
in void insert

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

And if you think the answer is "struct node", check out your code.
 
I don't see any mallocs() to allocate space for the structures.
 
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").
 

Similar threads

Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
11K
Replies
14
Views
4K