Program Overflow: Troubleshooting Tips

AI Thread Summary
The discussion revolves around a code snippet for a binary tree insertion program that is experiencing issues, specifically an overflow error. The primary concern is that the program does not allocate memory for the nodes, leading to potential stack overflow due to infinite recursion. This occurs when the `insert` function is called repeatedly without reaching a base case, as the pointer `*p` never becomes NULL. Additionally, there is a critical flaw where the program attempts to assign values to `*p->left` and `*p->right` without first allocating memory for `*p`, which would result in a crash when trying to dereference a null pointer. The conversation highlights the need for proper memory management using `malloc()` to prevent these errors and ensure the program functions correctly.
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").
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top