![]() |
| | #46 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
Code: p = &i; | |
| Ryan0773 is offline | |
| | #47 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #48 |
| C++ noob Join Date: Jan 2009
Posts: 43
| When would the i variable be destroyed. would it be destroyed when p was assigned to a new variable? |
| Ryan0773 is offline | |
| | #49 |
| C++ noob Join Date: Jan 2009
Posts: 43
| i was just wondering how to add body to a code, like how could i add body to a switch case like this: 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;
|
| Ryan0773 is offline | |
| | #50 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| 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.
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #51 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
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();
| |
| Ryan0773 is offline | |
| | #52 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
Also bad, because you just lost the pointer to the memory you allocated with new - and you are then deleting the pointer that points to local variable x. -- Mats
__________________ 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. | |
| matsp is offline | |
| | #53 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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();
}
|
| Ryan0773 is offline | |
| | #54 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| 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();
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
| | #55 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ 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. | |
| matsp is offline | |
| | #56 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
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! | |
| Ryan0773 is offline | |
| | #57 |
| Registered User Join Date: Oct 2006
Posts: 263
| |
| Elkvis is offline | |
| | #58 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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! |
| Ryan0773 is offline | |
| | #59 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #60 |
| C++ noob Join Date: Jan 2009
Posts: 43
| 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? |
| Ryan0773 is offline | |
![]() |
| Tags |
| basic, basics, c++, lots, noob |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Several Questions, main one is about protected memory | Tron 9000 | C Programming | 3 | 06-02-2005 07:42 AM |
| Trivial questions - what to do? | Aerie | A Brief History of Cprogramming.com | 23 | 12-26-2004 09:44 AM |
| Questions. | anonytmouse | A Brief History of Cprogramming.com | 4 | 05-19-2004 02:16 PM |
| C++ test questions | Mister C | C++ Programming | 9 | 09-08-2002 12:05 PM |