C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-05-2009, 02:40 PM   #46
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
It is a major mistake. By assigning the address of x to p, p now points to an object that will be automatically destroyed. As such, delete p; becomes a mistake. Furthermore, the object that was created on the previous line cannot be destroyed because there is no way to access it.
In the code though the first one needs to be there right? where it says:

Code:
p = &i;
Does that need to be there or would thta delete the i later?
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 02:46 PM   #47
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by Ryan0773
In the code though the first one needs to be there right?
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.
__________________
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   Reply With Quote
Old 01-05-2009, 03:55 PM   #48
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by laserlight View Post
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.
When would the i variable be destroyed. would it be destroyed when p was assigned to a new variable?
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 04:38 PM   #49
C++ noob
 
Ryan0773's Avatar
 
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;
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?
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 04:38 PM   #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   Reply With Quote
Old 01-05-2009, 05:00 PM   #51
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Quote:
Originally Posted by whiteflags View Post
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();
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 05:06 PM   #52
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Ryan0773 View Post
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();
Definitely BAD - do not EVER delete memory that was not allocated with new.
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   Reply With Quote
Old 01-05-2009, 05:08 PM   #53
C++ noob
 
Ryan0773's Avatar
 
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   Reply With Quote
Old 01-05-2009, 05:38 PM   #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   Reply With Quote
Old 01-05-2009, 05:48 PM   #55
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Ryan0773 View Post
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();
}
The red line overwrites the result of the blue line, and then you free the address of x, a local variable which, not gotten from new, so thus undefined behaviour [and that is about as good as driving across a road when the lights show red]...

--
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   Reply With Quote
Old 01-05-2009, 05:48 PM   #56
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Exclamation

Quote:
Originally Posted by whiteflags View Post
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();
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!
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 05:54 PM   #57
Registered User
 
Join Date: Oct 2006
Posts: 263
Quote:
Originally Posted by whiteflags View Post
Why is this hard?
he's a noob... try to cut him a bit of slack. the idea was to GUIDE him in the right direction so that he would come up with the code that you wrote on his own. Now you just handed it to him and he learns nothing.
Elkvis is offline   Reply With Quote
Old 01-05-2009, 05:57 PM   #58
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Exclamation

Quote:
Originally Posted by Elkvis View Post
he's a noob... try to cut him a bit of slack. the idea was to GUIDE him in the right direction so that he would come up with the code that you wrote on his own. Now you just handed it to him and he learns nothing.
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   Reply With Quote
Old 01-05-2009, 05:58 PM   #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   Reply With Quote
Old 01-05-2009, 06:17 PM   #60
C++ noob
 
Ryan0773's Avatar
 
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   Reply With Quote
Reply

Tags
basic, basics, c++, lots, noob

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:18 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22