![]() |
|
|
#1 |
![]() |
C++ programming
Hi all!
I have been reading a book about C++ and i have a very important question. I just finished the chapter of pointers, but i didn't get the idea on how to use them, in fact i didnt even make a single program, just kept on reading. (I made a lot of programs, but not advanced programs like the ones in the book) The question is: Keep on reading until i finish the book or || Perfect pointers before going any further! Also, the chapter was really difficult because it was using pointers with arrays and structs. I got the idea of memory allocation though which for the book was most difficult. Any suggestion? [Never read a book before with this kind of pleasure ]
|
|
|
|
|
|
#2 |
![]() |
Right so lets see if I can help here.
So a pointer can be considered as a variable but it is one that deals in memory addresses. The whole idea of a pointer is that it points to something in memory by storing its memory location in the actual pointer. Generally a pointer takes about 32bits of memory which is always constant but it can point to anything, which can take up as much memory as possible but the point is that the amount required by a pointer is constant. So a pointer may be declared like so: Code:
int *p_myPointer; Now, you'll run into many problems if you try to access the above pointer. Why? Because its uninitialized and doesn't have a value. Say I had an int with a value, you can give the pointer its memory address like so (assuming you have already declared it): Code:
int myScore = 50; p_myPointer = &myScore; Code:
//Print to console cout << "My score is: " << *p_myPointer << endl; //Change value *p_myPointer = 10; //Reprint value cout << "My score now is: " << *p_myPointer << endl; Now you can also create pointers that don't take a memory address specifically from another variable/object, but that creates variables that are the same type that the pointer points to using dynamic memory. They perform the same as an ordinary variable/object but the syntax for doing things may be different (like using the derefrerencing opperator). E.g: Code:
int *p_aNewPtr = new int; Code:
*p_aNewPtr = 690; Code:
int *p_aNewPtr = new int[arraySize]; Code:
p_aNewPtr[0] = 3; Code:
delete p_aNewPtr; Code:
delete [] p_aNewPtr; |
|
|
|
| The Following 2 Users Say Thank You to Relinquish For This Useful Post: |
|
|
#3 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Stop and learn pointers. It is key to understanding C++ code as they are used all of the time.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#4 |
![]() Join Date: Sep 2008
Location: VA/PA
Posts: 500 (0.29/day)
Thanks: 139
Thanked 164 Times in 121 Posts
|
This x1000000. They're incredibly important and powerful.
__________________
![]() Made by PVTCaboose1337 |
|
|
|
|
|
#5 |
![]() |
WOW
Thanks relinquish ![]() Looks like i will have to learn them a lot. Also, there are the struct pointers which made me a bit of unbalanced! like: Code:
pt->name="Adam"; Last edited by Aleksander; Apr 18, 2012 at 06:27 AM. |
|
|
|
|
|
#6 |
|
Eligible for custom title
Join Date: Jun 2007
Location: C:\Program Files (x86)\Aphexdreamer\
Posts: 5,614 (2.59/day)
Thanks: 812
Thanked 888 Times in 674 Posts
|
From what I learned in my Intro Class, perhaps this will be a simpler to the point example for ya.
Lets say you have Code:
int aa = 7; //initializes aa to 7 int *aaPointer = &aa; ------------------------------------ Now if you were to do Code:
cout<<aaPointer; //You get the addres like 0029F989 Code:
cout<<*aaPointer; I'll add my classes PowerPoint on it for ya. Here it is: http://www.slideshare.net/gerfeso/unit-6-pointers
__________________
Sent from my PC using chrome. Last edited by AphexDreamer; Apr 18, 2012 at 06:44 AM. |
|
|
|
|
|
#7 |
![]() Join Date: Sep 2008
Location: Jerusalem, Israel
Posts: 2,121 (1.24/day)
Thanks: 213
Thanked 481 Times in 347 Posts
|
You gotta figure out pointers, as they are an integral part for C/Cpp programming. If you need some specific help, drop me a PM with specific questions and I'll do my best to answer.
__________________
Cameron: Core i7 2600K 4.5Ghz, MCR220, 2xMCR120, MCP655, ASRock P67 Extreme4, 4GB DDR3, 2xOCZ Vertex 30GB RAID0, GTX470, 2xHD5670, Modu82+ 625W, TT Xaser VI. Neuromancer: Core i7 975, DFI DK X58-T3eH6, 12GB DDR3 1333Mhz CL6-6-6-15-1T, 3x9600GSO 384MB, Hiper 880W, TT Xaser VI. Administrator of a 40 core Hadoop cluster. |
|
|
|
|
|
#8 | |
![]() |
Quote:
So the way I learnt what a structure was, it was that its like a way for a programmer to create their own variable type made up of the existing built in variable types and or possibly user defined objects and even other structures. So we may create a high score structure like so: Code:
struct highScore
{
string name;
int score;
};
Code:
highScore player1; Code:
player1.name = "Aleksander Dishnica"; player1.score = 500; Code:
highScore *p_player1 = new highScore; p_player1.name = "Aleksander Dishnica"; Code:
(*p_player1).name = "Aleksander Dishnica"; Code:
p_player1->name = "Aleksander Dishnica"; So to answer your last question. Pointers are important because they are fast! Especially as arguments to a function. |
|
|
|
|
| The Following User Says Thank You to Relinquish For This Useful Post: |
|
|
#9 |
![]() Join Date: Sep 2008
Location: VA/PA
Posts: 500 (0.29/day)
Thanks: 139
Thanked 164 Times in 121 Posts
|
Dynamic memory. I've use for linked lists and binary trees, among other things. I'm sure some of the more advanced/experienced c++ programmers here could give you more reasons.
__________________
![]() Made by PVTCaboose1337 |
|
|
|
|
|
#10 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Pointers are important for passing variable into methods (function) by reference instead of by value.
Since Relinquish is doing such a fine job with example, and I just got out of bed and my caffiene levels are low, I'll let him put up an example so it's correct.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#11 |
![]() |
LOL!
I was wondering... pointers are the same with java too? Or even same with all other languages that have pointers? Or not? |
|
|
|
|
|
#12 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
The concept of pointers is the same in all languages. Their syntax, implementation and restrictions can vary depending upon the language.
How's that for being crystal clear!
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following 2 Users Say Thank You to Kreij For This Useful Post: |
|
|
#13 |
![]() Join Date: Sep 2009
Location: Belgium
Posts: 135 (0.10/day)
Thanks: 2
Thanked 21 Times in 11 Posts
|
master the pointers and you'll master the c/c++ programming, my young padowan...
|
|
|
|
|
|
#14 |
![]() Join Date: Jul 2011
Location: Kaunas, Lithuania
Posts: 469 (0.70/day)
Thanks: 309
Thanked 202 Times in 122 Posts
|
From my own experience, I can say that the best way to learn using pointers is... to use pointers. Practice. Practice. Practice.
In other words - unlike many other topics, just reading about pointers doesn't help that much in learning them. To truly master pointers, use them in practice. While using them, try to think up and see as many possible uses for them as possible - as there are lots. Some that would probably never come to mind when using them just a little, let alone from simply reading a textbook. Pointers are a darn powerful and useful "breed of beasts", as long as their "trainer" knows how to "train these beasts to perform exquisite tricks". And now on the more humorous side: Pointers can help do a lot of things. Like: making a program run faster, make some forms of code easier to write, produce segfaults, reduce memory usage, segfaults, code obfuscation, working with bare hardware, segfaults, memory manipulation, did I mention segfaults? Yes, also helps to make segfaults, just to make one's day happier! Weee! (can't help not to also point out: This ^____^ ) Edit: And: Yeah, what stupido said... =3
__________________
Why do you wear glasses if you're deaf? Code:
while (1) {
alone();
}
Last edited by Vinska; Apr 26, 2012 at 07:49 AM. |
|
|
|
| The Following 2 Users Say Thank You to Vinska For This Useful Post: |
|
|
#15 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Here's a ittle to chew on Alek ...
There are three basic ways to pass arguments to functions. By value, by reference and by address. When you pass an argument by value, a copy is created within the scope of the function and destroyed when the function terminates. Code:
void foo (int a)
{
a++;
cout << a << endl;
}
int x = 1;
cout << x << endl;
foo(x);
cout << x << endl;
cout results...
x = 1
a = 2
x = 1
Code:
void foo (int &a)
{
a++;
cout << a << endl;
}
int x = 1;
cout << x << endl;
foo(x);
cout << x << endl;
cout results ...
x = 1
a = 2
x = 2
Code:
void foo (int *a)
{
*a = 2;
}
int x = 1;
cout << x << endl;
foo (&x)
cout << x << endl;
cout results ...
x = 1
x = 2
![]() @Vinska : You forgot to mention segfaults. lol
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
Last edited by Kreij; Apr 18, 2012 at 03:58 PM. |
|
|
|
| The Following 2 Users Say Thank You to Kreij For This Useful Post: |
|
|
#16 | |
![]() |
Quote:
Let me explain. If you pass by value, you are copying the contents of a variable to a temporary copy within a function. Depending on the size of that variable it can be a lengthy process. So something that's 24 bytes will take longer to copy than something that's 4 bytes. 4 bytes is quite small so when you pass by address using pointers, its quite quick. So when I say significant, I mean it in terms of performance. All I will say about pointers is don't get carried away with them. You can end up with some nasty memory leaks. Like someone else said, practice and practice and see when it is necessary to use them. |
|
|
|
|
| The Following User Says Thank You to Relinquish For This Useful Post: |
|
|
#17 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Relinq is right, it can be extrememly significant in terms of memory use (stack) and performance.
Here's an extreme (but pretty useless) example ... Code:
struct MyStruct
{
int a;
int b[1000000];
} myStruct;
void foo(MyStruct z, int b)
{
z.a = b;
}
foo (myStruct, 2);
Code:
void foo (Mystruct *z, int b)
{
z->a = b;
}
foo (&myStruct, 2);
(I think I got the syntax right. lol This is why I use C#. It makes my brain hurt less)
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
Last edited by Kreij; Apr 18, 2012 at 05:22 PM. |
|
|
|
|
|
#18 | |
![]() Join Date: Jul 2011
Location: Kaunas, Lithuania
Posts: 469 (0.70/day)
Thanks: 309
Thanked 202 Times in 122 Posts
|
Quote:
In the first example, after changing the field a, the change "stays" in the function - after the function finishes its execution, the said copy goes out of scope and is destroyed, leaving the value in the original copy that was passed to the function, unchanged. In the second one, since the function is then working with the "original" copy, any modifications stay. This is very useful, when one needs that a function would "permanently" modify a variable passed to it. P.S. Kreij, there was a small mistake in Your first example, so I edited it in the quote. I hope You don't mind. P.P.S Also, Kreij, I think there also is a mistake in Your second example. I also edited this one in the quite.
__________________
Why do you wear glasses if you're deaf? Code:
while (1) {
alone();
}
Last edited by Vinska; Apr 26, 2012 at 07:48 AM. |
|
|
|
|
| The Following User Says Thank You to Vinska For This Useful Post: |
|
|
#19 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Thanks Vinska! more than once I've beat my head against a wall trying to figure out someone else's post of code only to find they had made a error when typing. lol
Changed in my original post also. I wans't sure about that, Vin. Dont' we need to access the derefenced pointer inside the function also? (Like in post #15, example 3) @Alek : Did your head explode yet? If not we can start on pointers to pointer, null pointers, void pointers and that kind of stuff.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
|
|
#20 |
![]() Join Date: Sep 2008
Location: Jerusalem, Israel
Posts: 2,121 (1.24/day)
Thanks: 213
Thanked 481 Times in 347 Posts
|
Pointers to functions anyone ?
![]() Oh, and there is one thing to always remember regarding memory leaks (if you're on *nix): "Be kind: Valgrind."
__________________
Cameron: Core i7 2600K 4.5Ghz, MCR220, 2xMCR120, MCP655, ASRock P67 Extreme4, 4GB DDR3, 2xOCZ Vertex 30GB RAID0, GTX470, 2xHD5670, Modu82+ 625W, TT Xaser VI. Neuromancer: Core i7 975, DFI DK X58-T3eH6, 12GB DDR3 1333Mhz CL6-6-6-15-1T, 3x9600GSO 384MB, Hiper 880W, TT Xaser VI. Administrator of a 40 core Hadoop cluster. |
|
|
|
|
|
#21 | ||
![]() Join Date: Jul 2011
Location: Kaunas, Lithuania
Posts: 469 (0.70/day)
Thanks: 309
Thanked 202 Times in 122 Posts
|
Quote:
...I got sidetracked, didn't I? Also, to quote cplusplus.com (quote slightly modified) : Quote:
__________________
Why do you wear glasses if you're deaf? Code:
while (1) {
alone();
}
Last edited by Vinska; Apr 26, 2012 at 07:48 AM. |
||
|
|
|
|
|
#22 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Okay you win. I'm getting page faults when trying to access C++ info in my brain.
I'm going back to my C# GPU-Z shared memory thread.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
Last edited by Kreij; Apr 18, 2012 at 05:56 PM. |
|
|
|
|
|
#23 |
![]() |
I don't know what to say!
I can't thank you guys enough for your help
|
|
|
|
|
|
#24 |
|
Hardcore Monkey Moderator
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,133 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts
|
Just say you won't give up and will keep letting us help you.
![]() Someday you will understand that passing the knowledge you have gained to someone who is new at it, is more important and gratifying than using it.
__________________
Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other. Get more tech news on a wide variety of topics at NextPowerUp
|
|
|
|
| The Following 3 Users Say Thank You to Kreij For This Useful Post: |
|
|
#25 | ||
![]() Join Date: Jan 2012
Location: Dover, New Hampshire, USA
Posts: 4,277 (8.85/day)
Thanks: 1,284
Thanked 1,333 Times in 989 Posts
|
Quote:
http://www.gnu.org/philosophy/free-sw.html Quote:
__________________
MyHeat |
||
|
|
|
| The Following User Says Thank You to Aquinus For This Useful Post: |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ programming | Aleksander | Programming & Webmastering | 21 | Jan 26, 2012 02:23 PM |
| Guides on Programming? | Kreij | Programming & Webmastering | 15 | Dec 31, 2010 04:43 AM |
| C programming question! | binsky3333 | Programming & Webmastering | 9 | Feb 19, 2009 06:56 AM |
| Programming C | LORD_OF_WAR | Programming & Webmastering | 7 | Feb 4, 2009 01:22 PM |
| Ti-84/ti-83 programming | binsky3333 | General Hardware | 9 | Aug 30, 2008 12:02 AM |