Sign in to follow this  
qw3rty_rocks

variable problems

Recommended Posts

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

[b]terrainbox terrain1[map_height][map_width];[/b]

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:
[b]error: array bound is not an integer constant before ‘]’ token[/b]

If more information is needed just ask.[b]
[/b]

Share this post


Link to post
Share on other sites
haegarr    7372
[quote name='QW3RTY ROCKS' timestamp='1310889672' post='4836281']
I have trying to fix a problem with one of my variables. I have tried declaring it like so, as a global variable:

[b]terrainbox terrain1[map_height][map_width];[/b]

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:
[b]error: array bound is not an integer constant before ‘]’ token[/b]
[/quote]
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 [i]constant[/i] integer valued expression. Your map_height and/or map_width are presumably integer [i]variables[/i], but integer [i]constants[/i] are expected.

Share this post


Link to post
Share on other sites
Nanoha    2682
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 this post


Link to post
Share on other sites
qw3rty_rocks    100
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:

[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[i].x = x;
terrain1[i].y = y;
fscanf(input_file, "%d", &terrain1[i].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[i].x+1, terrain1[i].y, -14.0f);
glVertex3f(terrain1[i].x, terrain1[i].y, -14.0f);
glVertex3f(terrain1[i].x, terrain1[i].y+1, -14.0f);
glVertex3f(terrain1[i].x+1, terrain1[i].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;
}[/code]

What do you guys think?


Share this post


Link to post
Share on other sites
ApochPiQ    23000
[code]int map_height = 0;
int map_width = 0;
//terrainbox terrain1[map_height][map_width];
terrainbox *terrain1 = new terrainbox[map_height*map_width];[/code]

This is your problem.

This code runs [i]before[/i] 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 [i]after[/i] you have loaded the width and height values.

Share this post


Link to post
Share on other sites
qw3rty_rocks    100
Using vectors still give me segmentation fault?

Here is updated code:

[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[i].x = x;
terrain1[i].y = y;
fscanf(input_file, "%d", &terrain1[i].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[i].x+1, terrain1[i].y, -14.0f);
glVertex3f(terrain1[i].x, terrain1[i].y, -14.0f);
glVertex3f(terrain1[i].x, terrain1[i].y+1, -14.0f);
glVertex3f(terrain1[i].x+1, terrain1[i].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;
}[/code]

Share this post


Link to post
Share on other sites
qw3rty_rocks    100
I don't use a debugger




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

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

so this might mean its having problems at line 38 which is below:[b]
[/b]

[code]fscanf(input_file, "%d", &terrain1[i].type);[/code]

Share this post


Link to post
Share on other sites
ApochPiQ    23000
[quote name='QW3RTY ROCKS' timestamp='1310953138' post='4836585']
I don't use a debugger
[/quote]



Well, we just found your problem [img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img]

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 this post


Link to post
Share on other sites
qw3rty_rocks    100
ok I found the problem line of code.

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

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 this post


Link to post
Share on other sites
rip-off    10976
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 this post


Link to post
Share on other sites
qw3rty_rocks    100
Hey well i fixed my problem. :lol:

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

[code]terrain1.resize(map_height*map_width);[/code]

to

[code]terrain1.resize(map_height*map_width+3);[/code]

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 this post


Link to post
Share on other sites
Zahlman    1682
[quote name='QW3RTY ROCKS' timestamp='1312028655' post='4842504']
Hey well i fixed my problem. :lol:

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

[code]terrain1.resize(map_height*map_width);[/code]

to

[code]terrain1.resize(map_height*map_width+3);[/code]
[/quote]

This is [b]not a correct fix for the problem[/b].

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 this post


Link to post
Share on other sites
Trienco    2555
[quote name='QW3RTY ROCKS' timestamp='1312078491' post='4842705']
Well you see the +3 was because +1 did not work.
[/quote]

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.

Is your loadMap still looking like this?
[code]
int i;
for(int y=0;y<map_height;y++){
for(int x=0;x<map_width;x++){
i++;
terrain1[i].x = x;
terrain1[i].y = y;
fscanf(input_file, "%d", &terrain1[i].type);
}
}

[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this