0

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?

  • 1
    What is the reason for declaring all of those pointers at the beginning of `main`? They don't serve any real purpose, except to get you into trouble that need not happen. It seems you're falling victim to "if the function requires a pointer, then I must declare a pointer." Wrong: `mazeLayout(&height,&width, &seed, &curPath);` – PaulMcKenzie May 02 '15 at 16:17
  • @PaulMcKenzie Yeah I guess that would be more effective, I'm honestly not even sure why I decided that'd be more effective than using a ref. – FutureWizard May 02 '15 at 16:22
  • The `&` in my comment is the *address-of* operator, not a reference. Yes, they share the same syntax, but one has nothing to do with the other. – PaulMcKenzie May 02 '15 at 16:24
  • Ahh, I see what you're saying now. The book I'm using didn't cover the fact that that was even a possibility! Thank you so much for pointing that out to me! – FutureWizard May 02 '15 at 16:28
  • @FutureWizard In such case, you might want to pick up a [better book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Angew is no longer proud of SO May 02 '15 at 17:09

2 Answers2

2

I think you require

maze = new int* [height];
for(int i = 0; i < height; i++){
//                   ^
//                   ^            
    maze[i] = new int[width];
}

as currently, you are using the value of width to loop over. If height is bigger than width, then this will result in unallocated arrays for some of the maze[i] in your code as it stands. Then attempts to access this unallocated memory later in code will result in undefined behaviour.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

You have a bug in the for loop:

maze = new int* [height];
for(int i = 0; i < width; i++){
    maze[i] = new int[width];
}

maze points to an array of height elements, but then your for loop goes up to width. If width is larger than height, you get Undefined Behaviour (probably writing into random memory), under which anything can happen.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455