Recent

Author Topic: Variables initialization  (Read 6707 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Variables initialization
« Reply #15 on: January 21, 2024, 12:29:27 pm »
For some things it is guaranteed, because it's considered part of the language.
It may be part of FPC but it is not part of the Pascal language.

It's part of the language dialect(s) that FPC understands.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Variables initialization
« Reply #16 on: January 21, 2024, 01:11:52 pm »
I miss that if you want to allocate initialized data, and getmem is citated above,  one should use AllocMem, not GetMem...
AllocMem initializes to all zero's, also on the stack, a bit like Default().
« Last Edit: January 21, 2024, 04:02:26 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Variables initialization
« Reply #17 on: January 23, 2024, 09:39:38 pm »
I miss that if you want to allocate initialized data, and getmem is citated above,  one should use AllocMem, not GetMem...
AllocMem initializes to all zero's, also on the stack, a bit like Default().

If one touches the whole allocated memory after the allocation anyway, then GetMem is better.

Чебурашка

  • Hero Member
  • *****
  • Posts: 593
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Variables initialization
« Reply #18 on: January 26, 2024, 08:13:57 am »
Isn't it easier to design execution flow in such way that values assigned naturally with meaningful values before the first time the value is being used anywhere in program. Then there is no guessing or duplicate operations.

A software where is difficult to tell whether variables are initialized or not probably needs rewritten from zero, as I could see several times.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Variables initialization
« Reply #19 on: January 26, 2024, 01:57:44 pm »
Well the rule is pretty simple: Stack, locals: Uninitialized, Heap, global: Initialized.
« Last Edit: January 26, 2024, 01:59:53 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Variables initialization
« Reply #20 on: January 27, 2024, 05:48:18 pm »
Well the rule is pretty simple: Stack, locals: Uninitialized, Heap, global: Initialized.

Wrong. GetMem or New will allocate from the heap, but the data contained there will be random.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Variables initialization
« Reply #21 on: January 27, 2024, 07:11:39 pm »
But that is why a mentioned allocmem. Suppose array allocation. Grr. Sometimes you can not read my replies Sarah.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: Variables initialization
« Reply #22 on: February 01, 2024, 09:35:27 pm »
But that is why a mentioned allocmem. Suppose array allocation. Grr. Sometimes you can not read my replies Sarah.

The post I quoted did not mention AllocMem at all, it generally mentioned Heap and there it is simply not true that the allocated memory will always be cleared as your sentence suggests.

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Variables initialization
« Reply #23 on: February 01, 2024, 11:42:53 pm »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Variables initialization
« Reply #24 on: February 02, 2024, 02:40:30 pm »
so almost exactly what i wrote....
pascaldragon is right when you know the whole memory will be committed but if and only if that is the case. otherwise you should use allocmem if you want initialized memory.
« Last Edit: February 02, 2024, 02:43:24 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018