1

I am working with openGL in C++ and while I was taking input for the vertices in a loop, I encountered a problem where the number of vertices is changing with the input of the input value, although I am not swapping the variable.

Here the variable that I am in trouble with is numPoints, I have declared it at the top with the include lines (to try to make it global, I am originally from Java). and the value changes when the input loop value changes to i == 2. I am taking two values from keyboard, x and y. The detailed code with the main function is given below.

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


#include <stdlib.h>
#include "stdio.h"

int pointValx[0];
int pointValy[0];
int numPoint;

void takeInput()
{
printf("Screen Size is 0 - 400 in X and 0 - 500 in Y\n");
printf("Lab for Line and Point\n");
printf("number of lines >> \n");
scanf("%d",&numPoint); //comment this line for Line

pointValx[numPoint];
pointValy[numPoint];

printf("numPoint >> %d\n",numPoint);

for(int i = 0; i < numPoint;)
{
    int x,y;
    printf("Input for X >> %d\n", i);
    scanf("%d",&x);
    printf("numPoint >> %d\n",numPoint);
    if(x >= 0 && x <= 400)
    {
        printf("Input for Y >> %d\n", i);
        scanf("%d",&y);
        if(y >= 0 && y <= 500)
        {
            pointValx[i] = x;
            pointValy[i] = y;
            i++;
        }
        else
        {
            printf("Y value crossed the limit\n");
        }
    }
    else
    {
       printf("X value crossed the limit\n");
    }
   }

   printf("End of Input file\n");
 }


/// MAIN FUNCTION

int main(int argc, char *argv[])
{
int win;

glutInit(&argc, argv);      /* initialize GLUT system */

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(400,500);        /* width=400pixels height=500pixels */
win = glutCreateWindow("GL_LINES and Points");  /* create window */

/* from this point on the current window is win */
takeInput();

glClearColor(0.0,0.0,0.0,0.0);  /* set background to black */
gluOrtho2D(0,400,0,500);        /* how object is mapped to window */
glutDisplayFunc(displayCB);     /* set window's display callback */

glutMainLoop();         /* start processing events... */

/* execution never reaches this point */

return 0;
}

2 Answers2

2
pointValx[numPoint];
pointValy[numPoint];

This code doesn't do what you think it does

It accesses the value at index numPoint and then does nothing with it. Accessing the value itself is undefined behavior.

What you should do is declare them as pointer and then allocate the arrays.

int* pointValx;
int* pointValy;

void takeInput()
{
printf("Screen Size is 0 - 400 in X and 0 - 500 in Y\n");
printf("Lab for Line and Point\n");
printf("number of lines >> \n");
scanf("%d",&numPoint); //comment this line for Line

pointValx = (int*)malloc(numPoint*sizeof(int));
pointValy = (int*)malloc(numPoint*sizeof(int));

and after you are done with them you should free them:

free(pointValx);
free(pointValy);
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • I have done this but I am getting an error > invalid conversion from 'void*' to 'int*' [-fpermissive] in the line where I have allocated the memory to those pointer, pointValx and pointValy – MD Kamal Hossain Shajal Nov 10 '15 at 10:49
  • @MDKamalHossainShajal Then you need to cast it using `pointValx = (int*)malloc(numPoint*sizeof(int));` – ratchet freak Nov 10 '15 at 10:50
  • @MDKamalHossainShajal That indicates that you really are programming in C++, even though you don't use any C++ functionality or functions. Then I really implore you to learn C++ properly, including using `std::vector` instead of pointers. Check [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for beginners books and tutorials. – Some programmer dude Nov 10 '15 at 11:29
1

The problem is these two arrays:

int pointValx[0];
int pointValy[0];

Here you declare two arrays of size zero. Any indexing in them will be out of bounds and lead to undefined behavior.

Arrays are fixed when you compile your program, you can't change the size later at runtime. If you want to change the size at runtime, then you need to use std::vector instead (which is what I recommend) or allocate them dynamically using pointers and new[].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • but I redefined their size in the takeInput() method. plus, I have checked to take input till i = 10 before I force quit it. – MD Kamal Hossain Shajal Nov 10 '15 at 10:40
  • @MDKamalHossainShajal But you ***can't*** change the size of an array at runtime, it is fixed when you compile your program. – Some programmer dude Nov 10 '15 at 10:40
  • Oh. okay but I need to access these arrays from anywhere I want in this file. How can I do so then? – MD Kamal Hossain Shajal Nov 10 '15 at 10:43
  • He is using C (at least according to his tags) so `new[]` and `vector` aren't available – ratchet freak Nov 10 '15 at 10:43
  • @ratchetfreak I was looking at the original tag, before the edit. – Some programmer dude Nov 10 '15 at 10:46
  • @MDKamalHossainShajal If you are using C (not C++ as you originally tagged your question) then use `malloc` to dynamically allocate the "arrays". And you have two ways of getting access to these arraya from all parts of the program: Pass the ***pointers*** as arguments to the functions that needs them (my recommendation) or keep the pointer variables as global variables, just like now. – Some programmer dude Nov 10 '15 at 10:48