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 Ryan0773 For example, what do you mean by "lose hold of" the pointer? Typically something like this: ...

  1. #61
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ryan0773 View Post
    For example, what do you mean by "lose hold of" the pointer?
    Typically something like this:
    Code:
    int *p = new int(42);
    ...  // no line doing "delete p" here... 
    p = new int(17);
    The red line looses the pointer you got back in the blue line - so the memory you got back from the blue line can not be freed, because you no longer hold a pointer to it.

    [The same would happen if p is assigned 0, or &x, or any other change of the value of p].

    --
    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.

  2. #62
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    But is my code correct now?

    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";
        delete p;
        p = new int(x);
        cout<< *p <<"\n";
        delete p;
        p = 0;
        cin.get();
    }
    This is the code that i had finished before, i forgot to take out the p = &x before so that was a mistake. I made p = 0 as a precaution.

  3. #63
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ryan0773 View Post
    But is my code correct now?

    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";
        delete p;
        p = new int(x);
        cout<< *p <<"\n";
        delete p;
        p = 0;
        cin.get();
    }
    This is the code that i had finished before, i forgot to take out the p = &x before so that was a mistake. I made p = 0 as a precaution.
    The red line should not be there, as p is not pointing t memory from "new", but a local variable in your function.

    --
    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.

  4. #64
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by matsp View Post
    The red line should not be there, as p is not pointing t memory from "new", but a local variable in your function.

    --
    Mats
    srry, forgot to fix that should be:

    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);
        cout<< *p <<"\n";
        delete p;
        p = 0;
        cin.get();
    }

  5. #65
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that is good.

    --
    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.

  6. #66
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    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.
    Don't give me that crap. There is no point in banging your head against a wall forever. When several people have chimed in about an issue and the poster still doesn't get it, you need to make a judgment call.

    Quote Originally Posted by Ryan0773 View Post
    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?
    I didn't mean to be short with you, you probably read to much into it.

    matsp's post should be pretty clear. The part you are confused about is about how new returns the value you need to follow with delete. If you reassign the pointer before that you "lose hold of" the pointer. Anyway, I think your level of understanding is fine, but if it's still really confusing then just watch Binky. I think someone posted it before now but a second recommendation never hurt.

  7. #67
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by matsp View Post
    Yes, that is good.

    --
    Mats
    ok thanks, I'm really sorry i got ticked at you, i thought that i ahd fixed the problems but i overlooked them, hehe,twice. Anyways thanks for the help!!!

    Quote Originally Posted by whiteflags View Post
    Don't give me that crap. There is no point in banging your head against a wall forever. When several people have chimed in about an issue and the poster still doesn't get it, you need to make a judgment call.
    I agree with you 100%, only problem is, you chimed up the wall and passed me the rope, but i havent learned how to climb yet (metaphores are awesome(not sure if metaphore is the right word...))


    Quote Originally Posted by whiteflags View Post
    matsp's post should be pretty clear. The part you are confused about is about how new returns the value you need to follow with delete. If you reassign the pointer before that you "lose hold of" the pointer. Anyway, I think your level of understanding is fine, but if it's still really confusing then just watch Binky. I think someone posted it before now but a second recommendation never hurt.
    Someone did recommend it before, it's kindof wierd how the guy is teaching people how to use c++ with creepy clay things but what the heck, it works. I dont think i could have lost hold of a pointer in my code, from the example mats showed me, i think that can only happen if you forget to free a pointer that is associated with new using delete. Unless i accidentaly lost i in my code without realising it...

    Thanks alot for being patient and sorry i got ticked off at you.

  8. #68
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Just one more thing. In the binky video it shows how a pointer works. but when it makes y point to the same thing as x using this:
    Code:
    int* x;
    int* y;
    
    x = new int;
    *x = 42;
    y = x;
    *y = 13;
    Since y is equal to 13 and it points to the same thing as x, because y was made after and the program is told that y = x makes it show x as 13 when we run the program, but what happenned to the 42? was it erased?

  9. #69
    Registered User
    Join Date
    May 2006
    Posts
    903
    More or less. You could represent it this way (though it is not exactly what happens):

    int x = 42;
    x = 13;

  10. #70
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by Desolation View Post
    More or less. You could represent it this way (though it is not exactly what happens):

    int x = 42;
    x = 13;
    I still dont understand why we make x = 42 in the first place and i still dont know what happens to the 42

  11. #71
    Registered User
    Join Date
    May 2006
    Posts
    903
    It was only an example to show how y will modify the value at the address pointed by x. There's no real purpose to all of this and nothing "happens" to 42, you are really only replacing whatever was stored in the address pointed to by x. In this case, the address pointed to by x holds 42 and this value is changed to 13 through y.

  12. #72
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Alright, i get it, thanks!

  13. #73
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, the values 42 and 13 are "numeric literals" or "constants" - they are just values that will be stored in the location that corresponds to the variable x, or the memory location held in x when x is a pointer (and y).

    Also, if you have an alias (two pointers pointing to the same location in memory) as you have above, then both values will change when you store something through the pointer.

    --
    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.

  14. #74
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    I have been trying to loop a switch case code so that when the default is activated, it loops back to the begginning of the switch case. Unfortunately, when i tried to construct a loop that would do this it didn't work, as a matter of fact, it failed epicly. I tried to make a for while loop that when default was chosen (or when the number input was more than that of the max number of cases) it would loop back to the beginning. I also tried using a pointer and well...that didn't work.

    The tutorials on looping are all about looping integers, one of them was close (do...while) but that wouldn't really help either. I want to know how i can make it loop back tot the begginning of the selection when default is activated.

  15. #75
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Solution 1:
    Code:
    bool flag = true;
    while (!flag)
    {
        switch (...)
        {
            // ...
            default:
                flag = false;
        }
    }
    Solution 2:
    Code:
    while (...)
    {
        switch (...)
        {
            // ...
            default:
                continue;
        }
    }
    It is not at all that hard as you make it out to be. Learn how to use flowcharts and pseudo code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 5 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