No Error returned using pointers

I've been trying to make a text-based game and I'm constantly getting no execution error and no compilation errors. Everything's returning 0.

I have a player obj already created in a class so I will leave out that large boring snippet of code. All i've been trying to do is use a function that should be running from what I see (displayMenu). I know compilers read top to bottom so how come Im not getting a single stitch of input back?

source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
void createPlayer() {} 
/* for later 
void clean(Player *p) {
    p = NULL; 
    delete p; 
} 
*/ 
void levelOne(Player *p) {
    std::cout << "Welcome to the game " << p->getID() << '\n';
    //clean(&p);  
} 
void levelTwo(Player *p) {}  
void startGame() {} 
void howToPlay() {} 
void displayMenu() {
    int userChoice = 0; 
    
    std::cout << "1. Start Game\n"; 
    std::cout << "2.How to Play\n"; 
    std::cout << "3.Exit game\n";
    std::cin >> userChoice; 
    switch(userChoice)
    {
        case 1: 
            startGame(); 
            break; 
        case 2:
            howToPlay(); 
            break; 
        case 3: 
            std::cout << "Game exited, have a nice day!\n"; 
            clean(); 
            break; 
        default: 
            std::cout << "Function displayMenu() did nothing\n"; 
            break; 
    } 
} 
 

int main() 
{ 
    Player newPlayer; 
    displayMenu();  
        
    return 0; 
} 


Thanks if you can help me here :) I just wanna add that the Player class isn't the issue because I tested that and it works fine.
Last edited on
I don't see anything in the code you've posted that might cause anomalous behavior, although I'll say that clean() makes no sense.
@helios Im deleting the pointer I used for level one, each level I pass the Player object so I know whose playing the level and their attributes. When I finish the level I wanna delete the pointer and since I have multiple level it became something repetitive in levels so I made it a function to call and you pass your pointer through and delete it. And yea Im not using a proper IDE with a good compiler so when I get home Ill throw it in VS and see what I get. For now thats what Im doing but Im going to make a destructor for it
Last edited on
I guess my compiler really is just trash, after hitting compile like a million times it decided to work and everything's working ! Sorry to bug u guys enjoy the weekend :)
Im deleting the pointer
No, you're not. You're first leaking the pointer and then deleting a null pointer (which has no effect).
You probably meant this:
1
2
3
4
void clean(Player *p) {
    delete p;
    p = NULL;
}
Ah okay, can you explain in more detail why having p = NULL; first leaks it? Thanks :) It just made sense to me as a noob to set the pointer to null before I delete it for extra saftey. Would it be more logical to simply delete the pointer and not include the NULL statement?
Last edited on
If you null before deleting, how do you know what to delete?
Imagine I give you my phone number, but before I leave I take the paper I gave you and burn it, and tell you "don't forget to call me!" Are you going to be able to call me?

In that particular case yes, nulling the pointer makes no difference, because you're only nulling the local copy in the the clean() function. When clean() returns that pointer disappears anyway. In fact, I would not be surprised if the compiler optimizes that assignment away.
Than you for the reply:) interesting idea how do you think I should delete the passed pointer object without it being a local thing.should I be deleting the reference of the true pointer ?
Last edited on
1
2
3
4
void clean(Player *&p){
    delete p;
    p = nullptr;
}

Or better yet, don't use naked pointers at all.
1
2
3
4
5
std::unique_ptr<Player> p(new Player);
p.reset();
//p is now null.
p.reset(new Player);
//p is now non-null. 
Awesome thanks man :) I like your c++11 "std::unique_ptr" much better and I'm gonna learn about it tonight.
Last edited on
Topic archived. No new replies allowed.