Recent

Author Topic: indexind pointer  (Read 5617 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10669
  • Debugger - SynEdit - and more
    • wiki
Re: indexind pointer
« Reply #15 on: June 25, 2024, 05:41:05 pm »
arrays are just pointers in fact - nothing specjal.

Actually:
- Array have range checks for bounds. Really helpful to find bugs, and improve stability/safety of the app
- dynamic array are refcounted.
...


Also
Quote
Code: Pascal  [Select][+][-]
  1. p := GetMem(sizeof(Double)*n);

That can be written as
Code: Pascal  [Select][+][-]
  1. var p: array of double; // no pre-defined size for the array
  2. begin
  3.   SetLength(p, n); // And you can resize with this statement too.
  4. end;



Laur

  • New Member
  • *
  • Posts: 35
Re: indexind pointer
« Reply #16 on: June 25, 2024, 05:58:56 pm »
OK. maybe it's just that...

Thanks.

...

but i noticed one problem with this:

is possible use this... nomenclature for array of pointers?

array of double of double; ?

maybe:
tp : array[0..x] of array of double;

SetLength(tp[6], 888);

is this allowable in fp?
« Last Edit: June 25, 2024, 06:54:14 pm by Laur »

Zoran

  • Hero Member
  • *****
  • Posts: 1892
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: indexind pointer
« Reply #17 on: June 25, 2024, 07:53:45 pm »
is this allowable in fp?

Don't you know that you can try simple things before asking?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5811
  • Compiler Developer
Re: indexind pointer
« Reply #18 on: June 25, 2024, 08:10:53 pm »
I dont' see any revelation in pointer indexing.

var p : PDouble;
    n : Integer;

.... some code

and:

p := GetMem(sizeof(Double)*n);

snip

Please use [code=pascal][/code]-tags to avoid the forum software interpreting the code and to make it easier viewable.

is this allowable in fp?

Don't you know that you can try simple things before asking?

If something works that might not necessarily mean that it is correct, so asking is nevertheless a good thing. Though in this specific case it would be correct...

Laur

  • New Member
  • *
  • Posts: 35
Re: indexind pointer
« Reply #19 on: June 25, 2024, 08:25:43 pm »
I haven't used Pascal for many years because it's too clumsy, I just... temporarily need to do something with this crap and that's why I'm asking.

I noticed that you are not very communicative here -
perhaps this is a forum for children;
You are doing such clowning here in front of those who are more stupid (by your presumption) - right?

OK.And that's enough.


PascalDragon

  • Hero Member
  • *****
  • Posts: 5811
  • Compiler Developer
Re: indexind pointer
« Reply #20 on: June 25, 2024, 08:42:39 pm »
I haven't used Pascal for many years because it's too clumsy, I just... temporarily need to do something with this crap and that's why I'm asking.

And with that attitude you'll really raise the interest of others in helping you... 🙄

TRon

  • Hero Member
  • *****
  • Posts: 3788
Re: indexind pointer
« Reply #21 on: June 25, 2024, 09:16:06 pm »
Code: Pascal  [Select][+][-]
  1. p := GetMem(sizeof(Double)*n);
  2.  
  3. for i :=  0 to N-1 do
  4.  p[i] := sin(i*pi/N);
  5.  


this is safe, and natural;

If you truly have the believe that the code as posted represents safe programming then I'm sorry to remark (e.g. not meant to offend) , that there is something seriously wrong with either the concept of safe programing or your interpretation of that concept (My bet would be on the latter  :) ).

It is lacking some serious defensive programming and is a good example of why it doesn't matter how much safety is present in the language/compiler itself that bad code will still be bad code no matter what.

That doesn't take away the fact that if you wish to do exactly that, that you can do exactly that what you can do in c you can do in Pascal as well.
« Last Edit: June 25, 2024, 09:18:25 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

Laur

  • New Member
  • *
  • Posts: 35
Re: indexind pointer
« Reply #22 on: June 27, 2024, 05:11:46 pm »
I know everything can be coded in pas, but the c version will be simpler,
becaus pas is... too poor for optimal, efficient coding.  :D

c++ is still more efficient, of course.

Thaddy

  • Hero Member
  • *****
  • Posts: 16387
  • Censorship about opinions does not belong here.
Re: indexind pointer
« Reply #23 on: June 27, 2024, 05:31:41 pm »
I know everything can be coded in pas, but the c version will be simpler,
becaus pas is... too poor for optimal, efficient coding.  :D

c++ is still more efficient, of course.
That is all simply not true and I showed you that.
There is nothing wrong with being blunt. At a minimum it is also honest.

Laur

  • New Member
  • *
  • Posts: 35
Re: indexind pointer
« Reply #24 on: June 27, 2024, 06:01:48 pm »
what You showed?

and i'm strongly afrayd becouse:
"Actually:
- Array have range checks for bounds. Really helpful to find bugs, and improve stability/safety of the app
..."

probably an overhead of these 'dynamics' arrays is tramedous!

pointers ale always fastest... therefore c is better.

btw: the fastest is assembler, but it's quite other story.  :D

« Last Edit: June 27, 2024, 06:42:52 pm by Laur »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 10669
  • Debugger - SynEdit - and more
    • wiki
Re: indexind pointer
« Reply #25 on: June 27, 2024, 06:58:24 pm »
what You showed?

and i'm strongly afrayd becouse:
"Actually:
- Array have range checks for bounds. Really helpful to find bugs, and improve stability/safety of the app
..."

probably an overhead of these 'dynamics' arrays is tramedous!

pointers ale always fastest... therefore c is better.

Yes and no. Range checks are controlled by compiler settings. You enable them for testing, but not for releases.

Ref counts can add a bit of overhead if you have multiple variables sharing the same array (like several pointers to the same memory), and you often change to which memory an array variable points. That isn't necessarily the case in all apps.

But as I said earlier
If you have the option of using an array (and that is an array with correct bounds) then that is preferable, because range checks can catch errors.

Or, if you don't have the option, then you can use a pointer with index. But you should know what your reason is, because by not using arrays you invite the risk of other potential issues, and you may loose hours, day or weeks to finding errors, that an array would have avoided. Of course the keyword is risk. It may not happen. But it can happen to anyone, up to the highest expert.

If your app is that time critical, then you need more details to determine if and how much an array actually affects you.

But also, if your app is that time critical your initial question of using pointer with index, may be to slow too.
Are you accessing elements in random order, will that cause cache misses?
Or are you accessing then in sequence? But then an index is slow, and you should use "inc(ptr)" instead.

And, btw, in principal the index vs inc speed diff applies to all languages. Only that some C compilers will silently optimize index access into code that performs inc instead. But that relies on the compiuler recognizing special cases, whereas the safe way is to write code using inc directly.

Laur

  • New Member
  • *
  • Posts: 35
Re: indexind pointer
« Reply #26 on: June 27, 2024, 07:11:27 pm »
I don't see code of these 'dynamic' arrays, then I can't say how big is overhead..
probably very big - 10 times slower than pointers?

Make test.

Thaddy

  • Hero Member
  • *****
  • Posts: 16387
  • Censorship about opinions does not belong here.
Re: indexind pointer
« Reply #27 on: June 27, 2024, 07:23:45 pm »
I don't see code of these 'dynamic' arrays, then I can't say how big is overhead..
probably very big - 10 times slower than pointers?

Make test.
as i wrote, and i confirm this, you may be old, but you know jack shit about compiler construction.
1958 is the pivot to me..  8-) O:-)
For the core developers it is 1970, which has a ....unix connection which it isn't because it coincedence.
« Last Edit: June 27, 2024, 07:27:12 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

TRon

  • Hero Member
  • *****
  • Posts: 3788
Re: indexind pointer
« Reply #28 on: June 27, 2024, 07:40:14 pm »
@Laur:
As already shown you can do exactly the same in/with pascal as you can do in c. Since that is not landing properly that can only mean one thing. So stop bothering us and stay using c/c++ or whatever language that is faster and easier to use according to your believes because quite frankly we (or at least I) am not in the habit of wasting time convincing someone that does not want to be convinced that his/her believes are wrong.

All the brave talks but still not a single line of code from your hands for comparison... thus yet another hot air balloon.. :)
I do not have to remember anything anymore thanks to total-recall.

TRon

  • Hero Member
  • *****
  • Posts: 3788
Re: indexind pointer
« Reply #29 on: June 27, 2024, 07:42:20 pm »
@Laur:
Did I already mention hot air ? (annoying isn't it ?)
I do not have to remember anything anymore thanks to total-recall.

 

TinyPortal © 2005-2018