|
|
back to boardCrash (Access Violation) Hi, This is my code and it gives Access Violation on test case #1 . Please Help. Its running fine on my system. #include<stdio.h> #include<stdlib.h> struct Tree { struct Tree *left; struct Tree *right; int x; int lsize; }; int insert(struct Tree *root, int x){ struct Tree *temp =(struct Tree *) malloc(sizeof(struct Tree)); temp->x = x; temp->left = NULL; temp->right = NULL; temp->lsize=0; struct Tree *temp2 = root; int flag = 0; int count = 0; struct Tree *prev = NULL; while(temp2!=NULL){ if(temp2->x>temp->x){ flag = 0; prev = temp2; temp2->lsize++; temp2 = temp2->left; } else{ flag = 1; prev = temp2; count = count + 1 + prev->lsize; temp2 = temp2->right; } } if (flag==0) prev->left = temp; else prev->right = temp; return count; } int main(){ int n; scanf("%d", &n); int i = 1; int x,y; scanf("%d %d", &x, &y); struct Tree *root = (struct Tree *) malloc(sizeof(struct Tree)); root->x = x; root->left = NULL; root->right = NULL; int *ans =(int *) malloc(n*sizeof(int)); int j = 0; while(j<n){ ans[j]=0; j++; } ans[0]++; while (i<n) { scanf("%d %d", &x,&y); int k = insert(root,x); ans[k]++; i++; } i = 0; while(i<n){ printf("%d \n", ans[i]); i++; } return 0; } Re: Crash (Access Violation) This code was submitted for C++. It doesn't even compile for C? What might be the issue? It compiles and runs fine on my system. |
|
|