![]() |
| | #31 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
The second mistake will be pointed out by the compiler. EDIT: Okay, there is a fourth mistake: x is not initialised either.
__________________ 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 | |
| | #32 |
| C++ noob Join Date: Jan 2009
Posts: 43
| |
| Ryan0773 is offline | |
| | #33 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
Quote:
Code: int i = 123;
__________________ 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 | |
| | #34 |
| C++ noob Join Date: Jan 2009
Posts: 43
| How could i fix the problem of redeclaration? When the compiler tells me thats the problem it doesn't have a suggestion that could fix it. Well i guess they were garbage values (if that means that they didn't have any value) but that was just an example, i guess this is what it would look like with values: 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";
int *p = new int(x);
p = &x;
cout<< *p <<"\n";
delete p;
p = 0;
cin.get();
}
PS: You told me there were four error, the redeclaration, and the two garbage values...whats the last one? Nvm, the last one was the p = 0 thing Last edited by Ryan0773; 01-04-2009 at 01:29 PM. Reason: Oops |
| Ryan0773 is offline | |
| | #35 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
Code: int *p = new int(x); Code: p = new int(x); Quote:
However, there is another mistake: Code: p = &x;
__________________ 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 | |
| | #36 |
| C++ noob Join Date: Jan 2009
Posts: 43
| It worked, thanks alot! I still dont fully understand why a pointer would be needed (can they copy large amounts of code or just one simple integer?)but at least i know how to work them. Again thanks alot! Ill be sure to try and get through the other tutorials! |
| Ryan0773 is offline | |
| | #37 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
Quote:
![]() Quote:
__________________ 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 | |
| | #38 | |
| 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);
p = &x;
cout<< *p <<"\n";
delete p;
p = 0;
cin.get();
}
What do you mean by cheap? | |
| Ryan0773 is offline | |
| | #39 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| There is still that last mistake. This: Code: p = new int(x); p = &x; cout<< *p <<"\n"; Code: p = new int(x); cout<< *p <<"\n"; Quote:
__________________ 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 | |
| | #40 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
Thanks again for your help! I still dont really understand what you mean by cheap, is it less work for the computer? Like a shortcut? Last edited by Ryan0773; 01-04-2009 at 06:57 PM. Reason: Forgot something | |
| Ryan0773 is offline | |
| | #41 | |
| verbose cat Join Date: Jun 2003
Posts: 209
| Quote:
The way variables are passed to functions, a copy is made of what you pass. So, for example, when you pass a simple int to a function, the program makes a copy of that int for the function to use. If you want to pass one of the 3d objects, it would make a copy of all that data, which could end up being several thousand bytes of data. An alternative is to use a pointer to the object to tell the function where to look for the data. So now the program makes a copy of the pointer for the function to use, and then the function looks at the data that pointer is pointing to. Instead of making a copy of hundreds or thousands of bytes of data, it only has to copy a single pointer and the program is much quicker, especially if you are drawing a 3d scene with dozens of 3d objects having to be passed to the display function several times per second. With a pointer now, the program is working on processing your data rather than spending time copying it all over the place too. In C++ we prefer to use references, but there are still times when pointers are needed. Under the hood, a reference is doing almost the same thing that a pointer does, but the program takes care of most of the dereferencing and other things that make pointers complicated so you can focus on making the program do what you want.
__________________ abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments " | |
| jEssYcAt is offline | |
| | #42 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
__________________ 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 | |
| | #43 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Perhaps have a look at: http://cslibrary.stanford.edu/104/ It's quite silly, but it explains the basics. Also, the tutorial poisons your mind. I don't know why they insist on using T * in C++, but know that it is possible to write T*, as well. Instead of: int *p You can write: int* p The later reinforces that the actual type of the variable is int*. If you dereference it, you get an int, but the pointer itself has a type, which is important. You must also learn the difference between a value and an address. Pointers store addresses. Dereferencing addresses (ie pointers), you get the values. Everything allocated by new must be freed with delete. It is not automatic. If you assign a new address to a pointer which carries an address from new, that address is lost forever and you cannot free the memory you allocated. That memory is now lost. It will still be there in the program, taking up space, without the ability of being used. It's a memory leak. Here are a few uses of pointers: - Dynamic memory allocation. new returns a pointer to the memory allocated (this is usually rare in C++, however, as std::vector & co is typically used for dynamic memory). - Passing large objects around without copying (although, in C++, typically references are used for this). - Allow multiple parts of your program to access the same data everywhere, without copying (then it would be multiple instances of that data). - Allow multiple parts of your program to change some shared data and make sure all other parts get the new data (since all parts point to a single source of the data, if the data is then changed...).
__________________ 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 | |
| | #44 | |
| C++ noob Join Date: Jan 2009
Posts: 43
| Quote:
| |
| Ryan0773 is offline | |
| | #45 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,354
| Quote:
__________________ 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 | |
![]() |
| 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 |