Initializing Records Automatically

classic Classic list List threaded Threaded
29 messages Options
12
L-9
Reply | Threaded
Open this post in threaded view
|

Initializing Records Automatically

L-9
http://stanleyxu2005.blogspot.com/2008/01/potential-memory-leak-by-initializing.html

Interesting..

So with

procedure InitRecord(out R; SizeOfRecord: Integer);
begin
  FillChar(R, SizeOfRecord, #0);
end;

Even if it has dynamic array, ansistrings, shortstrings, integers,

I assume it is okay to initialize it this way (assumptions are bad).

The problem: initializing records with automated types by going through
each and every field is error prone because if you expand the record
later on with more fields, you might forget to update your custom init
procedure..

And a question: does old borland style "object" initialize anything as
classes do? Stack based ones? heap ones?

Interesting pondering: what would be nicee is an auto initialized record:

procedure example;
var something: somerec autoinit;
begin
end;

Many times I find myself initializing stuff tediously and error prone
when you do it a human way without automation.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

Florian Klaempfl
L schrieb:

> http://stanleyxu2005.blogspot.com/2008/01/potential-memory-leak-by-initializing.html
>
>
> Interesting..
>
> So with
>
> procedure InitRecord(out R; SizeOfRecord: Integer);
> begin
>  FillChar(R, SizeOfRecord, #0);
> end;
>
> Even if it has dynamic array, ansistrings, shortstrings, integers,
>
> I assume it is okay to initialize it this way (assumptions are bad).
>
> The problem: initializing records with automated types by going through
> each and every field is error prone because if you expand the record
> later on with more fields, you might forget to update your custom init
> procedure..
>
> And a question: does old borland style "object" initialize anything as
> classes do? Stack based ones? heap ones?
>
> Interesting pondering: what would be nicee is an auto initialized record:
>
> procedure example;
> var something: somerec autoinit;
> begin
> end;
>
> Many times I find myself initializing stuff tediously and error prone
> when you do it a human way without automation.

Records and objects containing automated fields are initialized? Or do I
miss something? The whole blog post is nonsense, the proper way to
finalize a record before cleaning it with fillchar is calling finalize
with the record.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

Marco van de Voort
> L schrieb:
> >
> > Many times I find myself initializing stuff tediously and error prone
> > when you do it a human way without automation.
>
> Records and objects containing automated fields are initialized?

Are they? Or only the automated fields?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

ik-6
In reply to this post by L-9
Since when are you using fillchar on a string in Pascal ? that's a C approach.

BTW Most Pascal's string functions will stop on the first #0 they
encounter, at least the Delphi's tool that I remember.

I think he misses the entire point in his blog.

Ido

On Mon, Mar 24, 2008 at 1:20 PM, L <[hidden email]> wrote:

> http://stanleyxu2005.blogspot.com/2008/01/potential-memory-leak-by-initializing.html
>
>  Interesting..
>
>  So with
>
>  procedure InitRecord(out R; SizeOfRecord: Integer);
>  begin
>   FillChar(R, SizeOfRecord, #0);
>  end;
>
>  Even if it has dynamic array, ansistrings, shortstrings, integers,
>
>  I assume it is okay to initialize it this way (assumptions are bad).
>
>  The problem: initializing records with automated types by going through
>  each and every field is error prone because if you expand the record
>  later on with more fields, you might forget to update your custom init
>  procedure..
>
>  And a question: does old borland style "object" initialize anything as
>  classes do? Stack based ones? heap ones?
>
>  Interesting pondering: what would be nicee is an auto initialized record:
>
>  procedure example;
>  var something: somerec autoinit;
>  begin
>  end;
>
>  Many times I find myself initializing stuff tediously and error prone
>  when you do it a human way without automation.
>  _______________________________________________
>  fpc-pascal maillist  -  [hidden email]
>  http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



--
http://ik.homelinux.org/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

L-9
In reply to this post by L-9
Florian wrote..
> Records and objects containing automated fields are initialized? Or do I
> miss something?

Oh...?
This is something I have never been clear about :-(

So a record with an ansistring and an integer.. for example even local scope..

Will be all zeros?

procedure test;
var somerec: Tsomerec;
begin
  // these checks are okay?
  if somerec.astring = ''
  if somerec.someint = 0

end;

i.e. above is not random data?

The reason I ask is because some times i have had odd buggy behavior when I use local scope records... but when I use a global scope one it is okay..

If this is some bug or screwy code of mine I want to find out and solve this.






_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Initializing Records Automatically

Jonas Maebe-2

On 24 Mar 2008, at 12:53, L wrote:

> So a record with an ansistring and an integer.. for example even  
> local scope..
>
> Will be all zeros?
>
> procedure test;
> var somerec: Tsomerec;
> begin
> // these checks are okay?
> if somerec.astring = ''  if somerec.someint = 0
>
> end;
>
> i.e. above is not random data?

The ansistring is initialised on entry, the someint is not. The reason  
that refcounted fields simply have to be initialised, is that  
otherwise you'd get a crash as soon as you'd try to assign something  
to them (since an assignment is basically "increfcount(source);  
decrefcount(target); pointer(target):=pointer(source)", which would go  
horribly wrong if target could contain garbage).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

Jonas Maebe-2
In reply to this post by ik-6

On 24 Mar 2008, at 12:03, ik wrote:
> Since when are you using fillchar on a string in Pascal ? that's a C  
> approach.

He's using it on a record. Initialising records with fillchar  
(regardless of what they contain) is quite common in Pascal as well.  
And as Florian mentioned, if you do this for a record containing  
refcounted fields, you first have to call finalize() on it.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

L-9
In reply to this post by L-9
L wrote:
> Florian wrote..
>> Records and objects containing automated fields are initialized? Or do I
>> miss something?
>
> Oh...?
> This is something I have never been clear about :-(
>

p.s. see also:

http://community.freepascal.org:10000/bboards/message?message_id=223327&forum_id=24082

So. let's clear up the confusion for good..

Ansistrings are initialized in the record?
But integers too?

Just ansistrings but not integers?

Then, I will write a clear article explaining it all for everyone to
reference ;-)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Initializing Records Automatically

Marco van de Voort
> L wrote:
> Ansistrings are initialized in the record?
> But integers too?

Only automated types are initialized/finalized, the rest not. So the blog
post has a point, it is something to look out for.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

L-9
In reply to this post by L-9

I think confusion was created also because that guys blog post only had
a record with one field in it (a string).. he should have used more
fields with integers, pointers, etc to demonstrate a point more.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

Re: Initializing Records Automatically

L-9
One thing to ponder about:

A class inside a record...
trecord = record
  something: TSomeClass;
  int: integer;
  //... etc
end;

My brain hurts.

This will cause issues with a Fillchar for the class, or not?

I wouldn't use this normally (a class inside a record), but it is still
interesting to think about.. for a standardized initialization system
for records..

The idea is.. I want to create/ponder about a "standardized" way to init
ANY sort of record because a lot of silly bugs in pascal are because of
these uninitialized local scope records, or similar non initialized things.

We need to improve safety of our programs through automated methods..we
aren't programming in C!

(I recently noticed the compiler has some detection switch for
uninitialized variables and have yet to try it.. sounds like a cool
testing ability)

It is nice to have the performance benefit of non initialized records
*but* at the same time it is nice to use SAFE features explicitely if we
can.

I am *all for* an "autoinit" record switch feature, but then this breaks
compatibility with delphi if such a feature is thought up. Maybe I
suggest it in the "Delphi Wikia" website too just as useful pondering if
anything.

procedure test;
var rec: AnyRecEvenWithInts AUTOINIT;
begin
end;

Are there are issues to think about if the classes are stored inside
that record ? That may mean it is not such an easy feature to
implement... or is it?

In the mean time, I will experiment with the fillchar/finalize tricks
with "simple" records containing ansistrings and integers and such
things. I didn't really have sincerity about it, or a correct
standardized way to do it, until discussing on this list regarding
finalize() and such things.  Honestly, I didn't even know much about
fillchar due to my lack of "turbopascal record" experience. I've always
been manually initializing my records and a pain in the butt that is.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: Initializing Records Automatically

Jonas Maebe-2

On 24 Mar 2008, at 14:10, L wrote:

> One thing to ponder about:
>
> A class inside a record...
> trecord = record
> something: TSomeClass;
> int: integer;
> //... etc
> end;
>
> My brain hurts.

A class field is just a pointer. Since classes are not reference  
counted, nothing special is done for record fields which are classes.

> This will cause issues with a Fillchar for the class, or not?

No.

> The idea is.. I want to create/ponder about a "standardized" way to  
> init ANY sort of record because a lot of silly bugs in pascal are  
> because of these uninitialized local scope records, or similar non  
> initialized things.

If you have local record which was declared but not yet used, a simple  
fillchar(rec,sizeof(rec),0) will set everything to 0/nil/empty. If it  
may have been used earlier and contains refcounted fields, you first  
have to call finalize(rec).

Or you can let the compiler decide whether or not finalize() is  
required by using a wrapper with an out-parameter like the blog guy did.

>

Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Erlang/YAWS vs Free Pascal/Xitami

Jilani Khaldi-2
In reply to this post by L-9
Which is better for my web application?
http://www.dotpas/org/cgi-bin/articles/a01

--
***
Jilani KHALDI
http://www.dotpas.org
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Erlang/YAWS vs Free Pascal/Xitami

Jilani Khaldi-2
Jilani Khaldi wrote:
> Which is better for my web application?
> http://www.dotpas/org/cgi-bin/articles/a01
Soory, the URL is:
http://www.dotpas.org/cgi-bin/articles/a01

--
***
Jilani KHALDI
http://www.dotpas.org
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Erlang/YAWS vs Free Pascal/Xitami

Jonas Maebe-2

On 24 Mar 2008, at 16:53, Jilani Khaldi wrote:
> Jilani Khaldi wrote:
>> Which is better for my web application?
>> http://www.dotpas/org/cgi-bin/articles/a01
> Soory, the URL is:
> http://www.dotpas.org/cgi-bin/articles/a01

I guess the main problem is that you are writing Pascal in a way which  
is very suited for a functional language (like Erlang), but which is  
extremely inefficient when used in an imperative language like Pascal  
(or C, C++, Java, ...).

Anyway, I don't think such posts are very much suited to this list  
(unless the idea is to instigate endless discussions about "which  
language is best"), so I'd prefer it if you could take this to the fpc-
other list.

Thanks.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

classes initialization

L-9
In reply to this post by L-9
Recently we discussed records and how to initialize them.. Jonas cleared
up a lot for me (and Marco/Florian etc.). Thanks much..

So.. now I ask..

type something = class
 s: ansistring;
 int:integer
end;


Is "int" set to zero when it is a local scope var in a procedure created
on the heap with Create()?

Or just the string is initialized since it is automated?


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: classes initialization

Jonas Maebe-2

On 25 Mar 2008, at 04:27, L wrote:

> So.. now I ask..
>
> type something = class
> s: ansistring;
> int:integer
> end;
>
> Is "int" set to zero when it is a local scope var in a procedure  
> created on the heap with Create()?

New class instances are always automatically filled with zeroes upon  
creation.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
L-9
Reply | Threaded
Open this post in threaded view
|

Re: classes initialization

L-9
In reply to this post by L-9
What about old borland objects (heap and stack initialization) process?

I am guessing they are more like records... or do heap objects act like
classes in that they are filled with zeros?

p.s. I think the inconsistent behavior between local scope records and
global scope records is kind of a flaw/danger in modern pascal.. as the
bugs may not be caught until someone builds a program with local scope
var.. and for eg. many demonstrations are built in simple global console
programs as test cases. Nowadays I always (try to) build things inside a
procedure in a test case to ensure local scope is being used. But as
humans make mistakes, again I think this part of modern pascal can be
improved and we should push to the language less dangerous by default.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: classes initialization

Jonas Maebe-2

On 25 Mar 2008, at 13:40, L wrote:

> What about old borland objects (heap and stack initialization)  
> process?
>
> I am guessing they are more like records...

They are exactly like records.

> or do heap objects act like classes in that they are filled with  
> zeros?

Unlike classes, objects have no hidden dereferencing or whatever and  
follow exactly the same semantics as other types regardless of whether  
they are on the stack or on the heap.

> p.s. I think the inconsistent behavior between local scope records  
> and global scope records is kind of a flaw/danger in modern pascal..  
> as the bugs may not be caught until someone builds a program with  
> local scope var..

Using uninitialised variables is virtually always bad, regardless of  
the scope. And the fact that global variables are zeroed at the  
program start is afaik not defined by the Pascal standard. It's just a  
side effect of the way most operating systems work.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Re: classes initialization

M Pulis
>
>> p.s. I think the inconsistent behavior between local scope records  
>> and global scope records is kind of a flaw/danger in modern  
>> pascal.. as the bugs may not be caught until someone builds a  
>> program with local scope var..
>
> Using uninitialised variables is virtually always bad, regardless  
> of the scope. And the fact that global variables are zeroed at the  
> program start is afaik not defined by the Pascal standard. It's  
> just a side effect of the way most operating systems work.
>


Affirmative.

Pascal does not define any variable initialization itself - one  
should always init all Pascal vars of any type. Doing so is an  
excellent coding habit also portable to any other language/OS -  
protects against low-level implementation changes.

I never trust a side effect I didn't code myself..... 8-))

Gary
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12