Home > Net >  I am getting stack smash error specifically on moodle
I am getting stack smash error specifically on moodle

Time:10-18

I have ran the same code on my local compiler and it works perfectly but for some reason on moodle it runs , gives an output and at the end gives a stack smash error. Is it because of the sscanf? Here is the input: 10 (1,3) (12,10) (6,5) (22,13) (2,15) (35,-10) (15,-15) (20,5) (12,-8) (1,-10)

#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<stdlib.h>
using namespace std;
int arr[20] , arr2[20],end = 0;
struct Point
{int x, y;
};
Point p0;
Point nextToTop(vector<Point> &S)
{
    Point p = S.front();
    S.erase(S.begin());
    Point res = S.front();
    S.insert(S.begin(),p);
    return res;
}
void swap(Point &p1, Point &p2)
{Point temp = p1;
    p1 = p2;
    p2 = temp;
}
 

int distSq(Point p1, Point p2)
{return (p1.x - p2.x)*(p1.x - p2.x)  
          (p1.y - p2.y)*(p1.y - p2.y);
}
 

int orientation(Point p, Point q, Point r)
{int val = (q.y - p.y) * (r.x - q.x) -
              (q.x - p.x) * (r.y - q.y);
    if (val == 0) return 0;  
    return (val > 0)? 1: 2; 
}
 

int compare(const void *vp1, const void *vp2)
{
   Point *p1 = (Point *)vp1;
   Point *p2 = (Point *)vp2;
 
   int o = orientation(p0, *p1, *p2);
   if (o == 0)
     return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
 
   return (o == 2)? -1: 1;
}

void convexHull(Point points[], int n)
{
 
   int ymin = points[0].y, min = 0;
   for (int i = 1; i < n; i  )
   {
     int y = points[i].y;

  
     if ((y < ymin) || (ymin == y &&
         points[i].x < points[min].x))
        ymin = points[i].y, min = i;
   }
 

   swap(points[0], points[min]);
 

   p0 = points[0];
   qsort(&points[1], n-1, sizeof(Point), compare);
   int m=1;

   for (int i=1; i<n; i  )
   {
       
       while (i < n-1 && orientation(p0, points[i],
                                    points[i 1]) == 0)
          i  ;
 
 
       points[m] = points[i];
       m  ;  
   }
 

   if (m < 3) return;
vector<Point> S;
   S.insert(S.begin(),points[0]);
   S.insert(S.begin(),points[1]);
   S.insert(S.begin(),points[2]);
 for (int i = 3; i < m; i  )
   {
 
      while (S.size()>1 && orientation(nextToTop(S), S.front(), points[i]) != 2)
         S.erase(S.begin());
      S.insert(S.begin(),points[i]);
   }
      vector<Point> S2 = S;
      int k=0;
   while(S2.size()>0){
       Point a = S2.front();
       arr[k] = a.x;
       arr2[k] = a.y;
       S2.erase(S2.begin());
       k  ;
   }
   int c,d;
   for(int j = 0; j<k;j  ){
       for(int i = j 1; i<k;i  ){
       if(arr[j]>arr[i]){
           c = arr[j];
           d = arr2[j];
           arr[j] = arr[i];
           arr2[j] = arr2[i];
           arr[i] = c;
           arr2[i] = d;
       }else if(arr[j] == arr[i]){
           c = arr[j];
           d = arr2[j];
           if(arr2[j] > arr2[j]){
               arr[j] = arr[i];
           arr2[j] = arr2[i];
           arr[i] = c;
           arr2[i] = d;}}}}
   for(int j =0;j<k;j  )
   {cout << "(" << arr[j] << "," << arr2[j] <<")" << endl;
       }}
int main()
{int n;
    cin>>n;
    Point points[n] ;
    int a,b;
    char c[5];
   for(int i=0;i<n;i  ){
       cin>>c;
       sscanf(c, "(%d,%d)", &a,&b);
       points[i] = {a,b};}
convexHull(points, n);
    return 0;
}

Here is the output picture

CodePudding user response:

I looked at your code, and I think problem is in sscanf, and solution is actually simple. Try to change declaration of string ( char array ) in main function from char c[5]; to char c[10]; it can be any other number, but I think, if you enter even bigger numbers and c[10] will make error, so I recommend you to make it char c[50]; or because you're using integers I think char c[25]; will be fine.

  •  Tags:  
  • c
  • Related