I'm starting to work on a small project to test my knowledge and I came across something odd and wouldn't mind having whatever is happening explained to me.
First off my code:
#include <iostream>
#include<string>
#include<ctime>
#include<cstdlib>
using namespace std;
void mazeLayout(int* p_height, int* p_width, int* p_seed, string* p_curPath){
int start;
int stop;
int temp = *p_seed;
cout << temp << "\n";
while(temp > *p_width || temp > *p_height || temp <= 0){
if(temp <= 0){
temp = temp+8;
}
temp = temp-7;
cout << temp << "\n";
}
};
int main()
{
int width;
int *p_width = &width;
int height;
int *p_height = &height;
int seed;
int *p_seed = &seed;
string curPath;
string *p_curPath = &curPath;
int **maze;
srand(time(NULL));
seed = rand()+8*24;
seed = seed%50;
cout << "Welcome to the maze generator\n\n";
cout << "Width: ";
cin >> width;
cout << "Height: ";
cin >> height;
maze = new int* [height];
for(int i = 0; i < width; i++){
maze[i] = new int[width];
}
mazeLayout(p_height, p_width, p_seed, p_curPath);
for(int i = 0; i < width; i++ ){
delete[] maze[i];
}
delete[] maze;
}
It may be sloppy and as I had pointed out to me in a previous post I should probably be working with vector
rather than pointers if I'm just a beginner, I'm not looking for critiques on that currently(they are welcome though).
Anyway, back to my point. When I run this and plug in a value for width
that is > 10 and a value for height
that is between 5 & 8 I get an odd output.
Multiple times it prints out something like:
once 007417B8 is 6584960
and then the program ends giving me the signal that everything ran fine with no errors.
Any other combinations of numbers results in a normal outcome(outside of a combination such as 14 & 3 which results in a crash due to a bad memory address)
Can someone tell me what exactly is happening here?