Recent

Author Topic: Initialization of variables  (Read 10674 times)

riwu

  • New Member
  • *
  • Posts: 15
Initialization of variables
« on: December 08, 2016, 09:05:48 am »
Quote
By default, variables in Pascal are not initialized after their declaration.
Is there a compiler option or some tools that change this behavior and automatically initialize integers to 0 etc?

Also, why does Pascal not allow initialization of multiple variables of the same type? 
eg.
Code: [Select]
x, y: integer = 0;instead we have to do
Code: [Select]
x: integer = 0;
y: integer = 0;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Initialization of variables
« Reply #1 on: December 08, 2016, 10:03:05 am »
Quote
By default, variables in Pascal are not initialized after their declaration.
Is there a compiler option or some tools that change this behavior and automatically initialize integers to 0 etc?

No, but there is an option to change them to a random value. (-gtt). This is for debugging though.

Quote
Also, why does Pascal not allow initialization of multiple variables of the same type? 
eg.
Code: [Select]
x, y: integer = 0;instead we have to do
Code: [Select]
x: integer = 0;
y: integer = 0;

In general, initialization a variable is considered an action rather than a declaration, so something for the program, not the declaration block. Iniitalized variables are a later (D4) addition and borrowed from VB

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Initialization of variables
« Reply #2 on: December 08, 2016, 10:12:49 am »
Note that global integer variables (heap allocated) ARE in fact initialized to 0, afaik.
Local variables (stack allocated) are not.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Initialization of variables
« Reply #3 on: December 08, 2016, 05:51:06 pm »
I think objects have their fields set to zero on Create.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Initialization of variables
« Reply #4 on: December 08, 2016, 05:58:30 pm »
I think objects have their fields set to zero on Create.

Classes have, objects have not.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Initialization of variables
« Reply #5 on: December 08, 2016, 06:43:13 pm »
Managed types (ansi and Unicode strings, dynamic arrays, non-Corba interfaces), global or not, are also initialised by compiler-inserted code, I believe.

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Initialization of variables
« Reply #6 on: December 09, 2016, 02:26:50 am »
I think objects have their fields set to zero on Create.

Classes have, objects have not.

I meant the object that is created by instantiating a class. (Not the old object type.)

 

TinyPortal © 2005-2018