Computer Science Canada

pointers?

Author:  AsianSensation [ Tue Jul 08, 2003 9:33 pm ]
Post subject:  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++?

Author:  Tony [ Tue Jul 08, 2003 10:39 pm ]
Post subject: 

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.

Author:  UBC_Wiskatos [ Tue Jul 08, 2003 11:18 pm ]
Post subject:  Re: pointers?

AsianSensation wrote:
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.

Author:  SilverSprite [ Wed Jul 09, 2003 1:18 am ]
Post subject: 

They wouldn't invent them if they weren't useful.. so it's probably you not understand what OOP is or inexperience.

Author:  Tony [ Wed Jul 09, 2003 2:34 am ]
Post subject: 

SilverSprite wrote:
They wouldn't invent them if they weren't useful..


well they did publish turing Rolling Eyes

Laughing seriosly though there are some weird things in programming languages... like goto... everybody hates them, but it still works Confused

Author:  SilverSprite [ Wed Jul 09, 2003 2:51 am ]
Post subject: 

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..

Author:  rizzix [ Wed Jul 09, 2003 2:55 am ]
Post subject: 

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++

Author:  SilverSprite [ Wed Jul 09, 2003 2:59 am ]
Post subject: 

i love pointers to functions:D

Author:  Tony [ Wed Jul 09, 2003 3:01 am ]
Post subject: 

me too, OOP pwnz Laughing

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 Very Happy Its great.

Author:  rizzix [ Wed Jul 09, 2003 3:13 am ]
Post subject: 

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 Smile
and there is an correct OOP equivalent of Pointers to Functions as well.

Author:  UBC_Wiskatos [ Wed Jul 09, 2003 11:46 am ]
Post subject: 

rizzix wrote:
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. Wink

Author:  Homer_simpson [ Wed Jul 09, 2003 1:34 pm ]
Post subject: 

[quote="tony"]me too, OOP pwnz Laughing
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

Author:  Tony [ Wed Jul 09, 2003 6:37 pm ]
Post subject: 

i dont care, OOP still pwnz Laughing

besides, cant entirely trust what they teach you in school.

Author:  rizzix [ Wed Jul 09, 2003 6:49 pm ]
Post subject: 

I Agree Laughing

Author:  SilverSprite [ Wed Jul 09, 2003 6:54 pm ]
Post subject: 

Yeah I'll have to agree too..

Author:  PaddyLong [ Wed Jul 09, 2003 7:32 pm ]
Post subject: 

lol we oce had a geography teacher that said the entire ocean froze over

Author:  UBC_Wiskatos [ Wed Jul 09, 2003 8:27 pm ]
Post subject: 

[quote="Homer_simpson"]
tony wrote:
me too, OOP pwnz Laughing
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. Smile

Author:  AsianSensation [ Wed Jul 09, 2003 8:50 pm ]
Post subject: 

whoa, never knew pointers could do this much...

oh well, then it's just another reason to start learning them and using them...

Author:  Homer_simpson [ Wed Jul 09, 2003 10:43 pm ]
Post subject: 

UBC_Wiskatos wrote:

Well theoretical OOD and well-done OOD are two different things. Smile

what's OOD??!! Confused

Author:  Catalyst [ Wed Jul 09, 2003 10:52 pm ]
Post subject: 

object oriented design (unless thats a typo)

Author:  Homer_simpson [ Wed Jul 09, 2003 10:56 pm ]
Post subject: 

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

Author:  Catalyst [ Wed Jul 09, 2003 11:21 pm ]
Post subject: 

ood is planning out how all ur classes interact and are used

Author:  Mazer [ Thu Jul 10, 2003 9:21 am ]
Post subject: 

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.

Author:  rizzix [ Thu Jul 10, 2003 9:48 am ]
Post subject: 

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++

Author:  Mazer [ Thu Jul 10, 2003 12:31 pm ]
Post subject: 

rizzix wrote:
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?

Author:  UBC_Wiskatos [ Thu Jul 10, 2003 1:31 pm ]
Post subject: 

Mazer wrote:
rizzix wrote:
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).

Author:  Mazer [ Thu Jul 10, 2003 1:39 pm ]
Post subject: 

ok... so how would you free the memory being used by the object you created?

Author:  rizzix [ Thu Jul 10, 2003 1:54 pm ]
Post subject: 

use the delete operator =)

Author:  rizzix [ Thu Jul 10, 2003 1:59 pm ]
Post subject: 

AsianSensation wrote:
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!

Author:  Homer_simpson [ Thu Jul 10, 2003 2:39 pm ]
Post subject: 

here's a tutorials on pointers basic : http://www.cplusplus.com/doc/tutorial/tut3-3.html


: