variable problems

Started by
15 comments, last by Trienco 12 years, 8 months ago
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

If more information is needed just ask.


Advertisement

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.
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).

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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?


int map_height = 0;
int map_width = 0;
//terrainbox terrain1[map_height][map_width];
terrainbox *terrain1 = new terrainbox[map_height*map_width];


This is your problem.

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Arrays are not resizeable. Use a std::vector, it's a resizable array.
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;
}
What line of code does the debugger say the segfault occurred on?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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);


I don't use a debugger




Well, we just found your problem wink.gif

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement