I am creating an object called "ID1000" of type "Maps". The object contains a member, "Platforms[][]". Platforms[][] is a two-dimensional array.
class Maps{
int Platforms[][4]
} ID1000;
ID1000.Platforms[0][0] = 100;
Here, I set Platforms[0][0] to 100. However, when I execute this line of code...
al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "TargetY: %f", 800 - TargetY);
object_name[0][0] suddenly changes from 100 to 1065353216. This line is simply a function of the 2d graphics library Allegro that draws text onto the window. I cannot for the life of me figure out why it is at all related to my member's value.
Please, if you have any ideas or suggestions, I would appreciate it.
Here is the full program...
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <math.h>
#include <iostream>
const int WIDTH = 1000;
const int HEIGHT = 800;
class Maps {
public:
int NumPlat;
int MapWidth;
int Platforms[][4];
};
int main(int argc, char **argv)
{
Maps ID1000;
ID1000.Platforms[0][0] = 100;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font18;
float TargetY = 0;
if(!al_init())
return -1;
display = al_create_display(WIDTH, HEIGHT);
if(!display)
return -1;
al_install_keyboard();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
font18 = al_load_font("BALTH___.ttf", 18, 0);
std::cout << ID1000.Platforms[0][0];
al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "TargetY: %f", 800 - TargetY); //display horizontal position on screen
std::cout << ID1000.Platforms[0][0];
al_destroy_font(font18);
return 0;
}
The array int Platforms[][4]
is empty (it's an array of zero rows, 4 ints each). It is also invalid in a C++ program. (this is valid C, though, where it's called "flexible array element". The intended use is for you to over-allocate the object and address the extra memory after the object using array access syntax)
The memory location that you access when you access ID1000.Platforms[0][0]
(first int in the first row) is outside the array, and in fact outside the ID1000
object.
At this point you're in undefined behavior, anything can be happening. I would guess the memory location you're accessing is the one used by the pointer display
, where you write the result of al_create_display()
.