# variable problems

This topic is 2395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have trying to fix a problem with one of my variables. I have tried declaring it like so, as a global variable:

terrainbox terrain1[map_height][map_width];

If I declare it in a funtion then it has no problems at all but I need it to be global and it is giving me the following error:
error: array bound is not an integer constant before ‘]’ token

##### Share on other sites

I have trying to fix a problem with one of my variables. I have tried declaring it like so, as a global variable:

terrainbox terrain1[map_height][map_width];

If I declare it in a funtion then it has no problems at all but I need it to be global and it is giving me the following error:
error: array bound is not an integer constant before ‘]’ token

I assume the following: As a global, in-place variable the index range(s) must be known at compile time, and they need to be given by a constant integer valued expression. Your map_height and/or map_width are presumably integer variables, but integer constants are expected.

##### Share on other sites
if your changing map_height/map_width at runtime then its not possible to do (you can allocate with new instead). If those values are NOT going to be changing then just define them as constant.

const int map_height = 1;
const int map_width = 1;
terrainbox terrain1[map_height][map_width];

Of course they need to be known before you declare your terrain. Otherwise will have to do somethign like:

terrainbox *terrain1 = new terrainbox[map_height*map_width];
// Make sure to delete it at the end with
delete [] terrain1;
terrain1 = NULL;

Althought its declared as just an array (not a 2 dimensional one), indexing should still work but you might have to do this:

terrain1[x][y] now becomes terrain1[x+y*map_width]; (or maybe terrain1[x*map_height+y]; depending if you want your array to be row or column major).

##### Share on other sites
This variable is where my map is getting loaded into so it can't be a constant but with the other method it compiles but it exits with segmentation fault

So I think I will paste my code:

#include <iostream> #include <stdlib.h> #include <fstream> #include <GL/glut.h> using namespace std; struct object{ float x; float y; float vspeed; float hspeed; }; struct terrainbox{ float x; float y; int type; }; int map_height = 0; int map_width = 0; //terrainbox terrain1[map_height][map_width]; terrainbox *terrain1 = new terrainbox[map_height*map_width]; void loadMap(){ FILE *input_file = fopen("data/maps/test.ssm", "r"); fscanf(input_file, "%d", &map_height); fscanf(input_file, "%d", &map_width); int x; int y; for(int i=0;i<map_height*map_width;i++){ y++; x++; terrain1.x = x; terrain1.y = y; fscanf(input_file, "%d", &terrain1.type); } return; } object player1; void handleKeypress(unsigned char key, //The key that was pressed int x, int y) { //The current mouse coordinates switch (key) { case 27: //Escape key delete [] terrain1; terrain1 = NULL; exit(0); //Exit the program } } void initRendering() { glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); player1.x = -2.0; player1.y = 1.5; player1.vspeed = 0.07; } void drawScene() { //Clear information from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective glBegin(GL_QUADS); for(int i=0;i<map_height;i++){ glVertex3f(terrain1.x+1, terrain1.y, -14.0f); glVertex3f(terrain1.x, terrain1.y, -14.0f); glVertex3f(terrain1.x, terrain1.y+1, -14.0f); glVertex3f(terrain1.x+1, terrain1.y+1, -14.0f); } //player glVertex3f(player1.x+0.5f, player1.y, -14.0f); glVertex3f(player1.x, player1.y, -14.0f); glVertex3f(player1.x, player1.y+0.5f, -14.0f); glVertex3f(player1.x+0.5f, player1.y+0.5f, -14.0f); /*glVertex3f(-0.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 1.5f, -14.0f); glVertex3f(-0.5f, 1.5f, -14.0f);*/ glEnd(); glutSwapBuffers(); //Send the 3D scene to the screen } void handleResize(int w, int h) { //Tell OpenGL how to convert from coordinates to pixel values glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective //Set the camera perspective glLoadIdentity(); //Reset the camera gluPerspective(45.0, //The camera angle (double)w / (double)h, //The width-to-height ratio 1.0, //The near z clipping coordinate 200.0); //The far z clipping coordinate } void update(int value){ player1.y += player1.vspeed; player1.vspeed -= 0.0015; glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv){ loadMap(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Platformer"); initRendering(); glutKeyboardFunc(handleKeypress); glutDisplayFunc(drawScene); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }

What do you guys think?

##### Share on other sites
int map_height = 0; int map_width = 0; //terrainbox terrain1[map_height][map_width]; terrainbox *terrain1 = new terrainbox[map_height*map_width];

This code runs before your program starts (i.e. before main() is called). At that point, your width and height are both 0, so you try to allocate a 0-size array... boom.

You need to wait and only allocate the array after you have loaded the width and height values.

##### Share on other sites
Arrays are not resizeable. Use a std::vector, it's a resizable array.

##### Share on other sites
Using vectors still give me segmentation fault?

Here is updated code:

#include <iostream> #include <stdlib.h> #include <fstream> #include <GL/glut.h> #include <vector> using namespace std; struct object{ float x; float y; float vspeed; float hspeed; }; struct terrainbox{ float x; float y; int type; }; int map_height = 0; int map_width = 0; //terrainbox terrain1[map_height][map_width]; //terrainbox *terrain1 = new terrainbox[map_height*map_width]; vector<terrainbox> terrain1; void loadMap(){ FILE *input_file = fopen("data/maps/test.ssm", "r"); fscanf(input_file, "%d", &map_height); fscanf(input_file, "%d", &map_width); terrain1.resize(map_height*map_width); int i; for(int y=0;y<map_height;y++){ for(int x=0;x<map_width;x++){ i++; terrain1.x = x; terrain1.y = y; fscanf(input_file, "%d", &terrain1.type); } } return; } object player1; void handleKeypress(unsigned char key, //The key that was pressed int x, int y) { //The current mouse coordinates switch (key) { case 27: //Escape key exit(0); //Exit the program } } void initRendering() { glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); player1.x = -2.0; player1.y = 1.5; player1.vspeed = 0.07; } void drawScene() { //Clear information from last draw glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective glLoadIdentity(); //Reset the drawing perspective glBegin(GL_QUADS); for(int i=0;i<map_height;i++){ glVertex3f(terrain1.x+1, terrain1.y, -14.0f); glVertex3f(terrain1.x, terrain1.y, -14.0f); glVertex3f(terrain1.x, terrain1.y+1, -14.0f); glVertex3f(terrain1.x+1, terrain1.y+1, -14.0f); } //player glVertex3f(player1.x+0.5f, player1.y, -14.0f); glVertex3f(player1.x, player1.y, -14.0f); glVertex3f(player1.x, player1.y+0.5f, -14.0f); glVertex3f(player1.x+0.5f, player1.y+0.5f, -14.0f); /*glVertex3f(-0.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 0.5f, -14.0f); glVertex3f(-2.5f, 1.5f, -14.0f); glVertex3f(-0.5f, 1.5f, -14.0f);*/ glEnd(); glutSwapBuffers(); //Send the 3D scene to the screen } void handleResize(int w, int h) { //Tell OpenGL how to convert from coordinates to pixel values glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective //Set the camera perspective glLoadIdentity(); //Reset the camera gluPerspective(45.0, //The camera angle (double)w / (double)h, //The width-to-height ratio 1.0, //The near z clipping coordinate 200.0); //The far z clipping coordinate } void update(int value){ player1.y += player1.vspeed; player1.vspeed -= 0.0015; glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv){ loadMap(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("Platformer"); initRendering(); glutKeyboardFunc(handleKeypress); glutDisplayFunc(drawScene); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }

##### Share on other sites
What line of code does the debugger say the segfault occurred on?

##### Share on other sites
I don't use a debugger

edit: I used gdb and I ran the program and it said:

Program received signal SIGSEGV, Segmentation fault.
0x00345a04 in _IO_vfscanf () from /lib/i386-linux-gnu/libc.so.6

so this might mean its having problems at line 38 which is below:

fscanf(input_file, "%d", &terrain1.type);

##### Share on other sites

I don't use a debugger

Well, we just found your problem

Seriously, programming without a debugger is roughly akin to jumping out of the space shuttle with no space suit. The death may not be instant, but it's sure as hell gonna hurt...

On a slightly more useful note, initialize i to 0. Uninitialized variables are the tool of the devil, and if you ever leave a variable uninitialized in your programs, you should feel very naughty.

##### Share on other sites
ok I found the problem line of code.

fscanf(input_file, "%d", &map_height); //line 29

The problem is that I don't know whats wrong with it.

If I comment it out the program runs but if I put it back in it stops working?

what do I do now?

##### Share on other sites
Hello!

Have you checked if the file is opened correctly ? (i.e if input_file is not null). If it is, maybe the path you are giving the function is incorrect, thus it cannot find the file. Hope it helps.

##### Share on other sites
You should use error checking, see if the file is opened, see if a read succeeds. You also need to be careful to close the file when you're done. Using the standard C++ file objects make this a little easier. You should signal the calling function if the load failed.

Use of an uninitialised variable should trigger a warning. I recommend you increase your compiler warning level until it does (better still, just set the highest warning leve), then fix all the warnings your code generates. Finally, tell the compiler to treat warnings as errors from now on.

##### Share on other sites
Hey well i fixed my problem.

It was just an issue which I was modifying memory I was not supposed to. I had to change the following:

terrain1.resize(map_height*map_width);

to

terrain1.resize(map_height*map_width+3);

now I'm trying to work on my collision detection system (which is not going to well)

anyway I thank you guys for your help.

##### Share on other sites

Hey well i fixed my problem.

It was just an issue which I was modifying memory I was not supposed to. I had to change the following:

terrain1.resize(map_height*map_width);

to

terrain1.resize(map_height*map_width+3);

This is not a correct fix for the problem.

The logical error is that you have 'i++' in your loop before you use 'i' to index. This means that you put the first set of data into element 1, the next into element 2, etc., and skip element 0 completely.

You have map_width * map_height many places to work with, so that should be the storage size you want. I have no idea how you came up with the +3.

##### Share on other sites
Well you see the +3 was because +1 did not work.

##### Share on other sites

Well you see the +3 was because +1 did not work.

His point is that you didn't fix the bug. Your "fix" seems to be based on randomly making arbitrary changes until you don't _see_ the bug anymore. There should be neither a +1 or a +3 and how does preventing a segfault in a hacky way change anything about the actual bug where your code is _still_ reading/writing to the wrong location? You have essentially just made an easy to debug error (hard crash) into a hard to debug error (eventually you will notice at a completely different point in your program that some values aren't what they should be without any clue where it actually happens).

Sorry, but you can't fix a bug without actually understanding where it happens, why it happens and how to prevent it. A segfault or access violation isn't the bug, it's the consequence of a bug.

 int i; for(int y=0;y<map_height;y++){ for(int x=0;x<map_width;x++){ i++; terrain1.x = x; terrain1.y = y; fscanf(input_file, "%d", &terrain1.type); } }