![]() |
| | #61 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: int *p = new int(42); ... // no line doing "delete p" here... p = new int(17); [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 | |
| | #62 |
| C++ noob 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();
}
|
| Ryan0773 is offline | |
| | #63 | |
| 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 | |
| | #64 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
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 | |
| | #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 | |
| | #66 | ||
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Quote:
Quote:
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 | |
| | #67 | ||
| C++ noob Join Date: Jan 2009
Posts: 43
| 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:
Quote:
Thanks alot for being patient and sorry i got ticked off at you. | ||
| Ryan0773 is offline | |
| | #68 |
| C++ noob 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; |
| Ryan0773 is offline | |
| | #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 | |
| | #70 |
| C++ noob Join Date: Jan 2009
Posts: 43
| |
| Ryan0773 is offline | |
| | #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 | |
| | #72 |
| C++ noob Join Date: Jan 2009
Posts: 43
| Alright, i get it, thanks! |
| Ryan0773 is offline | |
| | #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 | |
| | #74 |
| C++ noob 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 | |
| | #75 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Solution 1: Code: bool flag = true;
while (!flag)
{
switch (...)
{
// ...
default:
flag = false;
}
}
Code: while (...)
{
switch (...)
{
// ...
default:
continue;
}
}
__________________ 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:
| |
| Elysia 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 |