Advertisement

understanding the use of pointers.....

Started by May 22, 2002 10:42 AM
26 comments, last by werdy666 22 years, 5 months ago
quote: Original post by foofightr

Overall, references breed laziness by hiding certain functionality. But the real bugger is when you are looking at other people''s code, or even your own code that you havent seen for a long time. Seeing that asterisk keeps you sharp, it is a visual reminder that "this is a pointer."


Care to clarify that for us, I''m sure you have good reasons for saying that you loose fonctionality when you use references. I''d like to know what functionality is lost....




"And that''s the bottom line cause I said so!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
quote: Original post by Mythril
maybe i am dumb, or just too much of a novice, but i still do not understand pointers. i know that they allow memory allocation(forgive me if that is the wrong wording) but i dont know why this is so useful, or how it is implemented.
the way i see it is that you make a pointer. then a variable that holds a value. then i believe the pointer holds the address of the variable. and by accessing the pointer, you access the value of the variable?
i have read articles in the reference section here, and they do nothing for me. if you guys would help me understand how they get the address, and the true advantages for these pointers in gaming and other programming, i would be much abliged.

Mythril


Ok, let''s assume we are working on a 32 bit platform. The reason it''s called a 32 bit platform and not a 16 bit platform is because it uses 32 bits to address memory, not 16 (like win 3.1)

16 bits:
0000000000000000
32 bits:
00000000000000000000000000000000

16 bits means you can have 65535 memory locations, where as with 32 bits, you can have 4294967295 memory locations.

So here''s what a pointer is: in a 32 bit system, a pointer is the 32 bit handle to the memory location.

Ok, we know where the memory is located, how do we know how long it is? Well, that''s why we cast pointers of certain types. We know that a pointer to a long value is a 32 bit memory location address of that long and the compiler knows to read the length of a long when accessing that variable.

How do you get the address of a variable? Well man, if this is the first time you are playing with them, I wouldn''t even bother with them. Read the other posts to see how dangerous and unnecessary pointers really are.
Advertisement
quote: Original post by Anonymous Poster
I always thought that the size of lets say an int depends rather on the compiler you''re using than the OS you''re running. Correct me if I''m wrong, but I don''t think there is some ISO/ANSI/Whatever standard that involves the size of the datatypes. I think it''s only specified to that point that char < short < int. I''m no expert at this since I''m quite stuck on windows using VC++ wich uses 32 bits for ints....
Flame me if I''m wrong


A char is always 1 byte
A short (which is a short form for short int) is always 2 bytes
A long (which is a short form for long int) is always 4 bytes

But, an int (without short/long prefix) equals to the machine word size. In Linux, Unix, Windows 95+, Windows NT+ etc. it is 4 bytes. In DOS and Windows 3.1 etc. it is 2 bytes.


Joakim Asplund
http://megajocke.cjb.net
Joakim Asplundhttp://megajocke.y2.org
"Care to clarify that for us, I''m sure you have good reasons for saying that you loose fonctionality when you use references. I''d like to know what functionality is lost...."

I know english might not be your first language (says you''re from Quebec), but read what I wrote more carefully. I never said any functionality is lost. I said it *hides* some functionality, because pointers and references are the same thing implemented differently. If you''re not very familiar with the code you''re looking at, it''s not immediately obvious that something like A = 5; might be modifying an outside value (if A is a reference). On the other hand, *A = 5; is a clear indicator of what''s going on.

I''ve had this happen a few times when looking at source code I got from the internet. The problem is worse if you mix references and pointers in the same functions. I''ll admit those source were overall badly coded, but the shoddy references only made things worse. Don''t ask for the specific source because I simply don''t remember.

Maybe this boils down to personal preference, but I tend to make my code as readable and clean as possible, so that if others were to look at it, they wouldn''t have any problems.
quote: Original post by foofightr
I said it *hides* some functionality, because pointers and references are the same thing implemented differently.

ITYM, pointers and references are different things implemented similarly.

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Yes, I''d have to agree with SabreMan here. The differences between pointers and references are very clear, even if the compiler implements them in a similar way.

There''s a saying "use references when you can, and pointers when you have to". Whether you follow that or not is up to you.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
ITYM = ?

That''s a new one for me...
"I think you mean".

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

This topic is closed to new replies.

Advertisement