C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 01-05-2009, 06:21 PM   #61
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 01-05-2009, 06:25 PM   #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.
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 06:26 PM   #63
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 01-05-2009, 06:28 PM   #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();
}
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 06:29 PM   #65
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 01-05-2009, 06:38 PM   #66
Registered User
 
Join Date: Apr 2006
Location: United States
Posts: 3,201
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.
__________________
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, 06:56 PM   #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.
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 07:22 PM   #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?
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 07:25 PM   #69
Registered User
 
Join Date: May 2006
Posts: 894
More or less. You could represent it this way (though it is not exactly what happens):

int x = 42;
x = 13;
Desolation is offline   Reply With Quote
Old 01-05-2009, 07:27 PM   #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
Ryan0773 is offline   Reply With Quote
Old 01-05-2009, 07:29 PM   #71
Registered User
 
Join Date: May 2006
Posts: 894
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.
Desolation is offline   Reply With Quote
Old 01-05-2009, 07:35 PM   #72
C++ noob
 
Ryan0773's Avatar
 
Join Date: Jan 2009
Posts: 43
Alright, i get it, thanks!
Ryan0773 is offline   Reply With Quote
Old 01-06-2009, 02:57 AM   #73
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 01-07-2009, 07:39 PM   #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.
Ryan0773 is offline   Reply With Quote
Old 01-08-2009, 02:28 AM   #75
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia 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:36 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