Yes. The i variable would be automatically destroyed when at the end of the function. This has nothing to do with pointers but with the lifetime of such variables.Originally Posted by Ryan0773
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i was just wondering how to add body to a code, like how could i add body to a switch case like this:
The program compiles but it doesn't run ( this is the script from the tut, by the way). The tutorials said that it wouldn't run because the functions dont have bodies. It's told me to add 'if' functions (i think thats what it was asking, that or it was comparing) I was just wondering how to give the functions bodies. I tried just simply adding a cout<< but that din't work. What would i need to do to make the program run?Code:int input; int* p; //not sure if the pointer would en up looping it but i just added this to give p = &input; //it a try if the program would run. cout<<"1. Play Game\n"; cout<<"2. Load Game\n"; cout<<"3. Play Multiplayer\n"; cout<<"4. Exit\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: playgame(); break; case 2: loadgame(); break; case 3: playmultiplayer(); break; case 4: cout<<"Thank you for playing!\n"; break; default: cout<<"Error, bad input.\n"; cout<< *p <<"\n"; break;
i is a local variable which would fall out of scope when the function returns, the pointer has nothing to do with it.
p = new int(x);
p = &x;
// ...
delete p;
That might not match the code you gave us but it doesn't matter, the problem is the same. The error here is two-fold:
a) the memory returned by new is an inaccessible object, thanks to the subsequent reassignment. This is called a memory leak, because you won't get that memory back until the program terminates.
b) delete is a mistake. p points to the x variable, which would fall out of scope on it's own.
When pointers point to something returned by new or new [] you should clean up those objects with a matching delete/delete [] before reassigning the pointer or before the pointer variable falls out of scope.
The added delete doesn't really seem to have any effect with the outcome but i guesse it's better safe then sorry. Thanks for the tip. So something like this?
Code:int i(25 + 4); int x(4); int *p; p = &i; cout<< *p <<"\n"; cout<< *p <<"\n"; delete p; p = new int(x); p = &x; cout<< *p <<"\n"; delete p; p = 0; cin.get();
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ao my previous code was alot better... Then what needs to be fixed???!!!
(Previouse code)
Code:#include <iostream> using namespace std; int main() { int i(25 + 4); int x(4); int *p; p = &i; cout<< *p <<"\n"; cout<< *p <<"\n"; p = new int(x); p = &x; cout<< *p <<"\n"; delete p; p = 0; cin.get(); }
Why is this hard?
Code:int i = 29; int x = 4; int *p; p = &i; cout << *p << "\n"; p = new int(x); cout << *p << "\n"; delete p; //done with p cin.get();
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
THATS EXACLTY WHAT I WROTE BEFORE!!!!!! (well not when i showed the "previouse code", i made a mistake, i forgot to take out the p = &x) I wrote that in my code then you sayd that i wrote it wrongand that i needed to add a delete somwhere. I added the extra cout<< as a little test, I just wanted to see that the pointer was working. I put the delete in the wrong place so someone else told me not to put a delete where i had placed it and that it was dangerous to delete something that wasn't assigned with new. It's not hard its just i have no friggin clue as to what you guys are trying to say!!!
You told me that the delete was a mistake, but then you showed me this code which is exactly what i did before that you said was wrong!
I dont need any slack, he wrote the exact same code that i had before! he told me that i needed to do something to delete, but the delete that i had was in the right place so i thought he meant something else. So i added that extra delete which was wrong because the int that i used it with was not associated with new. then he writes the exact same code that i had written before but without one of the cout<< functions. Technically he didn't hand me anything because i was right the whole time!
It certainly isn't EXACTLY what you posted - twice you posted code containing delete of variables that are not created by new - which is what I was objecting to.
And all local variables, whether pointers or otherwise, get destroyed at the end of the block they are in [although the actual space (on the stack) used by such variables may not get freed up until at the end of the function]. These variables are not destroyed with delete, they get destroyed by the compiler.
If you use new, you need to:
1. Make sure that you do not "loose hold of" the pointer you get back from new.
2. Use delete to free exactly the same pointer that you got back from new [by "the same pointer, it needs to have the same value, not necessarily THE VERY SAME VARIABLE - you can shuffle the pointer itself around between however many variables you like - as long as the call to delete is done with a pointer of the same type and with the same value].
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If you had read the other threads you would have noticed that laserlight had corrected me on the p = &x. I added the p = 0 because the tutorials said that making it a null pointer was a good idea. When i reposted the final code i had forgotten to take out the p = &x which i pointed out in my response to whiteflag I'm sorry, is almost completely my fault. I agree that i am a noob and that i sometimes have no idea what the heck im, or what anyone else is talking about. For example, what do you mean by "lose hold of" the pointer?