• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Segmentation

Can anyone explain to me why I am getting a segmentation fault when I switch to 2?
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
//  OpenGL with prototypes for glext
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

//  Globals
int th=0;       // Azimuth of view angle
int ph=0;       // Elevation of view angle
int mode=1;     // Dimension (1-4)
double w=1;     // W variable
double dim=2;   // Dimension of orthogonal box
char* text[] = {"","2D","3D constant Z","3D","4D"};  // Dimension display text

/*
 *  Convenience routine to output raster text
 *  Use VARARGS to make this more flexible
 */
#define LEN 8192  // Maximum length of text string
void Print(const char* format , ...)
{
   char    buf[LEN];
   char*   ch=buf;
   va_list args;
   //  Turn the parameters into a character string
   va_start(args,format);
   vsnprintf(buf,LEN,format,args);
   va_end(args);
   //  Display the characters one at a time at the current raster position
   while (*ch)
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*ch++);
}

/*
 *  Display the scene
 */
void display()
{
   //  Clear the image
   glClear(GL_COLOR_BUFFER_BIT);
   //  Reset previous transforms
   glLoadIdentity();
   //  Set view angle
   glRotated(ph,1,0,0);
   glRotated(th,0,1,0);
   //  Draw 10 pixel yellow points
   glColor3f(1,1,0);
   glPointSize(10);
   glBegin(GL_POINTS);
   switch (mode)
   {
   //  Two dimensions
   case 1:
      glVertex2d(0.1,0.1);
      glVertex2d(0.3,0.3);
      glVertex2d(0.5,0.5);
      glVertex2d(0.7,0.7);
      glVertex2d(0.9,0.9);
      break;
   //  Three dimensions - constant Z
   case 2:
      glVertex3d(0.1,0.1,w);
      glVertex3d(0.3,0.3,w);
      glVertex3d(0.5,0.5,w);
      glVertex3d(0.7,0.7,w);
      glVertex3d(0.9,0.9,w);
      break;
   //  Three dimensions - variable Z
   case 3:
      glVertex3d(0.1,0.1,0.1);
      glVertex3d(0.3,0.3,0.2);
      glVertex3d(0.5,0.5,0.4);
      glVertex3d(0.7,0.7,0.6);
      glVertex3d(0.9,0.9,0.9);
      break;
   //  Four dimensions
   case 4:
      glVertex4d(0.1,0.1,0.1,w);
      glVertex4d(0.3,0.3,0.2,w);
      glVertex4d(0.5,0.5,0.4,w);
      glVertex4d(0.7,0.7,0.6,w);
      glVertex4d(0.9,0.9,0.9,w);
      break;
   }
   glEnd();
   //  Draw axes in white
   glColor3f(1,1,1);
   glBegin(GL_LINES);
   glVertex3d(0,0,0);
   glVertex3d(1,0,0);
   glVertex3d(0,0,0);
   glVertex3d(0,1,0);
   glVertex3d(0,0,0);
   glVertex3d(0,0,1);
   glEnd();
   //  Label axes
   glRasterPos3d(1,0,0);
   Print("X");
   glRasterPos3d(0,1,0);
   Print("Y");
   glRasterPos3d(0,0,1);
   Print("Z");
   //  Display parameters
   glWindowPos2i(5,5);
   Print("View Angle=%d,%d  %s",th,ph,text[mode]);
   if (mode==2)
      Print("  z=%.1f",w);
   else if (mode==4)
      Print("  w=%.1f",w);
   //  Flush and swap
   glFlush();
   glutSwapBuffers();
}

/*
 *  GLUT calls this routine when a key is pressed
 */
void key(unsigned char ch,int x,int y)
{
   //  Exit on ESC
   if (ch == 27)
      exit(0);
   //  Reset view angle
   else if (ch == '0')
      th = ph = 0;
   //  Switch dimensions
   else if ('1'<=ch && ch<='4')
   {
      mode = ch-'0';
      if (mode==2) w = 0;
      if (mode==4) w = 1;
   }
   //  Increase w by 0.1
   else if (ch == '+')
      w += 0.1;
   //  Decrease w by 0.1
   else if (ch == '-')
      w -= 0.1;
   //  Tell GLUT it is necessary to redisplay the scene
   glutPostRedisplay();
}

/*
 *  GLUT calls this routine when an arrow key is pressed
 */
void special(int key,int x,int y)
{
   //  Right arrow key - increase azimuth by 5 degrees
   if (key == GLUT_KEY_RIGHT)
      th += 5;
   //  Left arrow key - decrease azimuth by 5 degrees
   else if (key == GLUT_KEY_LEFT)
      th -= 5;
   //  Up arrow key - increase elevation by 5 degrees
   else if (key == GLUT_KEY_UP)
      ph += 5;
   //  Down arrow key - decrease elevation by 5 degrees
   else if (key == GLUT_KEY_DOWN)
      ph -= 5;
   //  Keep angles to +/-360 degrees
   th %= 360;
   ph %= 360;
   //  Tell GLUT it is necessary to redisplay the scene
   glutPostRedisplay();
}

/*
 *  GLUT calls this routine when the window is resized
 */
void reshape(int width,int height)
{
   //  Ratio of the width to the height of the window
   double w2h = (height>0) ? (double)width/height : 1;
   //  Set the viewport to the entire window
   glViewport(0,0, width,height);
   //  Tell OpenGL we want to manipulate the projection matrix
   glMatrixMode(GL_PROJECTION);
   //  Undo previous transformations
   glLoadIdentity();
   //  Orthogonal projection box adjusted for the
   //  aspect ratio of the window
   glOrtho(-dim*w2h,+dim*w2h, -dim,+dim, -dim,+dim);
   //  Switch to manipulating the model matrix
   glMatrixMode(GL_MODELVIEW);
   //  Undo previous transformations
   glLoadIdentity();
}

/*
 *  Start up GLUT and tell it what to do
 */
int main(int argc,char* argv[])
{
  //  Initialize GLUT and process user parameters
   glutInit(&argc,argv);
   //  Request double buffered, true color window 
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   //  Request 500 x 500 pixel window
   glutInitWindowSize(500,500);
   //  Create the window
   glutCreateWindow("Coordinates");
   //  Tell GLUT to call "display" when the scene should be drawn
   glutDisplayFunc(display);
  //  Tell GLUT to call "reshape" when the window is resized
   glutReshapeFunc(reshape);
   //  Tell GLUT to call "special" when an arrow key is pressed
   glutSpecialFunc(special);
   //  Tell GLUT to call "key" when a key is pressed
   glutKeyboardFunc(key);
   //  Pass control to GLUT so it can interact with the user
   glutMainLoop();
   //  Return code
   return 0;
}

Open in new window

0
islandguy10
Asked:
islandguy10
  • 4
  • 2
2 Solutions
 
islandguy10Author Commented:
Switcht mode to 2 that is...so that it runs case 2.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Are you able to do some basic debugging (like commenting out the if(mode==2) Print(...); call) to narrow down exactly where it is seg faulting? After a quick look, I can't see anything blatantly obvious, so it would be good to have an idea of approx. location where the problem is!
0
 
islandguy10Author Commented:
crud i posted the wrong code...should be this one...
/*
 *  Coordinates
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
//  OpenGL with prototypes for glext
#define GL_GLEXT_PROTOTYPES

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#ifndef N
#define N 50000
#endif

//  Globals
int th=0;       // Azimuth of view angle
int ph=0;       // Elevation of view angle
int mode=1;     // Dimension (1-4)
double w=1;     // W variable
double dim = 40;   // Dimension of orthogonal box
char* text[] = {"3D"};  // Dimension display text

int i = 0;
double x_param,y_param,z_param,x_calc,y_calc,z_calc;
double h = 0.01;
double a = 10.0;
double b = 28.0;
double c = 8.0 / 3.0;

/*
 *  Convenience routine to output raster text
 *  Use VARARGS to make this more flexible
 */
#define LEN 8192  // Maximum length of text string
void Print(const char* format , ...)
{
   char    buf[LEN];
   char*   ch=buf;
   va_list args;
   //  Turn the parameters into a character string
   va_start(args,format);
   vsnprintf(buf,LEN,format,args);
   va_end(args);
   //  Display the characters one at a time at the current raster position
   while (*ch)
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*ch++);
}

/*
 *  Display the scene
 */
void display()
{
   //  Clear the image
   glClear(GL_COLOR_BUFFER_BIT);
   //  Reset previous transforms
   glLoadIdentity();
   //  Set view angle
   glRotated(ph,1,0,0);
   glRotated(th,0,1,0);
   //  Draw 1 pixel yellow points
   
   switch (mode)
   {
   //  Three dimensional display
   case 1:
   glColor3f(1,0,0);
   glPointSize(1);
   glBegin(GL_POINTS);
   x_param = 0.1;
   y_param = 0.0;
   z_param = 0.0;
   for (i=0;i<N;i++)
   {
      x_calc = x_param + h * a * (y_param - x_param);
      y_calc = y_param + h * (x_param * (b - z_param) - y_param);
      z_calc = z_param + h * (x_param * y_param - c * z_param);
      x_param = x_calc;
      y_param = y_calc;
      z_param = z_calc;
      if (i > 100)
         //printf("%d %g %g %g\n",i,x_param,y_param,z_param);
         glVertex3d(x_param,y_param,z_param);
   }
   glEnd();
      break;

   case 2:
   glColor3f(1,1,0);
   glLineWidth(1.0);
   glBegin(GL_LINES);
   x_param = 0.1;
   y_param = 0.0;
   z_param = 0.0;
   
   for (i=0;i<N;i++)
   {
      x_calc = x_param + h * a * (y_param - x_param);
      y_calc = y_param + h * (x_param * (b - z_param) - y_param);
      z_calc = z_param + h * (x_param * y_param - c * z_param);
      x_param = x_calc;
      y_param = y_calc;
      z_param = z_calc;
      if (i > 100)
         //printf("%d %g %g %g\n",i,x_param,y_param,z_param);

         glVertex3d(x_param,y_param,z_param);
   }
   glEnd();
      break;

   }
   
   //  Draw axes in white
   glColor3f(1,1,1);
   glBegin(GL_LINES);
   glVertex3d(0,0,0);
   glVertex3d(25,0,0);
   glVertex3d(0,0,0);
   glVertex3d(0,25,0);
   glVertex3d(0,0,0);
   glVertex3d(0,0,25);
   glEnd();
   //  Label axes
   glRasterPos3d(25,0,0);
   Print("X");
   glRasterPos3d(0,25,0);
   Print("Y");
   glRasterPos3d(0,0,25);
   Print("Z");
   //  Display parameters
   glWindowPos2i(5,5);
   Print("View Angle=%d,%d  %s",th,ph,text[mode]);
   if (mode==1)
        Print("  z=%.1f",w);
   else if (mode==2)
        Print("  w=%.1f",w);
   glFlush();
   glutSwapBuffers();
}

/*
 *  GLUT calls this routine when a key is pressed
 */
void key(unsigned char ch,int x,int y)
{
   //  Exit on ESC
   if (ch == 27)
      exit(0);
   //  Reset view angle
   else if (ch == '0')
      th = ph = 0;
   else if (ch == '1')
   {
       mode = 1;
   }
   else if (ch == '2')
   {
       mode = 2;
   }
   //  Increase dim by 1
   else if (ch == '-'){
      dim += 1;
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(-dim,+dim, -dim,+dim, -dim,+dim);
      glMatrixMode(GL_MODELVIEW);
   }
   //  Decrease dim by 1
   else if (ch == '+'){
      dim -= 1;
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(-dim,+dim, -dim,+dim, -dim,+dim);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
   }
   
   //  Tell GLUT it is necessary to redisplay the scene
   glutPostRedisplay();
}

/*
 *  GLUT calls this routine when an arrow key is pressed
 */
void special(int key,int x,int y)
{
   //  Right arrow key - increase azimuth by 5 degrees
   if (key == GLUT_KEY_RIGHT)
      th += 5;
   //  Left arrow key - decrease azimuth by 5 degrees
   else if (key == GLUT_KEY_LEFT)
      th -= 5;
   //  Up arrow key - increase elevation by 5 degrees
   else if (key == GLUT_KEY_UP)
      ph += 5;
   //  Down arrow key - decrease elevation by 5 degrees
   else if (key == GLUT_KEY_DOWN)
      ph -= 5;
   //  Keep angles to +/-360 degrees
   th %= 360;
   ph %= 360;
   //  Tell GLUT it is necessary to redisplay the scene
   glutPostRedisplay();
}

/*
 *  GLUT calls this routine when the window is resized
 */
void reshape(int width,int height)
{
   //  Ratio of the width to the height of the window
   double w2h = (height>0) ? (double)width/height : 1;
   //  Set the viewport to the entire window
   glViewport(0,0, width,height);
   //  Tell OpenGL we want to manipulate the projection matrix
   glMatrixMode(GL_PROJECTION);
   //  Undo previous transformations
   glLoadIdentity();
   //  Orthogonal projection box adjusted for the
   //  aspect ratio of the window
   glOrtho(-dim*w2h,+dim*w2h, -dim,+dim, -dim,+dim);
   //  Switch to manipulating the model matrix
   glMatrixMode(GL_MODELVIEW);
   //  Undo previous transformations
   glLoadIdentity();
}

/*
 *  Start up GLUT and tell it what to do
 */
int main(int argc,char* argv[])
{
  //  Initialize GLUT and process user parameters
   glutInit(&argc,argv);
   //  Request double buffered, true color window
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
   //  Request 1000 x 1000 pixel window
   glutInitWindowSize(1000,1000);
   //  Create the window
   glutCreateWindow("Coordinates");
   //  Tell GLUT to call "display" when the scene should be drawn
   glutDisplayFunc(display);
  //  Tell GLUT to call "reshape" when the window is resized
   glutReshapeFunc(reshape);
   //  Tell GLUT to call "special" when an arrow key is pressed
   glutSpecialFunc(special);
   //  Tell GLUT to call "key" when a key is pressed
   glutKeyboardFunc(key);
   //  Pass control to GLUT so it can interact with the user
   glutMainLoop();
   //  Return code
   return 0;
}

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
islandguy10Author Commented:
I did and found my solution.  Thank you for the tip mccarl.  I'm very new to C as all my experience has been with Java.  Excited to work with openGL though.  My problem was here:  

char* text[] = {"3D"};  // Dimension display text

Segmentation Fault due to attempting to access an array out of bounds.  Thanks for taking a look!
0
 
islandguy10Author Commented:
I gave full credit as those are the kind of answers I like.  Point me in the right direction to help me solve on my own.  
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I looked at that array in the first code posted and it was ok, but I didn't look closely enough at the second code that you posted.

Still, I'm glad that my tips helped you to get to your solution on your own!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now