
-----------------------------------
AsianSensation
Tue Jul 08, 2003 9:33 pm

pointers?
-----------------------------------
I have a question: What's the reason for using pointers? I am learning this stuff right now, but I fail to find a reason for the use of pointers...

is it for speed, or organization? Or am I simply not getting some fundamental ideas or stuff behind C++?

-----------------------------------
Tony
Tue Jul 08, 2003 10:39 pm


-----------------------------------
pointers to the object? as in

something -> function ?

man, thats what OOP is all about, to create objects and use them. Objects are objects, for example, a brick. It is an universal set of functions and variables that is the same in every of your walls (programs), but when you're building the wall (programming using OOP) you have to point to what kind of brink you want to be using.

Now a real programming example. You know how modules work, right? The Draw.Dot where Draw is the name of the module and Dot is name of the function? Its quite similar to the object, but with OOP, you can create a variable and point it to the object. You could create an array of objects. And have something like Drawing(2)->Draw.Dot

I hope this clears OOP basics up.

-----------------------------------
UBC_Wiskatos
Tue Jul 08, 2003 11:18 pm

Re: pointers?
-----------------------------------
I have a question: What's the reason for using pointers? I am learning this stuff right now, but I fail to find a reason for the use of pointers...

is it for speed, or organization? Or am I simply not getting some fundamental ideas or stuff behind C++?

A pointer is crucial for effective organization of code, for speed, for various algorithms, for data storage, and for dynamic stuff. Pointers play very important parts in APIs, and DLLs designed to be reusable, as well as COM objects and whatnot. It's hard to see why pointers are so important without beginning to use them. For example, my graphics engine is at its core, based on a large amount of pointers, which make it fast, reusable, scalable, robust, and whatnot. There's many types of pointers which are used for different things... Function pointers, void pointers, pointers used in callback functions, etc.

-----------------------------------
SilverSprite
Wed Jul 09, 2003 1:18 am


-----------------------------------
They wouldn't invent them if they weren't useful.. so it's probably you not understand what OOP is or inexperience.

-----------------------------------
Tony
Wed Jul 09, 2003 2:34 am


-----------------------------------
They wouldn't invent them if they weren't useful.. 

well they did publish turing  :roll: 

 :lol: seriosly though there are some weird things in programming languages... like goto... everybody hates them, but it still works  :?

-----------------------------------
SilverSprite
Wed Jul 09, 2003 2:51 am


-----------------------------------
well turing if for ppl who are just learning a new language.. and for those who find it hard to understand programming.. haha goto that thing is weird..

-----------------------------------
rizzix
Wed Jul 09, 2003 2:55 am


-----------------------------------
people hate goto cuz it lead to VERY BAD CODE. practically completely cryptic.

btw what's the point of point-ers? lol...
hmm just some C carry over to c++ thingie.
they could've just stuck to references.. but then u wouldn't have pointer arithmetic.. 

although u don't need that pointer arithmetic.. some languages just got rid of pointers.. since it usually leads to, lets say difficult to debug code... for them references were good enough.

this dosen't mean pointers are bad.. just think of them as a feature of c/c++

-----------------------------------
SilverSprite
Wed Jul 09, 2003 2:59 am


-----------------------------------
i love pointers to functions:D

-----------------------------------
Tony
Wed Jul 09, 2003 3:01 am


-----------------------------------
me too, OOP pwnz  :lol: 

the best part (other then the usual reusable rebust programming speach) is that instead of writing a complex function that figures out what objecest it does calculations for, you point to a function from the object  :D Its great.

-----------------------------------
rizzix
Wed Jul 09, 2003 3:13 am


-----------------------------------
pointers to functions isn't part of oop though :wink: it is still procedural,, something c++ inherited from c
pointers to Objects ,,, oh yes, now thats OOP  :)
and there is an correct OOP equivalent of Pointers to Functions as well.

-----------------------------------
UBC_Wiskatos
Wed Jul 09, 2003 11:46 am


-----------------------------------
people hate goto cuz it lead to VERY BAD CODE. practically completely cryptic.

btw what's the point of point-ers? lol...
hmm just some C carry over to c++ thingie.
they could've just stuck to references.. but then u wouldn't have pointer arithmetic.. 

although u don't need that pointer arithmetic.. some languages just got rid of pointers.. since it usually leads to, lets say difficult to debug code... for them references were good enough.

this dosen't mean pointers are bad.. just think of them as a feature of c/c++

Well unless you program in assembly, then goto or its equivalent are the only thing you have. ;)

-----------------------------------
Homer_simpson
Wed Jul 09, 2003 1:34 pm


-----------------------------------
[quote="tony"]me too, OOP pwnz  :lol: 
quote]
from what i know...pointers have nothing to do witg OOP...right now we're doing oop programming in c++ and i haven't used a single pointer...well mayb 1 or 2 =p

-----------------------------------
Tony
Wed Jul 09, 2003 6:37 pm


-----------------------------------
i dont care, OOP still pwnz   :lol: 

besides, cant entirely trust what they teach you in school.

-----------------------------------
rizzix
Wed Jul 09, 2003 6:49 pm


-----------------------------------
I Agree  :lol:

-----------------------------------
SilverSprite
Wed Jul 09, 2003 6:54 pm


-----------------------------------
Yeah I'll have to agree too..

-----------------------------------
PaddyLong
Wed Jul 09, 2003 7:32 pm


-----------------------------------
lol we oce had a geography teacher that said the entire ocean froze over

-----------------------------------
UBC_Wiskatos
Wed Jul 09, 2003 8:27 pm


-----------------------------------
me too, OOP pwnz  :lol: 
quote]
from what i know...pointers have nothing to do witg OOP...right now we're doing oop programming in c++ and i haven't used a single pointer...well mayb 1 or 2 =p

Well theoretical OOD and well-done OOD are two different things. :-)

-----------------------------------
AsianSensation
Wed Jul 09, 2003 8:50 pm


-----------------------------------
whoa, never knew pointers could do this much...

oh well, then it's just another reason to start learning them and using them...

-----------------------------------
Homer_simpson
Wed Jul 09, 2003 10:43 pm


-----------------------------------

Well theoretical OOD and well-done OOD are two different things. :-)
what's OOD??!!  :?

-----------------------------------
Catalyst
Wed Jul 09, 2003 10:52 pm


-----------------------------------
object oriented design (unless thats a typo)

-----------------------------------
Homer_simpson
Wed Jul 09, 2003 10:56 pm


-----------------------------------
lol... object oriented designing(even that is a word)  has nothing to do with what i'm doing i'm pretty sure that i'm doing OOp

-----------------------------------
Catalyst
Wed Jul 09, 2003 11:21 pm


-----------------------------------
ood is planning out how all ur classes interact and are used

-----------------------------------
Mazer
Thu Jul 10, 2003 9:21 am


-----------------------------------
speaking of pointers....  :wink: 

here's what i understand about them (which isn't much). unless i'm mistaken, pointers are supposed to make better use of your computer's memory. instead of declaring a variable and thus having your computer set aside a certain amount of memory for that variable, declaring a pointer set's aside a certain amount of memory so you can store the address to another memory location that stores information for an object. this way, once you're done with the object you can set it's pointer to NULL and it's like it never existed so the memory it was taking up is now free.

or something...
chances are that i am wrong because i don't really know what i'm talking about. please, someone correct me if that is so.

-----------------------------------
rizzix
Thu Jul 10, 2003 9:48 am


-----------------------------------
no finally ur allocating memory of the object whether u like it or not.
and after ur done with the object u have to free the allocated memory!

as i said just think of pointers as a feature of c++

-----------------------------------
Mazer
Thu Jul 10, 2003 12:31 pm


-----------------------------------
and after ur done with the object u have to free the allocated memory!

isn't that what your doing by setting the pointer to NULL?

-----------------------------------
UBC_Wiskatos
Thu Jul 10, 2003 1:31 pm


-----------------------------------
and after ur done with the object u have to free the allocated memory!

isn't that what your doing by setting the pointer to NULL?

No, NULL is a C macro that sets the pointer to memory address 0. The memory is still in use, you just can't access it anymore. This creates a memory leak, because when more memory is allocated, that object hasn't been deleted and no new memory can be created there until the computer is restarted.

A pointer allows you to work directly with the object in memory, it is as simple as that. This has speed benefits and other things. However, keep in mind that a pointer is just a gateway to your memory, when you delete it, the memory is still there.

I've used this analogy before. Say you have a neighbourhood lot with 10 houses. In one of the houses, there is a broken pipe, and you have to call the plumber.

Now, the plumber can do two things. He can go directly to the house and fix the pipe (faster), or he can make his own model of that same house, figure out how to fix it, then go to the house and fix the house (slow). The house is the object, the plumber is the agent, and the address is the pointer. By using a pointer, you just go straight to the house. By not using a pointer, the compiler creates a copy of the object in a function (that is, ByVal).

-----------------------------------
Mazer
Thu Jul 10, 2003 1:39 pm


-----------------------------------
ok... so how would you free the memory being used by the object you created?

-----------------------------------
rizzix
Thu Jul 10, 2003 1:54 pm


-----------------------------------
use the delete operator =)

-----------------------------------
rizzix
Thu Jul 10, 2003 1:59 pm


-----------------------------------
whoa, never knew pointers could do this much...

oh well, then it's just another reason to start learning them and using them...

no one will consider u a C/C++ programmer if u don't know how to use poitners.. cuz a lot of the libraries available out there use pointers/references extensively... without that knowledge ur lost!

-----------------------------------
Homer_simpson
Thu Jul 10, 2003 2:39 pm


-----------------------------------
here's a tutorials on pointers basic : http://www.cplusplus.com/doc/tutorial/tut3-3.html
