#include <iostream>
#include <fstream>
#include <string>
struct TextGrid {
char **tiles;
unsigned int height;
unsigned int width;
};
void paint(TextGrid& grid, unsigned int x, unsigned int y, char fill) {
if (x < grid.width && y < grid.height) {
grid.tiles[y][x] = fill;
}
}
void resize(TextGrid& grid, unsigned int width, unsigned int height, char fill) {
char **newTiles = new char*[height];
for (unsigned int i = 0; i < height; ++i) {
newTiles[i] = new char[width];
for (unsigned int j = 0; j < width; ++j) {
if (i < grid.height && j < grid.width) {
newTiles[i][j] = grid.tiles[i][j];
}
else {
newTiles[i][j] = fill;
}
}
}
for (unsigned int i = 0; i < grid.height; ++i) {
delete[] grid.tiles[i];
}
delete[] grid.tiles;
grid.tiles = newTiles;
grid.width = width;
grid.height = height;
}
void transform(TextGrid& grid, char target, char replacement) {
for (unsigned int i = 0; i < grid.height; ++i) {
for (unsigned int j = 0; j < grid.width; ++j) {
if (grid.tiles[i][j] == target) {
grid.tiles[i][j] = replacement;
}
}
}
}
TextGrid load_grid(std::string file_path) {
std::ifstream infile(file_path);
std::string line;
std::getline(infile, line);
unsigned int height = 1;
unsigned int width = line.length();
while (std::getline(infile, line)) {
++height;
}
infile.clear();
infile.seekg(0, std::ios::beg);
TextGrid grid;
grid.tiles = new char*[height];
grid.height = height;
grid.width = width;
for (unsigned int i = 0; i < height; ++i) {
grid.tiles[i] = new char[width];
std::getline(infile, line);
for (unsigned int j = 0; j < width; ++j) {
grid.tiles[i][j] = line[j];
}
}
infile.close();
return grid;
}
void save_grid(TextGrid& grid, std::string file_path) {
std::ofstream outfile(file_path);
for (unsigned int i = 0; i < grid.height; ++i) {
for (unsigned int j = 0; j < grid.width; ++j) {
outfile << grid.tiles[i][j];
}
outfile << '\n';
}
outfile.close();
}
void show(TextGrid& grid) {
for (unsigned int i = 0; i < grid.height; ++i) {
for (unsigned int j = 0; j < grid.width; ++j) {
std::cout << grid.tiles[i][j];
}
std::cout << '\n';
}
}
int main() {
TextGrid grid;
grid.width = 16;
grid.height = 8;
grid.tiles = new char*[grid.height];
for (unsigned int i = 0; i < grid.height; ++i) {
grid.tiles[i] = new char[grid.width];
for (unsigned int j = 0; j < grid.width; ++j) {
grid.tiles[i][j] = ' ';
}
paint(grid, 5, 2, 'X');
}
resize(grid, 20, 10, '.');
transform(grid, 'X', 'O');
save_grid(grid, "output.txt");
TextGrid loaded_grid = load_grid("output.txt");
show(loaded_grid);
for (unsigned int i = 0; i < grid.height; ++i) {
delete[] grid.tiles[i];
}
delete[] grid.tiles;
for (unsigned int i = 0; i < loaded_grid.height; ++i) {
delete[] loaded_grid.tiles[i];
}
delete[] loaded_grid.tiles;
return 0;
}
I keep getting error message Segmentation fault (core dumped). I tested the program through valgrind and got an error that there was use of an uninitialized value and and Invalid write of size 1 in the paint function. I cant figure out what the problem is. There are no memory leaks that valgrind has detected. the uninitialized value error occurred at line 4023AF in the "paint" function, and it involves a value of size 8. The Invalid write of size 1 also occurred at line 4023AF in the "paint" function. The program attempted to write a value of size 1 to memory at address 0x5.