Recent

Author Topic: GetMem 1x as Procedure, and 1x as Function - confused me  (Read 1723 times)

paule32

  • Sr. Member
  • ****
  • Posts: 280
GetMem 1x as Procedure, and 1x as Function - confused me
« on: March 01, 2024, 03:00:44 pm »
In the heaph.inc - there are:  https://www.freepascal.org/docs-html/rtl/system/getmem.html

you have "GetMem" twice.

- one Time a Procedure,
- one Time a Function with return Value.

My knowledge of other DSL's like C++ - so you can't have members with different return Values.
But you can have multiple overloaded members - with different Parameter's/Argument's.

What about FPC ?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11951
  • FPC developer.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #1 on: March 01, 2024, 03:25:12 pm »
Afaik for overloading, roughly the number of arguments must be different and/or typed different, as that is the signature used for which function to call. So not on the return type.

If the right version is chosen, on those criteria, it must then match the use for the rest of signature, including return value.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #2 on: March 01, 2024, 06:11:17 pm »
how about the function GetMem(size): Pointer ?
is that in context of the Memory Manager ?
where did GetMem "get" the Pointer, to allocate the Memory size ?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11951
  • FPC developer.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #3 on: March 01, 2024, 06:19:18 pm »
They both call the heapmanager's getmem.

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #4 on: March 01, 2024, 06:26:40 pm »
You are free to write your own memory manager anytime. It is also easy. Not like your other questions which take a lot more effort..
Look at the code in cmem.pp
« Last Edit: March 01, 2024, 06:28:38 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #5 on: March 02, 2024, 10:21:03 am »
how can I "store" the Pointer's to "allocated" Object's ?
with a classic "linked List" ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #6 on: March 02, 2024, 12:54:25 pm »
the linked list needs to be a doubly linked lst and store both allocation size and data (so a record) . Easy
If I smell bad code it usually is bad code and that includes my own code.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #7 on: March 02, 2024, 01:09:42 pm »
record's are like class's ?

All what is in a record, is transparent/public ?
All what is in a class, can be private, public  ... ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #8 on: March 02, 2024, 01:25:35 pm »
That has no relevance. Use records, before you can even use classes.
If I smell bad code it usually is bad code and that includes my own code.

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #9 on: March 02, 2024, 01:42:23 pm »
All what is in a record, is transparent/public ?

That used to be true in old Pascal, but modern Pascal language has "advanced" (also called "extended") records. FPC implements advanced records since version 2.6.
To be able to use advanced records, you need the mode switch "advancedrecords".

The main difference between records and classes is that
  - class is always allocated on the heap and each class variable declared in your code is an implicit pointer. You have to allocate it (by using constructor) and it is programmer's (yours) responsibility to destroy it, before the pointer goes out of scope.
  - record variables are allocated like any variable of some fundamental type - a record can be allocated on the stack. If you want a record allocated on the heap, you need a pointer which is allocated in code (with standard procedure new) and deallocate it (with dispose).

Another difference is that records do not have inheritance.

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #10 on: March 02, 2024, 01:59:48 pm »
In the heaph.inc - there are:  https://www.freepascal.org/docs-html/rtl/system/getmem.html

you have "GetMem" twice.

- one Time a Procedure,
- one Time a Function with return Value.

My knowledge of other DSL's like C++ - so you can't have members with different return Values.
But you can have multiple overloaded members - with different Parameter's/Argument's.

What about FPC ?

It is so in FPC as well, you are right that two overloaded routines with same name and in same scope need to have different signature (argument list). And these two routines do have different signatures.
The fact that they have different return values would not be enough by itself, if the argument list were the same, they could not be overloaded.

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #11 on: March 02, 2024, 02:18:21 pm »
Zoran, you are missing the point.
He is writing his own compiler so he needs to implement everything himself.
He is not very knowledgeable, so he should look at the basics first
Like here: https://secondboyet.com/Code/ToDADS_source.zip

Those are the basics, but for delphi. It still compiles, but not in his hands.... :)
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1678
    • http://www.cdbc.dk
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #12 on: March 02, 2024, 07:15:48 pm »
Hi
@Thaddy: I don't think that, without Julian's writing, he'll get much out of the sources.... From what I've seen from him so far, he needs to buy the book.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zoran

  • Hero Member
  • *****
  • Posts: 1882
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: GetMem 1x as Procedure, and 1x as Function - confused me
« Reply #13 on: March 02, 2024, 10:31:19 pm »
He is writing his own compiler
Oh... :-X

Zoran, you are missing the point.

Totally.

 

TinyPortal © 2005-2018