Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-04

Pointers in Pascal!!

13 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Zaka E-Lab

I have something like this:

PStruct = ^Struct;
Struct = record
          num    : integer;
          end;

I have a unit to handle that structure ( is a bit more complicated, but is a good example), in this unit I have a procedure that deletes ( disposes ) the pointer. How can I know if a pointer are created or not?, I mean, I don't wont to try to delete something that has not been created yet, but I don't know how to do it, if that is possible.

Thanks a lot.

Zaka.

Reply by email

Leonardo M. Ram�
to Zaka E-Lab

You can assign "nil" to the pointer before using it, then check if PStruct = nil it wasn't created.

Leonardo.

--- Zaka E-Lab wrote 20 lines
I have something like this: PStruct = ^Struct; Struct = record num : integer; end; I have a unit to handle that structure ( is a bit more complicated, but is a good example), in this unit I have a procedure that deletes ( disposes ) the pointer. How can I know if a pointer are created or not?, I mean, I don't wont to try to delete something that has not been created yet, but I don't know how to do it, if that is possible. Thanks a lot. Zaka. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

____________________________________________________________________________________ Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Reply by email

Zaka E-Lab
to Leonardo M. Ram�

Maybe I haven't explain it correctly. What I want is to construct a Unit that contains some procedures, one of them is create(var p : PStruct), and other is delete(var p : PStruct).

I don't create anything, the memory is allocated by the create procedure. How can the procedure know if the argument is a pointer already allocated or not?

I hope I've explain good enough.

Thanks.

Zaka.

Leonardo M. Ramé wrote
You can assign "nil" to the pointer before using it, then check if PStruct = nil it wasn't created. Leonardo.

Reply by email

Jonas Maebe
to Zaka E-Lab
Zaka E-Lab wrote
I don't create anything, the memory is allocated by the create procedure. How can the procedure know if the argument is a pointer already allocated or not?

It cannot know that without help. You have to do such bookkeeping yourself (e.g., as mentioned before, by assigning nil to all pointers when declaring them). That is one of the basic properties of a language with manual memory management.

Jonas

Reply by email

Zaka E-Lab
to Jonas Maebe

It's possible to give a default initialization value to a defined type? I suppose that it isn't, but it would be to easy to resolve that trouble that way.

Anyway, I appreciate your help and support.

Thanks.

Zaka.

Jonas Maebe wrote 7 lines
It cannot know that without help. You have to do such bookkeeping yourself (e.g., as mentioned before, by assigning nil to all pointers when declaring them). That is one of the basic properties of a language with manual memory management. Jonas

Reply by email

Joao Morais
to Jonas Maebe
Jonas Maebe wrote 6 lines
On 29 Apr 2008, at 15:35, Zaka E-Lab wrote: > It's possible to give a default initialization value to a defined type? No, that is not possible.

Except if your pointer is declared as a member of a class (which is always assigned to nil by default)

Reply by email

Hans Mårtensson
to Joao Morais
Joao Morais wrote 13 lines
Jonas Maebe wrote: > > On 29 Apr 2008, at 15:35, Zaka E-Lab wrote: > >> It's possible to give a default initialization value to a defined type? > > > No, that is not possible. Except if your pointer is declared as a member of a class (which is always assigned to nil by default)

But that would not work after the pointer was used and then it's memory freed.
So a better way might be:

Always when declaring pointers do it this way:

var p:  ^sometype = nil;

Then in stead of using the freemem, define your own procedure:

procedure myfreemem(var p: pointer);
begin if pnil then begin freemem(p); p:=nil end end;

(I would regard this a Pascal-improvement?)

Hans Mårtensson

Reply by email

Alan Krause
to Hans Mårtensson
Hans Mårtensson wrote 12 lines
But that would not work after the pointer was used and then it's memory freed. So a better way might be: Always when declaring pointers do it this way: var p: ^sometype = nil; Then in stead of using the freemem, define your own procedure: procedure myfreemem(var p: pointer); begin if pnil then begin freemem(p); p:=nil end end;

Use FreeAndNil() -

http://www.delphibasics.co.uk/RTL.asp?Name=FreeAndNil

Alan Krause

Reply by email

Marc Weustink
to Alan Krause
Alan Krause wrote 17 lines
Hans Mårtensson wrote: > But that would not work after the pointer was used and then it's > memory freed. > So a better way might be: > > Always when declaring pointers do it this way: > > var p: ^sometype = nil; > > Then in stead of using the freemem, define your own procedure: > > procedure myfreemem(var p: pointer); > begin if pnil then begin freemem(p); p:=nil end end; Use FreeAndNil() - http://www.delphibasics.co.uk/RTL.asp?Name=FreeAndNil

FreeAndNil works only for classes, not for objects/record

Marc

Reply by email

Mattias Gaertner
to Marc Weustink
Show quoted text 20 lines
Alan Krause wrote: > Hans Mårtensson wrote: >> But that would not work after the pointer was used and then it's >> memory freed. >> So a better way might be: >> >> Always when declaring pointers do it this way: >> >> var p: ^sometype = nil; >> >> Then in stead of using the freemem, define your own procedure: >> >> procedure myfreemem(var p: pointer); >> begin if pnil then begin freemem(p); p:=nil end end; > > Use FreeAndNil() - > > http://www.delphibasics.co.uk/RTL.asp?Name=FreeAndNil FreeAndNil works only for classes, not for objects/record
ReAllocMem(p,0);

Mattias

Reply by email

Marc Weustink
to Mattias Gaertner
Mattias Gaertner wrote 23 lines
On Wed, 30 Apr 2008 01:22:47 +0200 Marc Weustink wrote: > Alan Krause wrote: >> Hans Mårtensson wrote: >>> But that would not work after the pointer was used and then it's >>> memory freed. >>> So a better way might be: >>> >>> Always when declaring pointers do it this way: >>> >>> var p: ^sometype = nil; >>> >>> Then in stead of using the freemem, define your own procedure: >>> >>> procedure myfreemem(var p: pointer); >>> begin if pnil then begin freemem(p); p:=nil end end; >> Use FreeAndNil() - >> >> http://www.delphibasics.co.uk/RTL.asp?Name=FreeAndNil > FreeAndNil works only for classes, not for objects/record ReAllocMem(p,0);

Ah, now I understand why you use them :)

Anyway does it set p:=nil ?

Marc

Reply by email