This causes a segfault
#include <iostream>
using namespace std;
const int WORLDHEIGHT = 20;
const int WORLDWIDTH = 80;
class world{
public:
char worldGrid[WORLDHEIGHT][WORLDWIDTH];
world();
void updateWorldGrid(){
for (int i = 0; i < WORLDHEIGHT; i++){
for (int j = 0; i < WORLDWIDTH; i++){
cout << worldGrid[i][j];
}
cout << '\n';
}
}
};
world::world(){
for (int i = 0; i < WORLDHEIGHT; i++){ for (int j = 0; i < WORLDWIDTH; i++) worldGrid[i][j] = ' ';}
}
int main(){
world sampleWorld;
sampleWorld.updateWorldGrid();
return 0;
}
Both valgrind and gdb say that I have problems at world::world()
but I really don't know what is the problem there. It's probably a noob question but it's grinding my gears. Oh and it also doesn't do what I asked it(segfaults are kinda random).
Do I need to allocate the array manually? Thanks to anyone for the help in advance.
EDIT: I made a mistake when incrementing in my second loops in both word::word()
and updateWorldGrid()
thanks to @DevBlox and @sheepy for showing how blind I am :)