Recent

Author Topic: Can someone please explain pointers..  (Read 34236 times)

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: Can someone please explain pointers..
« Reply #30 on: June 24, 2021, 02:20:46 am »
That's true but, for instance .net applications often feel clumsy and slow (unless on a very, very fast machine) the same is often true of Java programs.
Indeed they are.

  Of course, for simple things they do ok but, when it comes to real programs they inevitably fall short of the mark.
Putting aside most of the Android apps, some of them quite sophisticated, there are for example: Apache Hadoop and ASP.NET - frameworks for a large scale Web applications, Eclipse and NetBeans, M$ Visual Studio, M$ SQL Management Studio, etc. Very real (some of them - to my regret).

I like and use AWK relatively often and, for some things it is wonderful but, I don't see something like Oracle or Photohop written in AWK given its totally absent facilities to manage memory.
I don't know for AWK, but PHP have a lion share in the server side languages (>75%).

And all that without the pointer concept.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Can someone please explain pointers..
« Reply #31 on: June 24, 2021, 03:59:59 am »
And all that without the pointer concept.
I'm not saying that many programs can't be written without using pointers but, a program that takes good advantage of the flexibility pointers provide will always have some advantages over one that doesn't. 

I don't know for AWK, but PHP have a lion share in the server side languages (>75%).
AWK is a text manipulation language.  Very simple, easy to learn and really useful to manipulate text.  I recommend it.  Good tool to have in one's toolbox.

Putting aside most of the Android apps, some of them quite sophisticated, there are for example: Apache Hadoop and ASP.NET - frameworks for a large scale Web applications, Eclipse and NetBeans, M$ Visual Studio, M$ SQL Management Studio, etc.
I cannot comment on some of the programs you mentioned but, I use Visual Studio regularly and it's rather far from being a speed demon (no surprise since it is a .net front end.)  It's got a lot of nice features but, it's a rather clumsy program.  I'd rather not use it but sometimes it is helpful.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

noszone

  • New Member
  • *
  • Posts: 46
Re: Can someone please explain pointers..
« Reply #32 on: June 24, 2021, 06:24:05 am »
Probably most real use cases are image manipulations/comparisons? Video coding/decoding? As mentioned Windows API, other works with *dll 3rd party. I think the most hardware drivers are using pointers.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can someone please explain pointers..
« Reply #33 on: June 24, 2021, 08:12:17 am »
Probably most real use cases are image manipulations/comparisons? Video coding/decoding? As mentioned Windows API, other works with *dll 3rd party. I think the most hardware drivers are using pointers.

Pointers are pervasive: everything uses them.

However, everything is not the same as everybody. These days it is comparatively rare for an application programmer to have to use them, since by and large he will be more concerned with components and libraries written by system programmers which hide the details.

And that is entirely as it should be, since there are things that can be done with and to pointers which makes them dangerous in the hands of the inexperienced.

Some languages support references, which are very similar to pointers but somewhat safer since they are usually defined as not being amenable to arithmetic operations. So while you can increment a pointer to move through memory and potentially mess something up, generally speaking you can't do that with a reference.

As to the ten-year-old "what are they?" question, I've always found it useful to decouple pointers and the trees/lists with which they're usually lumped and then give a useful example.

Consider an operating system or library with an entry point that returns data on (say) its current version. There are two ways of handling that: either the API can define that the caller passes it a pointer to some memory into which it can copy the data, or the API defines that it will return a pointer to data that the program can then read. That's basically all there is to them.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Can someone please explain pointers..
« Reply #34 on: June 24, 2021, 08:29:57 am »
Mark,
or refer to our example of a Pointer being like your credit-card of your bank-account from some weeks ago
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can someone please explain pointers..
« Reply #35 on: June 24, 2021, 09:26:39 am »
Mark,
or refer to our example of a Pointer being like your credit-card of your bank-account from some weeks ago

When I started explaining this stuff to perplexed students people didn't have credit cards.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: Can someone please explain pointers..
« Reply #36 on: June 24, 2021, 10:52:41 am »
When I started explaining this stuff to perplexed students people didn't have credit cards.
I'm still remembering the time when the Pascal pointers were strongly typed and the things you can only do with them were new(), dispose() and =.
Now, the classes are the better substitute, you can create/free them, can't do arithmetic, etc. This topic makes me wonder about how long I haven't used the ^ symbol. It seems to have been a long time ago, because I don't remember well.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can someone please explain pointers..
« Reply #37 on: June 24, 2021, 01:29:50 pm »
I'm still remembering the time when the Pascal pointers were strongly typed and the things you can only do with them were new(), dispose() and =.
Now, the classes are the better substitute, you can create/free them, can't do arithmetic, etc. This topic makes me wonder about how long I haven't used the ^ symbol. It seems to have been a long time ago, because I don't remember well.

I wrote something using pointers a couple of days ago... and then redid it to use variant records. The final code is more verbose and I don't know how well the compiler is compacting it, but the result is something that the compiler can at least check fairly thoroughly.

Years ago, a major selling point of Delphi when compared with MS Visual BASIC was that a single language and toolset could be used to write both applications and lower-level extension components (MS required C++ for that). I think it could be argued that lower-level stuff requires pointers (and that less-capable references aren't flexible enough), but by now I am of the opinion that they should be forbidden to ordinary users.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Can someone please explain pointers..
« Reply #38 on: June 24, 2021, 01:57:10 pm »
Some weeks ago i was doing some stuff in Excel VBA, extensively using pointers despite Visual Basic/VBA "officially" not supporting pointers (There are "undocumented" functions you can use to get some rudimentary pointer-functionality).
The stuff i was doing was, in a nutshell, removing an element from an array, and shifting the rest of the array "one down", which in Visual Basic/VBA is a nightmare to achieve with just the tools VBA officially offers.
Bottom Line: I agree with Mark, that pointers should be "hidden" from the ordinary user.
'Cause if you don't know what you're doing, you're going to create spectacular crashes, probably at the most unconvinient time.
« Last Edit: June 24, 2021, 01:59:58 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Can someone please explain pointers..
« Reply #39 on: June 24, 2021, 03:26:06 pm »
Address of variable in memory can both be constant and variable. [$40000000] = 3 is example of variable with constant address. But address can also be variable. For example when you access array element it's address is calculated like <Address of array> + <Size of element> * <Index>. Another opportunity - to store address of one variable in another variable. That's, what pointer actually is.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Can someone please explain pointers..
« Reply #40 on: June 24, 2021, 05:24:47 pm »
I think hiding pointers from ordinary "users"/programmers encourages "clueless programming" resulting in programs that leave something to be desired.

Take a very simple program as an example, a checkbook program (not much to such a program.)  It would be expected that the program can present the information sorted by payee, date, check number, amount and possibly other criteria. 

Without using pointers sorting the checks is a real pain (not to mention slow if there are a lot of checks, e.g, a year's worth.)  The payee's name is a variably sized field, as will be the check's memo.  With pointers creating a list of checks sorted by a field is a piece of cake that takes very little code.  Without pointers, the programmer has to move variably sized fields around, that takes more code, it's more error prone and definitely slower due to all the necessary movement in memory.

Bottom line is, even something as simple as a checkbook program greatly benefits from using pointers.

Hiding pointers from a programmer is like hiding salt from a Chef.  Too much sodium is not good but so is not enough.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

alpine

  • Hero Member
  • *****
  • Posts: 1032
Re: Can someone please explain pointers..
« Reply #41 on: June 24, 2021, 05:39:00 pm »
Address of variable in memory can both be constant and variable. [$40000000] = 3 is example of variable with constant address. But address can also be variable. For example when you access array element it's address is calculated like <Address of array> + <Size of element> * <Index>. Another opportunity - to store address of one variable in another variable. That's, what pointer actually is.
Speaking of pointers and arrays, the C language once startled me with the commutativity of it's subscript operator [].
Look at the 5-th line for example:
Code: C  [Select][+][-]
  1. int main()
  2. {
  3.     int a[10];
  4.     a[5] = 1;
  5.     5[a] = 1;
  6.     *(a + 5) = 1;
  7. }
All statements are equivalent:
Code: [Select]
Dump of assembler code for function main:
   0x00000000004004f6 <+0>:     push   %rbp
   0x00000000004004f7 <+1>:     mov    %rsp,%rbp
=> 0x00000000004004fa <+4>:     movl   $0x1,-0x1c(%rbp)
   0x0000000000400501 <+11>:    movl   $0x1,-0x1c(%rbp)
   0x0000000000400508 <+18>:    movl   $0x1,-0x1c(%rbp)
   0x000000000040050f <+25>:    mov    $0x0,%eax
   0x0000000000400514 <+30>:    pop    %rbp
   0x0000000000400515 <+31>:    retq   

So what is a pointer and what is an index? The answer is obvious for experienced, but maybe not so obvious for for a novice.

@440bx
Do you consider
Code: [Select]
var obj: TObject; as a pointer?

Because, AFAIK it is a reference. And you can use TObjectList or some other container from the template library to do that job. Without using the ^.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can someone please explain pointers..
« Reply #42 on: June 24, 2021, 05:45:53 pm »
Code: C  [Select][+][-]
  1.     a[5] = 1;
  2.     5[a] = 1;
  3.  
...
So what is a pointer and what is an index? The answer is obvious for experienced, but maybe not so obvious for for a novice.

It's not so much that it's obvious for the experienced, since those experienced only in Pascal would expect a syntax error.

It's obvious for those who have read the specification in detail, since the equivalence of pointers and arrays is defined (not IIRC in K&R, but in ANSI).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Can someone please explain pointers..
« Reply #43 on: June 24, 2021, 06:02:25 pm »
Take a very simple program as an example, a checkbook program (not much to such a program.)  It would be expected that the program can present the information sorted by payee, date, check number, amount and possibly other criteria. 

Without using pointers sorting the checks is a real pain...

Ah, the old "do not spindle" instruction :-)

There's various answers I might suggest, starting off with the null answer: "you don't need pointers, references are adequate". However...

If sorting is a job suitable for the inexperienced, how is it that it's taken 60 years for people to agree how to do it? You might notice that some of the sorting algorithms in Knuth assume that you have scratch tapes available...

And if sorting and searching are so trivial, how is it that virtually all textbooks had an error in their instructions as to how to do a simple binary chop until just a few years ago?

I agree that pointers are useful- not essential, but useful- for that sort of job. However I'd suggest that the fact that the operations benefit from pointers and that pointers are tricky beasts means that searching and sorting operations should always be encapsulated: as they are of course with FPC's TStringList.

I'd go further, and suggest that as a rule all pointers and many uses of references should be encapsulated, since that would allow issues like circular references to be removed from the application programming domain which would make reference counting memory managers more tractable.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Can someone please explain pointers..
« Reply #44 on: June 24, 2021, 06:10:37 pm »
@440bx
Do you consider
Code: [Select]
var obj: TObject; as a pointer?

Because, AFAIK it is a reference. And you can use TObjectList or some other container from the template library to do that job. Without using the ^.
I wouldn't consider that a pointer but, if instead of TObject, it had been "class of object" (I probably have the syntax wrong, I stay away from objects/classes like the plague)  then I would definitely consider that a pointer (a hidden one which makes it worse.)



ETA:

@MarkMLI, sorry somehow I missed your post.  Here are my comments on it.

If sorting is a job suitable for the inexperienced, how is it that it's taken 60 years for people to agree how to do it?
Designing sorting algorithms is definitely not for the inexperienced but, the inexperienced definitely want to use someone's sorting algorithm and, when they do, understanding pointers would quite likely serve them well.

And if sorting and searching are so trivial,
I didn't say that sorting and searching are trivial.  What I say is that even something as trivial as a checkbook program needs them.  Because of that, a programmer should, at least, be familiar with a number of algorithms for them which means they should have a reasonable understanding of pointers.

However I'd suggest that the fact that the operations benefit from pointers and that pointers are tricky beasts means that searching and sorting operations should always be encapsulated: as they are of course with FPC's TStringList.
The problem is that hiding pointers has consequences, many of them really bad.  Among those consequences is that there are too many programmers who don't really understand what is happening in their programs because they don't see the hidden pointers.  The result is, they are much more likely to make a mistake than someone who see what is happening behind the scenes.  Not too mention that the program's design will likely be "less than optimal".

<snip> which would make reference counting memory managers more tractable.
reference counting memory managers... just what a programmer needs, some "memory manager" to get in the way of the programmer's design. 

Compilers shouldn't try to program... that's what a programmer is for.  Compilers should generate object code that faithfully reflects what the programmer specified in the source.   The road to hell is paved with compiler's good intentions.
« Last Edit: June 24, 2021, 06:40:52 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018