Lot of questions.

This is a discussion on Lot of questions. within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight It is a major mistake. By assigning the address of x to p, p now points ...

  1. #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?

  2. #47
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #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?

  4. #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?

  5. #50
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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.

  6. #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();

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  8. #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();
    }

  9. #54
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    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();

  10. #55
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  11. #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!

  12. #57
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    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.

  13. #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!

  14. #59
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  15. #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?

Page 4 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  3. Questions.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2004, 02:16 PM
  4. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM

Tags for this Thread


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