[Gambas-user] Tests against pointers in Gambas3

Jussi Lahtinen jussi.lahtinen at ...626...
Fri Jan 14 18:35:59 CET 2011


I'm not sure how much this has significance in Basic, but it is good habit.
And maybe it should be done internally in Free()..?

Jussi



On Fri, Jan 14, 2011 at 13:42, Demosthenes Koptsis
<demosthenesk at ...626...>wrote:

> hello again list,
>
> i am finishing the chapter of pointers soon.
>
> Something last about them.
>
> In C++ books it is suggested after the usage of a pointer to free it and
> then assign value 0.
>
> Do you think this advice could be done to Gambas3 like this for safe
> programming?
>
> Close #hMemory
> Free(pPointer)
> pPointer = 0
>
>
> -----Example ---------------------------------
>
> ' Gambas module file
>
> Public Sub Main()
>
> Dim pPointer As Pointer
> Dim hMemory As Stream
>
> pPointer = Alloc(4)
>
> Print pPointer
>
> hMemory = Memory pPointer For Read Write
>
> Write #hMemory, CByte(0) As Byte
> Write #hMemory, CByte(1) As Byte
> Write #hMemory, CByte(0) As Byte
> Write #hMemory, CByte(1) As Byte
>
> Close #hMemory
> Free(pPointer)
> pPointer = 0
>
> Print pPointer
> Print Byte@(pPointer)
>
> End
> -----Example ---------------------------------
>
> Notice with this implementation the last line
> Print Byte@(pPointer)
> raises an expected error.
> Otherwise we still could use the address of pPointer.
>
>
> On Wed, 2011-01-12 at 17:31 +0100, tobias wrote:
> > hi,
> >
> > > Hi,
> > >
> > > i study the cases of pointers and i found at this page
> > >
> http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html
> > >
> > > cases of program crashes from bad usage of pointers.
> > >
> > > i made a test for
> > > Attempting to write to memory already freed.
> > >
> > > ...
> > >
> > > in this example i free the pointer and then try to write to it.
> > > The result is that i can write and read normally after Free(pPointer)
> > >
> > > Is this ok?
> > >
> >
> > i noticed the same thing some time ago with a c program. this hasn't to
> > be a bug in gambas... i think this is just as jussi said:
> >  > Seems that Gambas still owns that address, but it is not "protected"
> > anymore
> >
> >
> ------------------------------------------------------------------------------
> > Protect Your Site and Customers from Malware Attacks
> > Learn about various malware tactics and how to avoid them. Understand
> > malware threats, the impact they can have on your business, and how you
> > can protect your company and customers by using code signing.
> > http://p.sf.net/sfu/oracle-sfdevnl
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
>
> --
> Regards,
> Demosthenes Koptsis.
>
>
>
> ------------------------------------------------------------------------------
> Protect Your Site and Customers from Malware Attacks
> Learn about various malware tactics and how to avoid them. Understand
> malware threats, the impact they can have on your business, and how you
> can protect your company and customers by using code signing.
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list