Discussion:
[Fwd: Gambas3 Pointers example]
(too old to reply)
Demosthenes Koptsis
2011-01-08 16:09:34 UTC
Permalink
But if i write,

iA = Integer@(pPointer)
Print iA

then iA gets the value 10.

why is that?

what's the difference between
iA = Read #hMemory As Integer
iA = Integer@(pPointer)

?
--
Regards,
Demosthenes Koptsis.
Jussi Lahtinen
2011-01-08 18:40:27 UTC
Permalink
Behaviour confirmed with Gambas 3 rev 3447 @ Ubuntu 10.10 64bit.
Though I don't think this is bug, since stream pointer position moves after
write.
"Seek #hMemory, 0" will fix that, in this case. See more from documentation
of Seek.

However I don't know how Interger@ gets correct position...

Jussi
Post by Demosthenes Koptsis
But if i write,
Print iA
then iA gets the value 10.
why is that?
what's the difference between
iA = Read #hMemory As Integer
?
--
Regards,
Demosthenes Koptsis.
---------- Forwarded message ----------
Date: Sat, 08 Jan 2011 10:32:12 +0200
Subject: Gambas3 Pointers example
Hello,
i remake an example for pointers in Gambas3-svn3432, ubuntu 10.04 LTS,
AMD64
-------------------------
' Gambas module file
Public Sub Main()
Dim pPointer As Pointer
Dim hMemory As Stream
Dim iA As Integer
pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
iA = Read #hMemory As Integer
Print iA
Free(pPointer)
End
-------------------------
the iA var when it prints is not 10 but something else like 0 or 32621
or similar.
What am i doing wrong?
--
Regards,
Demosthenes Koptsis.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any
company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Jussi Lahtinen
2011-01-08 18:43:46 UTC
Permalink
Argh... of course:

This uses internal counter:
iA = Read #hMemory As Integer

This uses pPointer as address:
iA = Integer@(pPointer)


Jussi
Post by Jussi Lahtinen
Though I don't think this is bug, since stream pointer position moves after
write.
"Seek #hMemory, 0" will fix that, in this case. See more from documentation
of Seek.
Jussi
Post by Demosthenes Koptsis
But if i write,
Print iA
then iA gets the value 10.
why is that?
what's the difference between
iA = Read #hMemory As Integer
?
--
Regards,
Demosthenes Koptsis.
---------- Forwarded message ----------
Date: Sat, 08 Jan 2011 10:32:12 +0200
Subject: Gambas3 Pointers example
Hello,
i remake an example for pointers in Gambas3-svn3432, ubuntu 10.04 LTS,
AMD64
-------------------------
' Gambas module file
Public Sub Main()
Dim pPointer As Pointer
Dim hMemory As Stream
Dim iA As Integer
pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
iA = Read #hMemory As Integer
Print iA
Free(pPointer)
End
-------------------------
the iA var when it prints is not 10 but something else like 0 or 32621
or similar.
What am i doing wrong?
--
Regards,
Demosthenes Koptsis.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any
company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Demosthenes Koptsis
2011-01-08 19:46:32 UTC
Permalink
Thanks Jussi!

It's nice to understand the language.
I didn't say that there was a bug, my mind just did not go to the
behavior of WRITE instruction, which it moved the stream pointer after
writing the data.

I remade the example to show this detail for better comprehension.

-------------------------------------
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream
Dim iA As Integer

pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write

Write #hMemory, 10 As Integer

'get stream pointer position
Print Seek(hMemory) '4

'set stream pointer potition
Seek #hMemory, 0

'get stream pointer position
Print Seek(hMemory) '0

'read from memory stream
iA = Read #hMemory As Integer
Print iA '10

'read from address
iA = Integer@(pPointer)
Print iA '10

Free(pPointer)
End
-------------------------------------

something else, as i read the docs i found a way to write to a pointer
by instructions

hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer

is there any other way that i did not notice?

there is also PRINT to stream but as i see it converts the expression to
string with Str$, so it is not a good option.
Post by Demosthenes Koptsis
iA = Read #hMemory As Integer
Jussi
Post by Jussi Lahtinen
Though I don't think this is bug, since stream pointer position moves after
write.
"Seek #hMemory, 0" will fix that, in this case. See more from documentation
of Seek.
Jussi
Post by Demosthenes Koptsis
But if i write,
Print iA
then iA gets the value 10.
why is that?
what's the difference between
iA = Read #hMemory As Integer
?
--
Regards,
Demosthenes Koptsis.
---------- Forwarded message ----------
Date: Sat, 08 Jan 2011 10:32:12 +0200
Subject: Gambas3 Pointers example
Hello,
i remake an example for pointers in Gambas3-svn3432, ubuntu 10.04 LTS,
AMD64
-------------------------
' Gambas module file
Public Sub Main()
Dim pPointer As Pointer
Dim hMemory As Stream
Dim iA As Integer
pPointer = Alloc(SizeOf(gb.Integer))
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
iA = Read #hMemory As Integer
Print iA
Free(pPointer)
End
-------------------------
the iA var when it prints is not 10 but something else like 0 or 32621
or similar.
What am i doing wrong?
--
Regards,
Demosthenes Koptsis.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any
company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Jussi Lahtinen
2011-01-08 20:07:28 UTC
Permalink
Post by Demosthenes Koptsis
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
is there any other way that i did not notice?
I'm not sure I understand what do you mean/need...
Documentation gives two options;

*WRITE* [ *#* *Stream* *,* ] *Expression* *AS* *Datatype*
*WRITE* [ *#* *Stream* *,* ] *String* [ *,* *Length* ]

But I personally use streams just for reading data from external functions.
So, in other words, my third option is to pass pointer to external function
and let it write to stream.

You can see example from my GambasTester project (attached), just search
"modf".
Here is C++ reference:
http://www.cplusplus.com/reference/clibrary/cmath/modf/

Jussi
P.S. Test will not pass right now, because some new functions (possibly
obsolete) are missing from test.
Demosthenes Koptsis
2011-01-08 21:07:19 UTC
Permalink
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
is there any other way that i did not notice?
thanks Jussi.
i just wondered if there is something else than write instruction for
writing into a pointer.

But as i see a pointer is manipulated by stream read/write instructions.

Never mind just exploring Gambas3 and docs.
Post by Jussi Lahtinen
I'm not sure I understand what do you mean/need...
Documentation gives two options;
*WRITE* [ *#* *Stream* *,* ] *Expression* *AS* *Datatype*
*WRITE* [ *#* *Stream* *,* ] *String* [ *,* *Length* ]
But I personally use streams just for reading data from external functions.
So, in other words, my third option is to pass pointer to external function
and let it write to stream.
You can see example from my GambasTester project (attached), just search
"modf".
http://www.cplusplus.com/reference/clibrary/cmath/modf/
Jussi
P.S. Test will not pass right now, because some new functions (possibly
obsolete) are missing from test.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-09 10:12:08 UTC
Permalink
Here is another question-case.

i Alloc 12 bytes.
i set in memory an integer 4 bytes
then i set in memory a float 8 bytes.

i can read the memory stream with READ, ok!

How can i to this with Integer@() and Float@() functions?
How can i move inside memory and use Integer@() and Float@()

example:
--------------------------
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer
Dim hMemory As Stream
Dim iA As Integer
Dim fA As Float

pPointer = Alloc(12)
hMemory = Memory pPointer For Read Write

Print "Stream pointer potition at:"; Seek(hMemory)
Print "Writing to memory stream a 4 byte integer"
Write #hMemory, 10 As Integer
Print "Stream pointer potition at:"; Seek(hMemory)

Print "Writing to memory stream a 8 byte float"
Write #hMemory, 4.2 As Float
Print "Stream pointer potition at:"; Seek(hMemory)

Print "Set stream potition at the begining"
Seek #hMemory, 0

Print "Stream pointer potition at:"; Seek(hMemory)

Print "Read the integer"
iA = Read #hMemory As Integer
Print iA
Print "Stream pointer potition at:"; Seek(hMemory)

Print "Read the float"
fA = Read #hMemory As Float
Print fA
Print "Stream pointer potition at:"; Seek(hMemory)

Print "\nRead the pointer"
Print "Integer read:"; Integer@(pPointer)
Print "Float read:"; Float@(pPointer)

Free(pPointer)

End
--------------------------

output:
--------------------------
Stream pointer potition at:0
Writing to memory stream a 4 byte integer
Stream pointer potition at:4
Writing to memory stream a 8 byte float
Stream pointer potition at:12
Set stream potition at the beginning
Stream pointer potition at:0
Read the integer
10
Stream pointer potition at:4
Read the float
4.2
Stream pointer potition at:12

Read the pointer
Integer read:10
Float read:-9.25596411572894E+61
--------------------------
Post by Demosthenes Koptsis
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
hMemory = Memory pPointer For Read Write
Write #hMemory, 10 As Integer
is there any other way that i did not notice?
thanks Jussi.
i just wondered if there is something else than write instruction for
writing into a pointer.
But as i see a pointer is manipulated by stream read/write instructions.
Never mind just exploring Gambas3 and docs.
Post by Jussi Lahtinen
I'm not sure I understand what do you mean/need...
Documentation gives two options;
*WRITE* [ *#* *Stream* *,* ] *Expression* *AS* *Datatype*
*WRITE* [ *#* *Stream* *,* ] *String* [ *,* *Length* ]
But I personally use streams just for reading data from external functions.
So, in other words, my third option is to pass pointer to external function
and let it write to stream.
You can see example from my GambasTester project (attached), just search
"modf".
http://www.cplusplus.com/reference/clibrary/cmath/modf/
Jussi
P.S. Test will not pass right now, because some new functions (possibly
obsolete) are missing from test.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
--
Regards,
Demosthenes Koptsis.
Demosthenes Koptsis
2011-01-09 13:59:08 UTC
Permalink
this example on pointers raises signal #6.
Gambas3 svn3432 Ubuntu 10.04, AMD64

---------------------------
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"

Public Sub Main()

Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String

'getwd
pBuf = Alloc(SizeOf(gb.String))
pFunc = Alloc(SizeOf(gb.String))

pFunc = getwd(pBuf)

sWorkingDirectory = Str@(pFunc)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory

End
---------------------------

output:
---------------------------
*** glibc detected *** Pointers86: free(): invalid next size (fast):
0x0000000001af39a0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4d6a2495b6]
/lib/libc.so.6(cfree+0x73)[0x7f4d6a24fe83]
Pointers86[0x405308]
Pointers86[0x422f50]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4d6a1f0c4d]
Pointers86[0x4043f9]
======= Memory map: ========
00400000-00445000 r-xp 00000000 08:14
2377106 /usr/local/bin/gbx3
00644000-00645000 r--p 00044000 08:14
2377106 /usr/local/bin/gbx3
00645000-0064f000 rw-p 00045000 08:14
2377106 /usr/local/bin/gbx3
0064f000-00651000 rw-p 00000000 00:00 0
01ade000-01aff000 rw-p 00000000 00:00 0
[heap]
7f4d64000000-7f4d64021000 rw-p 00000000 00:00 0
7f4d64021000-7f4d68000000 ---p 00000000 00:00 0
7f4d69fbb000-7f4d69fd1000 r-xp 00000000 08:14
1179728 /lib/libgcc_s.so.1
7f4d69fd1000-7f4d6a1d0000 ---p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d0000-7f4d6a1d1000 r--p 00015000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d1000-7f4d6a1d2000 rw-p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d2000-7f4d6a34c000 r-xp 00000000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a34c000-7f4d6a54b000 ---p 0017a000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54b000-7f4d6a54f000 r--p 00179000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54f000-7f4d6a550000 rw-p 0017d000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a550000-7f4d6a555000 rw-p 00000000 00:00 0
7f4d6a555000-7f4d6a55c000 r-xp 00000000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a55c000-7f4d6a75b000 ---p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75b000-7f4d6a75c000 r--p 00006000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75c000-7f4d6a75d000 rw-p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75d000-7f4d6a775000 r-xp 00000000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a775000-7f4d6a974000 ---p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a974000-7f4d6a975000 r--p 00017000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a975000-7f4d6a976000 rw-p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a976000-7f4d6a97a000 rw-p 00000000 00:00 0
7f4d6a97a000-7f4d6a97c000 r-xp 00000000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6a97c000-7f4d6ab7c000 ---p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7c000-7f4d6ab7d000 r--p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7d000-7f4d6ab7e000 rw-p 00003000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7e000-7f4d6ac00000 r-xp 00000000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ac00000-7f4d6adff000 ---p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6adff000-7f4d6ae00000 r--p 00081000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae00000-7f4d6ae01000 rw-p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae01000-7f4d6ae21000 r-xp 00000000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6ae95000-7f4d6ae96000 rw-p 00000000 00:00 0
7f4d6ae96000-7f4d6aed5000 r--p 00000000 08:14
2237984 /usr/lib/locale/en_US.utf8/LC_CTYPE
7f4d6aed5000-7f4d6aff3000 r--p 00000000 08:14
2239899 /usr/lib/locale/en_US.utf8/LC_COLLATE
7f4d6aff3000-7f4d6aff7000 rw-p 00000000 00:00 0
7f4d6b00b000-7f4d6b00c000 r--p 00000000 08:14
2237989 /usr/lib/locale/en_US.utf8/LC_NUMERIC
7f4d6b00c000-7f4d6b00d000 r--p 00000000 08:14
2232077 /usr/lib/locale/en_US.utf8/LC_TIME
7f4d6b00d000-7f4d6b00e000 r--p 00000000 08:14
2234328 /usr/lib/locale/en_US.utf8/LC_MONETARY
7f4d6b00e000-7f4d6b00f000 r--p 00000000 08:14
2235935 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f4d6b00f000-7f4d6b010000 r--p 00000000 08:14
2237938 /usr/lib/locale/en_US.utf8/LC_PAPER
7f4d6b010000-7f4d6b011000 r--p 00000000 08:14
2237858 /usr/lib/locale/en_US.utf8/LC_NAME
7f4d6b011000-7f4d6b012000 r--p 00000000 08:14
2235936 /usr/lib/locale/en_US.utf8/LC_ADDRESS
7f4d6b012000-7f4d6b013000 r--p 00000000 08:14
2235937 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
7f4d6b013000-7f4d6b014000 r--p 00000000 08:14
2239858 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
7f4d6b014000-7f4d6b01b000 r--s 00000000 08:14
2233563 /usr/lib/gconv/gconv-modules.cache
7f4d6b01b000-7f4d6b01c000 r--p 00000000 08:14
2235940 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
7f4d6b01c000-7f4d6b020000 rw-p 00000000 00:00 0
7f4d6b020000-7f4d6b021000 r--p 0001f000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b021000-7f4d6b022000 rw-p 00020000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b022000-7f4d6b023000 rw-p 00000000 00:00 0
7fff5c542000-7fff5c557000 rw-p 00000000 00:00 0
[stack]
7fff5c5ff000-7fff5c600000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
/mnt/store/LINUX/DOCUMENTS/DEVELOPMENT/Gambas/Προγραμματισμός σε
Gambas/Το βιβλίο της Gambas3/Projects/Pointers86
---------------------------

the last line is in Greek some part, it is the name of current
directory.
Jussi Lahtinen
2011-01-09 14:05:21 UTC
Permalink
Hmmm..... you cannot do alloc with size of string.
Size of string is depended on length of string!

Jussi
Post by Demosthenes Koptsis
this example on pointers raises signal #6.
Gambas3 svn3432 Ubuntu 10.04, AMD64
---------------------------
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(SizeOf(gb.String))
pFunc = Alloc(SizeOf(gb.String))
pFunc = getwd(pBuf)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory
End
---------------------------
---------------------------
0x0000000001af39a0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4d6a2495b6]
/lib/libc.so.6(cfree+0x73)[0x7f4d6a24fe83]
Pointers86[0x405308]
Pointers86[0x422f50]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4d6a1f0c4d]
Pointers86[0x4043f9]
======= Memory map: ========
00400000-00445000 r-xp 00000000 08:14
2377106 /usr/local/bin/gbx3
00644000-00645000 r--p 00044000 08:14
2377106 /usr/local/bin/gbx3
00645000-0064f000 rw-p 00045000 08:14
2377106 /usr/local/bin/gbx3
0064f000-00651000 rw-p 00000000 00:00 0
01ade000-01aff000 rw-p 00000000 00:00 0
[heap]
7f4d64000000-7f4d64021000 rw-p 00000000 00:00 0
7f4d64021000-7f4d68000000 ---p 00000000 00:00 0
7f4d69fbb000-7f4d69fd1000 r-xp 00000000 08:14
1179728 /lib/libgcc_s.so.1
7f4d69fd1000-7f4d6a1d0000 ---p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d0000-7f4d6a1d1000 r--p 00015000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d1000-7f4d6a1d2000 rw-p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d2000-7f4d6a34c000 r-xp 00000000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a34c000-7f4d6a54b000 ---p 0017a000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54b000-7f4d6a54f000 r--p 00179000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54f000-7f4d6a550000 rw-p 0017d000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a550000-7f4d6a555000 rw-p 00000000 00:00 0
7f4d6a555000-7f4d6a55c000 r-xp 00000000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a55c000-7f4d6a75b000 ---p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75b000-7f4d6a75c000 r--p 00006000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75c000-7f4d6a75d000 rw-p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75d000-7f4d6a775000 r-xp 00000000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a775000-7f4d6a974000 ---p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a974000-7f4d6a975000 r--p 00017000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a975000-7f4d6a976000 rw-p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a976000-7f4d6a97a000 rw-p 00000000 00:00 0
7f4d6a97a000-7f4d6a97c000 r-xp 00000000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6a97c000-7f4d6ab7c000 ---p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7c000-7f4d6ab7d000 r--p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7d000-7f4d6ab7e000 rw-p 00003000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7e000-7f4d6ac00000 r-xp 00000000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ac00000-7f4d6adff000 ---p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6adff000-7f4d6ae00000 r--p 00081000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae00000-7f4d6ae01000 rw-p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae01000-7f4d6ae21000 r-xp 00000000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6ae95000-7f4d6ae96000 rw-p 00000000 00:00 0
7f4d6ae96000-7f4d6aed5000 r--p 00000000 08:14
2237984 /usr/lib/locale/en_US.utf8/LC_CTYPE
7f4d6aed5000-7f4d6aff3000 r--p 00000000 08:14
2239899 /usr/lib/locale/en_US.utf8/LC_COLLATE
7f4d6aff3000-7f4d6aff7000 rw-p 00000000 00:00 0
7f4d6b00b000-7f4d6b00c000 r--p 00000000 08:14
2237989 /usr/lib/locale/en_US.utf8/LC_NUMERIC
7f4d6b00c000-7f4d6b00d000 r--p 00000000 08:14
2232077 /usr/lib/locale/en_US.utf8/LC_TIME
7f4d6b00d000-7f4d6b00e000 r--p 00000000 08:14
2234328 /usr/lib/locale/en_US.utf8/LC_MONETARY
7f4d6b00e000-7f4d6b00f000 r--p 00000000 08:14
2235935
/usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f4d6b00f000-7f4d6b010000 r--p 00000000 08:14
2237938 /usr/lib/locale/en_US.utf8/LC_PAPER
7f4d6b010000-7f4d6b011000 r--p 00000000 08:14
2237858 /usr/lib/locale/en_US.utf8/LC_NAME
7f4d6b011000-7f4d6b012000 r--p 00000000 08:14
2235936 /usr/lib/locale/en_US.utf8/LC_ADDRESS
7f4d6b012000-7f4d6b013000 r--p 00000000 08:14
2235937 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
7f4d6b013000-7f4d6b014000 r--p 00000000 08:14
2239858 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
7f4d6b014000-7f4d6b01b000 r--s 00000000 08:14
2233563 /usr/lib/gconv/gconv-modules.cache
7f4d6b01b000-7f4d6b01c000 r--p 00000000 08:14
2235940 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
7f4d6b01c000-7f4d6b020000 rw-p 00000000 00:00 0
7f4d6b020000-7f4d6b021000 r--p 0001f000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b021000-7f4d6b022000 rw-p 00020000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b022000-7f4d6b023000 rw-p 00000000 00:00 0
7fff5c542000-7fff5c557000 rw-p 00000000 00:00 0
[stack]
7fff5c5ff000-7fff5c600000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
/mnt/store/LINUX/DOCUMENTS/DEVELOPMENT/Gambas/Προγραμματισμός σε
Gambas/Το βιβλίο της Gambas3/Projects/Pointers86
---------------------------
the last line is in Greek some part, it is the name of current
directory.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Demosthenes Koptsis
2011-01-09 19:29:20 UTC
Permalink
so what can i do for

char *getwd(char *buf);

?
Post by Jussi Lahtinen
Hmmm..... you cannot do alloc with size of string.
Size of string is depended on length of string!
Jussi
Post by Demosthenes Koptsis
this example on pointers raises signal #6.
Gambas3 svn3432 Ubuntu 10.04, AMD64
---------------------------
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(SizeOf(gb.String))
pFunc = Alloc(SizeOf(gb.String))
pFunc = getwd(pBuf)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory
End
---------------------------
---------------------------
0x0000000001af39a0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4d6a2495b6]
/lib/libc.so.6(cfree+0x73)[0x7f4d6a24fe83]
Pointers86[0x405308]
Pointers86[0x422f50]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4d6a1f0c4d]
Pointers86[0x4043f9]
======= Memory map: ========
00400000-00445000 r-xp 00000000 08:14
2377106 /usr/local/bin/gbx3
00644000-00645000 r--p 00044000 08:14
2377106 /usr/local/bin/gbx3
00645000-0064f000 rw-p 00045000 08:14
2377106 /usr/local/bin/gbx3
0064f000-00651000 rw-p 00000000 00:00 0
01ade000-01aff000 rw-p 00000000 00:00 0
[heap]
7f4d64000000-7f4d64021000 rw-p 00000000 00:00 0
7f4d64021000-7f4d68000000 ---p 00000000 00:00 0
7f4d69fbb000-7f4d69fd1000 r-xp 00000000 08:14
1179728 /lib/libgcc_s.so.1
7f4d69fd1000-7f4d6a1d0000 ---p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d0000-7f4d6a1d1000 r--p 00015000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d1000-7f4d6a1d2000 rw-p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d2000-7f4d6a34c000 r-xp 00000000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a34c000-7f4d6a54b000 ---p 0017a000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54b000-7f4d6a54f000 r--p 00179000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54f000-7f4d6a550000 rw-p 0017d000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a550000-7f4d6a555000 rw-p 00000000 00:00 0
7f4d6a555000-7f4d6a55c000 r-xp 00000000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a55c000-7f4d6a75b000 ---p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75b000-7f4d6a75c000 r--p 00006000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75c000-7f4d6a75d000 rw-p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75d000-7f4d6a775000 r-xp 00000000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a775000-7f4d6a974000 ---p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a974000-7f4d6a975000 r--p 00017000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a975000-7f4d6a976000 rw-p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a976000-7f4d6a97a000 rw-p 00000000 00:00 0
7f4d6a97a000-7f4d6a97c000 r-xp 00000000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6a97c000-7f4d6ab7c000 ---p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7c000-7f4d6ab7d000 r--p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7d000-7f4d6ab7e000 rw-p 00003000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7e000-7f4d6ac00000 r-xp 00000000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ac00000-7f4d6adff000 ---p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6adff000-7f4d6ae00000 r--p 00081000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae00000-7f4d6ae01000 rw-p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae01000-7f4d6ae21000 r-xp 00000000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6ae95000-7f4d6ae96000 rw-p 00000000 00:00 0
7f4d6ae96000-7f4d6aed5000 r--p 00000000 08:14
2237984 /usr/lib/locale/en_US.utf8/LC_CTYPE
7f4d6aed5000-7f4d6aff3000 r--p 00000000 08:14
2239899 /usr/lib/locale/en_US.utf8/LC_COLLATE
7f4d6aff3000-7f4d6aff7000 rw-p 00000000 00:00 0
7f4d6b00b000-7f4d6b00c000 r--p 00000000 08:14
2237989 /usr/lib/locale/en_US.utf8/LC_NUMERIC
7f4d6b00c000-7f4d6b00d000 r--p 00000000 08:14
2232077 /usr/lib/locale/en_US.utf8/LC_TIME
7f4d6b00d000-7f4d6b00e000 r--p 00000000 08:14
2234328 /usr/lib/locale/en_US.utf8/LC_MONETARY
7f4d6b00e000-7f4d6b00f000 r--p 00000000 08:14
2235935
/usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f4d6b00f000-7f4d6b010000 r--p 00000000 08:14
2237938 /usr/lib/locale/en_US.utf8/LC_PAPER
7f4d6b010000-7f4d6b011000 r--p 00000000 08:14
2237858 /usr/lib/locale/en_US.utf8/LC_NAME
7f4d6b011000-7f4d6b012000 r--p 00000000 08:14
2235936 /usr/lib/locale/en_US.utf8/LC_ADDRESS
7f4d6b012000-7f4d6b013000 r--p 00000000 08:14
2235937 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
7f4d6b013000-7f4d6b014000 r--p 00000000 08:14
2239858 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
7f4d6b014000-7f4d6b01b000 r--s 00000000 08:14
2233563 /usr/lib/gconv/gconv-modules.cache
7f4d6b01b000-7f4d6b01c000 r--p 00000000 08:14
2235940 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
7f4d6b01c000-7f4d6b020000 rw-p 00000000 00:00 0
7f4d6b020000-7f4d6b021000 r--p 0001f000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b021000-7f4d6b022000 rw-p 00020000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b022000-7f4d6b023000 rw-p 00000000 00:00 0
7fff5c542000-7fff5c557000 rw-p 00000000 00:00 0
[stack]
7fff5c5ff000-7fff5c600000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
/mnt/store/LINUX/DOCUMENTS/DEVELOPMENT/Gambas/Προγραμματισμός σε
Gambas/Το βιβλίο της Gambas3/Projects/Pointers86
---------------------------
the last line is in Greek some part, it is the name of current
directory.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-09 19:32:00 UTC
Permalink
Although i get the working directory,
Post by Demosthenes Koptsis
Post by Demosthenes Koptsis
/mnt/store/LINUX/DOCUMENTS/DEVELOPMENT/Gambas/Προγραμματισμός σε
Post by Demosthenes Koptsis
Gambas/Το βιβλίο της Gambas3/Projects/Pointers86
but maybe the implementation of
char *getwd(char *buf);

is not correct.... i dont know!
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
Post by Demosthenes Koptsis
Hmmm..... you cannot do alloc with size of string.
Size of string is depended on length of string!
Jussi
Post by Demosthenes Koptsis
this example on pointers raises signal #6.
Gambas3 svn3432 Ubuntu 10.04, AMD64
---------------------------
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(SizeOf(gb.String))
pFunc = Alloc(SizeOf(gb.String))
pFunc = getwd(pBuf)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory
End
---------------------------
---------------------------
0x0000000001af39a0 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4d6a2495b6]
/lib/libc.so.6(cfree+0x73)[0x7f4d6a24fe83]
Pointers86[0x405308]
Pointers86[0x422f50]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4d6a1f0c4d]
Pointers86[0x4043f9]
======= Memory map: ========
00400000-00445000 r-xp 00000000 08:14
2377106 /usr/local/bin/gbx3
00644000-00645000 r--p 00044000 08:14
2377106 /usr/local/bin/gbx3
00645000-0064f000 rw-p 00045000 08:14
2377106 /usr/local/bin/gbx3
0064f000-00651000 rw-p 00000000 00:00 0
01ade000-01aff000 rw-p 00000000 00:00 0
[heap]
7f4d64000000-7f4d64021000 rw-p 00000000 00:00 0
7f4d64021000-7f4d68000000 ---p 00000000 00:00 0
7f4d69fbb000-7f4d69fd1000 r-xp 00000000 08:14
1179728 /lib/libgcc_s.so.1
7f4d69fd1000-7f4d6a1d0000 ---p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d0000-7f4d6a1d1000 r--p 00015000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d1000-7f4d6a1d2000 rw-p 00016000 08:14
1179728 /lib/libgcc_s.so.1
7f4d6a1d2000-7f4d6a34c000 r-xp 00000000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a34c000-7f4d6a54b000 ---p 0017a000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54b000-7f4d6a54f000 r--p 00179000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a54f000-7f4d6a550000 rw-p 0017d000 08:14
1180003 /lib/libc-2.11.1.so
7f4d6a550000-7f4d6a555000 rw-p 00000000 00:00 0
7f4d6a555000-7f4d6a55c000 r-xp 00000000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a55c000-7f4d6a75b000 ---p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75b000-7f4d6a75c000 r--p 00006000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75c000-7f4d6a75d000 rw-p 00007000 08:14
2231491 /usr/lib/libffi.so.5.0.10
7f4d6a75d000-7f4d6a775000 r-xp 00000000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a775000-7f4d6a974000 ---p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a974000-7f4d6a975000 r--p 00017000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a975000-7f4d6a976000 rw-p 00018000 08:14
1180001 /lib/libpthread-2.11.1.so
7f4d6a976000-7f4d6a97a000 rw-p 00000000 00:00 0
7f4d6a97a000-7f4d6a97c000 r-xp 00000000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6a97c000-7f4d6ab7c000 ---p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7c000-7f4d6ab7d000 r--p 00002000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7d000-7f4d6ab7e000 rw-p 00003000 08:14
1180008 /lib/libdl-2.11.1.so
7f4d6ab7e000-7f4d6ac00000 r-xp 00000000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ac00000-7f4d6adff000 ---p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6adff000-7f4d6ae00000 r--p 00081000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae00000-7f4d6ae01000 rw-p 00082000 08:14
1179958 /lib/libm-2.11.1.so
7f4d6ae01000-7f4d6ae21000 r-xp 00000000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6ae95000-7f4d6ae96000 rw-p 00000000 00:00 0
7f4d6ae96000-7f4d6aed5000 r--p 00000000 08:14
2237984 /usr/lib/locale/en_US.utf8/LC_CTYPE
7f4d6aed5000-7f4d6aff3000 r--p 00000000 08:14
2239899 /usr/lib/locale/en_US.utf8/LC_COLLATE
7f4d6aff3000-7f4d6aff7000 rw-p 00000000 00:00 0
7f4d6b00b000-7f4d6b00c000 r--p 00000000 08:14
2237989 /usr/lib/locale/en_US.utf8/LC_NUMERIC
7f4d6b00c000-7f4d6b00d000 r--p 00000000 08:14
2232077 /usr/lib/locale/en_US.utf8/LC_TIME
7f4d6b00d000-7f4d6b00e000 r--p 00000000 08:14
2234328 /usr/lib/locale/en_US.utf8/LC_MONETARY
7f4d6b00e000-7f4d6b00f000 r--p 00000000 08:14
2235935
/usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
7f4d6b00f000-7f4d6b010000 r--p 00000000 08:14
2237938 /usr/lib/locale/en_US.utf8/LC_PAPER
7f4d6b010000-7f4d6b011000 r--p 00000000 08:14
2237858 /usr/lib/locale/en_US.utf8/LC_NAME
7f4d6b011000-7f4d6b012000 r--p 00000000 08:14
2235936 /usr/lib/locale/en_US.utf8/LC_ADDRESS
7f4d6b012000-7f4d6b013000 r--p 00000000 08:14
2235937 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
7f4d6b013000-7f4d6b014000 r--p 00000000 08:14
2239858 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
7f4d6b014000-7f4d6b01b000 r--s 00000000 08:14
2233563 /usr/lib/gconv/gconv-modules.cache
7f4d6b01b000-7f4d6b01c000 r--p 00000000 08:14
2235940 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
7f4d6b01c000-7f4d6b020000 rw-p 00000000 00:00 0
7f4d6b020000-7f4d6b021000 r--p 0001f000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b021000-7f4d6b022000 rw-p 00020000 08:14
1179985 /lib/ld-2.11.1.so
7f4d6b022000-7f4d6b023000 rw-p 00000000 00:00 0
7fff5c542000-7fff5c557000 rw-p 00000000 00:00 0
[stack]
7fff5c5ff000-7fff5c600000 r-xp 00000000 00:00 0
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0
[vsyscall]
/mnt/store/LINUX/DOCUMENTS/DEVELOPMENT/Gambas/Προγραμματισμός σε
Gambas/Το βιβλίο της Gambas3/Projects/Pointers86
---------------------------
the last line is in Greek some part, it is the name of current
directory.
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Benoît Minisini
2011-01-09 20:37:25 UTC
Permalink
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
You must know C programming first, and understand how getwd works. By typing
'man getwd', you will learn that you must not use that function, it is
dangerous and so deprecated.

Then you will read that buf must be a pointer to a memory allocation of
PATH_MAX bytes. You must find the value of this constant inside the standard C
header files.

SizeOf(gb.String) is 4 (on 32 bits system). It is the number of bytes used by
Gambas to store a pointer to a Gambas string. This is the reason why you get a
crash.

Regards,
--
Benoît Minisini
Demosthenes Koptsis
2011-01-09 21:17:44 UTC
Permalink
yes i read the man and i know that is suggested not to use this
function.

Although i just use it for demonstration for pointers.

That i didn't do is to check the header files.

The other thing is that i am total newbie to man 3 and i need some time
to get used to it, so please be patient.

i see that the usage of external functions are more far than a medium
level of Basic programming but i think i can manage it in time.
Post by Benoît Minisini
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
You must know C programming first, and understand how getwd works. By typing
'man getwd', you will learn that you must not use that function, it is
dangerous and so deprecated.
Then you will read that buf must be a pointer to a memory allocation of
PATH_MAX bytes. You must find the value of this constant inside the standard C
header files.
SizeOf(gb.String) is 4 (on 32 bits system). It is the number of bytes used by
Gambas to store a pointer to a Gambas string. This is the reason why you get a
crash.
Regards,
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-09 23:16:41 UTC
Permalink
1) Ok, i found why this function is dangerous.
https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/760-BSI.html
"Since the user cannot specify the length of the buffer passed to
getwd(), use of this function is discouraged. The length of a pathname
described in {PATH_MAX} is file system-dependent and may vary from one
mount point to another, or might even be unlimited. It is possible to
overflow this buffer in such a way as to cause applications to fail or
possible system security violations."

But here i make a test no real life project.

2) So, i found in /usr/include/linux/limits.h
Line Number: 12
#define PATH_MAX 4096 /* # chars in a path name including nul */

is this the value?

i tried this value in my example with no success, i make somewhere else
the mistake.

---------------
' Gambas module file
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"

Public Sub Main()

Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String

'getwd
pBuf = Alloc(4096)
pFunc = Alloc(4096)

pFunc = getwd(pBuf)

sWorkingDirectory = Str@(pFunc)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory

End
---------------

signal #6

----------------
*** glibc detected *** Pointers5: double free or corruption (!prev):
0x0893f498 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x17b591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x17cde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x17fecd]
Pointers5[0x805ed2e]
Pointers5[0x8076a6a]
Pointers5[0x8050a99]
Pointers5[0x80513d8]
Pointers5[0x80689cd]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126bd6]
Pointers5[0x804b151]
======= Memory map: ========
00110000-00263000 r-xp 00000000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00263000-00264000 ---p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00264000-00266000 r--p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00266000-00267000 rw-p 00155000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00267000-0026a000 rw-p 00000000 00:00 0
00428000-00438000 r-xp 00000000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00438000-00439000 r--p 0000f000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00439000-0043b000 rw-p 00010000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
0043b000-0043c000 rw-p 00000000 00:00 0
0046e000-0046f000 r-xp 00000000 00:00 0 [vdso]
006c9000-006ce000 r-xp 00000000 08:04
688690 /usr/lib/libffi.so.5.0.10
006ce000-006cf000 ---p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006cf000-006d0000 r--p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006d0000-006d1000 rw-p 00006000 08:04
688690 /usr/lib/libffi.so.5.0.10
00739000-0075d000 r-xp 00000000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075d000-0075e000 r--p 00023000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075e000-0075f000 rw-p 00024000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
00aa5000-00ac2000 r-xp 00000000 08:04 1995027 /lib/libgcc_s.so.1
00ac2000-00ac3000 r--p 0001c000 08:04 1995027 /lib/libgcc_s.so.1
00ac3000-00ac4000 rw-p 0001d000 08:04 1995027 /lib/libgcc_s.so.1
00bca000-00bcc000 r-xp 00000000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcc000-00bcd000 r--p 00001000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcd000-00bce000 rw-p 00002000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00c25000-00c3a000 r-xp 00000000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3a000-00c3b000 r--p 00014000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3b000-00c3c000 rw-p 00015000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3c000-00c3e000 rw-p 00000000 00:00 0
00dc3000-00dc7000 r-xp 00000000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc7000-00dc8000 r--p 00003000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc8000-00dc9000 rw-p 00004000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00eeb000-00f06000 r-xp 00000000 08:04 1996732 /lib/ld-2.11.1.so
00f06000-00f07000 r--p 0001a000 08:04 1996732 /lib/ld-2.11.1.so
00f07000-00f08000 rw-p 0001b000 08:04 1996732 /lib/ld-2.11.1.so
08048000-0807e000 r-xp 00000000 08:04 1785292 /usr/local/bin/gbx3
0807e000-0807f000 r--p 00035000 08:04 1785292 /usr/local/bin/gbx3
0807f000-08084000 rw-p 00036000 08:04 1785292 /usr/local/bin/gbx3
08084000-08086000 rw-p 00000000 00:00 0
08932000-08953000 rw-p 00000000 00:00 0 [heap]
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b768a000-b76c9000 r--p 00000000 08:04
737484 /usr/lib/locale/en_US.utf8/LC_CTYPE
b76c9000-b77e7000 r--p 00000000 08:04
737483 /usr/lib/locale/en_US.utf8/LC_COLLATE
b77e7000-b77e9000 rw-p 00000000 00:00 0
b77f1000-b77f2000 r--p 00000000 08:04
737500 /usr/lib/locale/en_US.utf8/LC_NUMERIC
b77f2000-b77f3000 r--p 00000000 08:04
735917 /usr/lib/locale/en_US.utf8/LC_TIME
b77f3000-b77f4000 r--p 00000000 08:04
735918 /usr/lib/locale/en_US.utf8/LC_MONETARY
b77f4000-b77f5000 r--p 00000000 08:04
752258 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
b77f5000-b77f6000 r--p 00000000 08:04
737523 /usr/lib/locale/en_US.utf8/LC_PAPER
b77f6000-b77f7000 r--p 00000000 08:04
737455 /usr/lib/locale/en_US.utf8/LC_NAME
b77f7000-b77f8000 r--p 00000000 08:04
735919 /usr/lib/locale/en_US.utf8/LC_ADDRESS
b77f8000-b77f9000 r--p 00000000 08:04
735920 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
b77f9000-b77fa000 r--p 00000000 08:04
737398 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
b77fa000-b7801000 r--s 00000000 08:04
1619066 /usr/lib/gconv/gconv-modules.cache
b7801000-b7802000 r--p 00000000 08:04
735921 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
b7802000-b7806000 rw-p 00000000 00:00 0
bfa53000-bfa68000 rw-p 00000000 00:00 0 [stack]

----------------

i saw that the first line says
*** glibc detected *** Pointers5: double free or corruption (!prev)

and i rem the one Free().

now i get
--------------
gbx3: warning: 1 allocation(s) non freed.

��m^@^@^@^@^@^@^@^@^@ocuments/Development/Gambas3/Pointers5
--------------

but no crash.

i played with the values of Alloc and i found that in my case a 29 bytes
are ok but with one Free() or else i get signal #11.

is there a bug with
Free(pBuf)
Free(pFunc)
?

3) On the other hand, i have a general question.
How to deal with char *pointers when their length is not known?

4) For example i try to use
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

again with no success.

in manuall says:
get_current_dir_name() will malloc(3) an array big enough to hold the
absolute pathname of the current working directory.

in my example:

---------------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = Alloc(4096)

pFunc = get_current_dir_name()

sWorkingDirectory = Str@(pFunc)
' Free(pFunc)
Print sWorkingDirectory


End
---------------------

if i use
Free(pFunc)

i get signal #11

gambas3-svn3418, ubuntu 10.04
Post by Benoît Minisini
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
You must know C programming first, and understand how getwd works. By typing
'man getwd', you will learn that you must not use that function, it is
dangerous and so deprecated.
Then you will read that buf must be a pointer to a memory allocation of
PATH_MAX bytes. You must find the value of this constant inside the standard C
header files.
SizeOf(gb.String) is 4 (on 32 bits system). It is the number of bytes used by
Gambas to store a pointer to a Gambas string. This is the reason why you get a
crash.
Regards,
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-09 23:54:46 UTC
Permalink
so i manage to make it work...

i saw an example that said to use PATH_MAX+1
https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/760-BSI.html

so i did

Example 1) ----------------------
' Gambas module file
'char *getcwd(char *buf, size_t size);
Extern getcwd(buf As Pointer, size As Integer) As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim pBuf As Pointer
Dim sWorkingDirectory As String

'getcwd
pFunc = Alloc(4097)
pBuf = Alloc(4097)

pFunc = getcwd(pBuf, 4097)

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory
Free(pFunc)
' Free(pBuf)
Print sWorkingDirectory

End
----------------------

a) note in 1st example that
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory

first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7

and the second after Free(pFunc) gives this
�s&^@^@^@^@^@^@^@^@^@ocuments/Development/Gambas3/Pointers7

b) if i use Free(pBuf)
i get signal #6
and
*** glibc detected *** Pointers7: double free or corruption (!prev):
0x087c94c0 ***

Example 2 --------------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = Alloc(4097)

pFunc = get_current_dir_name()

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory
Free(pFunc)

End
-----------------------
here i get correct results
but if i use Free(pFunc) i get signal #11



Example 3 ---------------------------
' Gambas module file
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"

Public Sub Main()

Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String

'getwd
pBuf = Alloc(4097)
pFunc = Alloc(4097)

pFunc = getwd(pBuf)

sWorkingDirectory = Str@(pFunc)
' Free(pBuf)
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory

End
--------------------------

here the same with example1.
Free(pBuf)
Free(pFunc)
raises both signal #6
and
*** glibc detected *** Pointers5: double free or corruption (!prev):
0x09b0e4a8 ***

also
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory

first Print is correct
second after Free is not correct

/home/user/Documents/Development/Gambas3/Pointers5
Post by Demosthenes Koptsis
1) Ok, i found why this function is dangerous.
https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/760-BSI.html
"Since the user cannot specify the length of the buffer passed to
getwd(), use of this function is discouraged. The length of a pathname
described in {PATH_MAX} is file system-dependent and may vary from one
mount point to another, or might even be unlimited. It is possible to
overflow this buffer in such a way as to cause applications to fail or
possible system security violations."
But here i make a test no real life project.
2) So, i found in /usr/include/linux/limits.h
Line Number: 12
#define PATH_MAX 4096 /* # chars in a path name including nul */
is this the value?
i tried this value in my example with no success, i make somewhere else
the mistake.
---------------
' Gambas module file
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(4096)
pFunc = Alloc(4096)
pFunc = getwd(pBuf)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory
End
---------------
signal #6
----------------
0x0893f498 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x17b591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x17cde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x17fecd]
Pointers5[0x805ed2e]
Pointers5[0x8076a6a]
Pointers5[0x8050a99]
Pointers5[0x80513d8]
Pointers5[0x80689cd]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126bd6]
Pointers5[0x804b151]
======= Memory map: ========
00110000-00263000 r-xp 00000000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00263000-00264000 ---p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00264000-00266000 r--p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00266000-00267000 rw-p 00155000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00267000-0026a000 rw-p 00000000 00:00 0
00428000-00438000 r-xp 00000000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00438000-00439000 r--p 0000f000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00439000-0043b000 rw-p 00010000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
0043b000-0043c000 rw-p 00000000 00:00 0
0046e000-0046f000 r-xp 00000000 00:00 0 [vdso]
006c9000-006ce000 r-xp 00000000 08:04
688690 /usr/lib/libffi.so.5.0.10
006ce000-006cf000 ---p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006cf000-006d0000 r--p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006d0000-006d1000 rw-p 00006000 08:04
688690 /usr/lib/libffi.so.5.0.10
00739000-0075d000 r-xp 00000000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075d000-0075e000 r--p 00023000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075e000-0075f000 rw-p 00024000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
00aa5000-00ac2000 r-xp 00000000 08:04 1995027 /lib/libgcc_s.so.1
00ac2000-00ac3000 r--p 0001c000 08:04 1995027 /lib/libgcc_s.so.1
00ac3000-00ac4000 rw-p 0001d000 08:04 1995027 /lib/libgcc_s.so.1
00bca000-00bcc000 r-xp 00000000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcc000-00bcd000 r--p 00001000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcd000-00bce000 rw-p 00002000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00c25000-00c3a000 r-xp 00000000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3a000-00c3b000 r--p 00014000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3b000-00c3c000 rw-p 00015000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3c000-00c3e000 rw-p 00000000 00:00 0
00dc3000-00dc7000 r-xp 00000000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc7000-00dc8000 r--p 00003000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc8000-00dc9000 rw-p 00004000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00eeb000-00f06000 r-xp 00000000 08:04 1996732 /lib/ld-2.11.1.so
00f06000-00f07000 r--p 0001a000 08:04 1996732 /lib/ld-2.11.1.so
00f07000-00f08000 rw-p 0001b000 08:04 1996732 /lib/ld-2.11.1.so
08048000-0807e000 r-xp 00000000 08:04 1785292 /usr/local/bin/gbx3
0807e000-0807f000 r--p 00035000 08:04 1785292 /usr/local/bin/gbx3
0807f000-08084000 rw-p 00036000 08:04 1785292 /usr/local/bin/gbx3
08084000-08086000 rw-p 00000000 00:00 0
08932000-08953000 rw-p 00000000 00:00 0 [heap]
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b768a000-b76c9000 r--p 00000000 08:04
737484 /usr/lib/locale/en_US.utf8/LC_CTYPE
b76c9000-b77e7000 r--p 00000000 08:04
737483 /usr/lib/locale/en_US.utf8/LC_COLLATE
b77e7000-b77e9000 rw-p 00000000 00:00 0
b77f1000-b77f2000 r--p 00000000 08:04
737500 /usr/lib/locale/en_US.utf8/LC_NUMERIC
b77f2000-b77f3000 r--p 00000000 08:04
735917 /usr/lib/locale/en_US.utf8/LC_TIME
b77f3000-b77f4000 r--p 00000000 08:04
735918 /usr/lib/locale/en_US.utf8/LC_MONETARY
b77f4000-b77f5000 r--p 00000000 08:04
752258 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
b77f5000-b77f6000 r--p 00000000 08:04
737523 /usr/lib/locale/en_US.utf8/LC_PAPER
b77f6000-b77f7000 r--p 00000000 08:04
737455 /usr/lib/locale/en_US.utf8/LC_NAME
b77f7000-b77f8000 r--p 00000000 08:04
735919 /usr/lib/locale/en_US.utf8/LC_ADDRESS
b77f8000-b77f9000 r--p 00000000 08:04
735920 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
b77f9000-b77fa000 r--p 00000000 08:04
737398 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
b77fa000-b7801000 r--s 00000000 08:04
1619066 /usr/lib/gconv/gconv-modules.cache
b7801000-b7802000 r--p 00000000 08:04
735921 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
b7802000-b7806000 rw-p 00000000 00:00 0
bfa53000-bfa68000 rw-p 00000000 00:00 0 [stack]
----------------
i saw that the first line says
*** glibc detected *** Pointers5: double free or corruption (!prev)
and i rem the one Free().
now i get
--------------
gbx3: warning: 1 allocation(s) non freed.
--------------
but no crash.
i played with the values of Alloc and i found that in my case a 29 bytes
are ok but with one Free() or else i get signal #11.
is there a bug with
Free(pBuf)
Free(pFunc)
?
3) On the other hand, i have a general question.
How to deal with char *pointers when their length is not known?
4) For example i try to use
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
again with no success.
get_current_dir_name() will malloc(3) an array big enough to hold the
absolute pathname of the current working directory.
---------------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4096)
pFunc = get_current_dir_name()
' Free(pFunc)
Print sWorkingDirectory
End
---------------------
if i use
Free(pFunc)
i get signal #11
gambas3-svn3418, ubuntu 10.04
Post by Benoît Minisini
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
You must know C programming first, and understand how getwd works. By typing
'man getwd', you will learn that you must not use that function, it is
dangerous and so deprecated.
Then you will read that buf must be a pointer to a memory allocation of
PATH_MAX bytes. You must find the value of this constant inside the standard C
header files.
SizeOf(gb.String) is 4 (on 32 bits system). It is the number of bytes used by
Gambas to store a pointer to a Gambas string. This is the reason why you get a
crash.
Regards,
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-09 23:59:06 UTC
Permalink
i attach the Projects in case you want to see them.
Post by Demosthenes Koptsis
so i manage to make it work...
i saw an example that said to use PATH_MAX+1
https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/760-BSI.html
so i did
Example 1) ----------------------
' Gambas module file
'char *getcwd(char *buf, size_t size);
Extern getcwd(buf As Pointer, size As Integer) As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim pBuf As Pointer
Dim sWorkingDirectory As String
'getcwd
pFunc = Alloc(4097)
pBuf = Alloc(4097)
pFunc = getcwd(pBuf, 4097)
Print sWorkingDirectory
Free(pFunc)
' Free(pBuf)
Print sWorkingDirectory
End
----------------------
a) note in 1st example that
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory
first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7
and the second after Free(pFunc) gives this
b) if i use Free(pBuf)
i get signal #6
and
0x087c94c0 ***
Example 2 --------------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4097)
pFunc = get_current_dir_name()
Print sWorkingDirectory
Free(pFunc)
End
-----------------------
here i get correct results
but if i use Free(pFunc) i get signal #11
Example 3 ---------------------------
' Gambas module file
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(4097)
pFunc = Alloc(4097)
pFunc = getwd(pBuf)
' Free(pBuf)
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory
End
--------------------------
here the same with example1.
Free(pBuf)
Free(pFunc)
raises both signal #6
and
0x09b0e4a8 ***
also
Print sWorkingDirectory
Free(pFunc)
Print sWorkingDirectory
first Print is correct
second after Free is not correct
/home/user/Documents/Development/Gambas3/Pointers5
Post by Demosthenes Koptsis
1) Ok, i found why this function is dangerous.
https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/760-BSI.html
"Since the user cannot specify the length of the buffer passed to
getwd(), use of this function is discouraged. The length of a pathname
described in {PATH_MAX} is file system-dependent and may vary from one
mount point to another, or might even be unlimited. It is possible to
overflow this buffer in such a way as to cause applications to fail or
possible system security violations."
But here i make a test no real life project.
2) So, i found in /usr/include/linux/limits.h
Line Number: 12
#define PATH_MAX 4096 /* # chars in a path name including nul */
is this the value?
i tried this value in my example with no success, i make somewhere else
the mistake.
---------------
' Gambas module file
' Gambas module file
'char *getwd(char *buf);
Extern getwd(buf As Pointer) As Pointer In "libc:6"
Public Sub Main()
Dim pBuf As Pointer
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'getwd
pBuf = Alloc(4096)
pFunc = Alloc(4096)
pFunc = getwd(pBuf)
Free(pFunc)
Free(pBuf)
Print sWorkingDirectory
End
---------------
signal #6
----------------
0x0893f498 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x17b591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0x17cde8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x17fecd]
Pointers5[0x805ed2e]
Pointers5[0x8076a6a]
Pointers5[0x8050a99]
Pointers5[0x80513d8]
Pointers5[0x80689cd]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x126bd6]
Pointers5[0x804b151]
======= Memory map: ========
00110000-00263000 r-xp 00000000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00263000-00264000 ---p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00264000-00266000 r--p 00153000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00266000-00267000 rw-p 00155000 08:04
2019753 /lib/tls/i686/cmov/libc-2.11.1.so
00267000-0026a000 rw-p 00000000 00:00 0
00428000-00438000 r-xp 00000000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00438000-00439000 r--p 0000f000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
00439000-0043b000 rw-p 00010000 08:04
1906544 /usr/local/lib/gambas3/gb.eval.so.0.0.0
0043b000-0043c000 rw-p 00000000 00:00 0
0046e000-0046f000 r-xp 00000000 00:00 0 [vdso]
006c9000-006ce000 r-xp 00000000 08:04
688690 /usr/lib/libffi.so.5.0.10
006ce000-006cf000 ---p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006cf000-006d0000 r--p 00005000 08:04
688690 /usr/lib/libffi.so.5.0.10
006d0000-006d1000 rw-p 00006000 08:04
688690 /usr/lib/libffi.so.5.0.10
00739000-0075d000 r-xp 00000000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075d000-0075e000 r--p 00023000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
0075e000-0075f000 rw-p 00024000 08:04
2019761 /lib/tls/i686/cmov/libm-2.11.1.so
00aa5000-00ac2000 r-xp 00000000 08:04 1995027 /lib/libgcc_s.so.1
00ac2000-00ac3000 r--p 0001c000 08:04 1995027 /lib/libgcc_s.so.1
00ac3000-00ac4000 rw-p 0001d000 08:04 1995027 /lib/libgcc_s.so.1
00bca000-00bcc000 r-xp 00000000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcc000-00bcd000 r--p 00001000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00bcd000-00bce000 rw-p 00002000 08:04
2019759 /lib/tls/i686/cmov/libdl-2.11.1.so
00c25000-00c3a000 r-xp 00000000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3a000-00c3b000 r--p 00014000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3b000-00c3c000 rw-p 00015000 08:04
2019772 /lib/tls/i686/cmov/libpthread-2.11.1.so
00c3c000-00c3e000 rw-p 00000000 00:00 0
00dc3000-00dc7000 r-xp 00000000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc7000-00dc8000 r--p 00003000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00dc8000-00dc9000 rw-p 00004000 08:04
1906676 /usr/local/lib/gambas3/gb.debug.so.0.0.0
00eeb000-00f06000 r-xp 00000000 08:04 1996732 /lib/ld-2.11.1.so
00f06000-00f07000 r--p 0001a000 08:04 1996732 /lib/ld-2.11.1.so
00f07000-00f08000 rw-p 0001b000 08:04 1996732 /lib/ld-2.11.1.so
08048000-0807e000 r-xp 00000000 08:04 1785292 /usr/local/bin/gbx3
0807e000-0807f000 r--p 00035000 08:04 1785292 /usr/local/bin/gbx3
0807f000-08084000 rw-p 00036000 08:04 1785292 /usr/local/bin/gbx3
08084000-08086000 rw-p 00000000 00:00 0
08932000-08953000 rw-p 00000000 00:00 0 [heap]
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b768a000-b76c9000 r--p 00000000 08:04
737484 /usr/lib/locale/en_US.utf8/LC_CTYPE
b76c9000-b77e7000 r--p 00000000 08:04
737483 /usr/lib/locale/en_US.utf8/LC_COLLATE
b77e7000-b77e9000 rw-p 00000000 00:00 0
b77f1000-b77f2000 r--p 00000000 08:04
737500 /usr/lib/locale/en_US.utf8/LC_NUMERIC
b77f2000-b77f3000 r--p 00000000 08:04
735917 /usr/lib/locale/en_US.utf8/LC_TIME
b77f3000-b77f4000 r--p 00000000 08:04
735918 /usr/lib/locale/en_US.utf8/LC_MONETARY
b77f4000-b77f5000 r--p 00000000 08:04
752258 /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
b77f5000-b77f6000 r--p 00000000 08:04
737523 /usr/lib/locale/en_US.utf8/LC_PAPER
b77f6000-b77f7000 r--p 00000000 08:04
737455 /usr/lib/locale/en_US.utf8/LC_NAME
b77f7000-b77f8000 r--p 00000000 08:04
735919 /usr/lib/locale/en_US.utf8/LC_ADDRESS
b77f8000-b77f9000 r--p 00000000 08:04
735920 /usr/lib/locale/en_US.utf8/LC_TELEPHONE
b77f9000-b77fa000 r--p 00000000 08:04
737398 /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
b77fa000-b7801000 r--s 00000000 08:04
1619066 /usr/lib/gconv/gconv-modules.cache
b7801000-b7802000 r--p 00000000 08:04
735921 /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
b7802000-b7806000 rw-p 00000000 00:00 0
bfa53000-bfa68000 rw-p 00000000 00:00 0 [stack]
----------------
i saw that the first line says
*** glibc detected *** Pointers5: double free or corruption (!prev)
and i rem the one Free().
now i get
--------------
gbx3: warning: 1 allocation(s) non freed.
--------------
but no crash.
i played with the values of Alloc and i found that in my case a 29 bytes
are ok but with one Free() or else i get signal #11.
is there a bug with
Free(pBuf)
Free(pFunc)
?
3) On the other hand, i have a general question.
How to deal with char *pointers when their length is not known?
4) For example i try to use
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
again with no success.
get_current_dir_name() will malloc(3) an array big enough to hold the
absolute pathname of the current working directory.
---------------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4096)
pFunc = get_current_dir_name()
' Free(pFunc)
Print sWorkingDirectory
End
---------------------
if i use
Free(pFunc)
i get signal #11
gambas3-svn3418, ubuntu 10.04
Post by Benoît Minisini
Post by Demosthenes Koptsis
so what can i do for
char *getwd(char *buf);
?
You must know C programming first, and understand how getwd works. By typing
'man getwd', you will learn that you must not use that function, it is
dangerous and so deprecated.
Then you will read that buf must be a pointer to a memory allocation of
PATH_MAX bytes. You must find the value of this constant inside the standard C
header files.
SizeOf(gb.String) is 4 (on 32 bits system). It is the number of bytes used by
Gambas to store a pointer to a Gambas string. This is the reason why you get a
crash.
Regards,
--
Regards,
Demosthenes
Jussi Lahtinen
2011-01-10 19:54:12 UTC
Permalink
Post by Demosthenes Koptsis
first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7
and the second after Free(pFunc) gives this
This has something to do how strings are handled in Gambas, I don't think
this is bug.

Some lines of simplified example code and explanation:

Dim pPointer As Pointer
Dim hMemory As Stream
Dim s As String

pPointer = Alloc(4) ' Alloc for four characters, last one means 'end of
string'.
hMemory = Memory pPointer For Write
Write #hMemory, Asc("a") As Byte
Write #hMemory, Asc("b") As Byte
Write #hMemory, Asc("c") As Byte
Write #hMemory, Asc("\0") As Byte

s = Str@(pPointer) ' This associates address to variable. Ie, in address
pPointer is content of this string.

Print s ' This will print out; "abc"

Seek #hMemory, 0
Write #hMemory, Asc("x") As Byte ' These two lines will access to address
pPointer and change first character in there.

Print s ' Therefore string changes to "xbc". Variable 's' is just
representation of content in some (in this case pPointer) address.
Post by Demosthenes Koptsis
b) if i use Free(pBuf)
i get signal #6
I don't think this should happen... Benoit?
Post by Demosthenes Koptsis
here i get correct results
but if i use Free(pFunc) i get signal #11
This shouldn't happen, it is bug.
However I couldn't reproduce it with Gambas 3 rev 3451,
but I got signal 6 from it.


Jussi
Jussi Lahtinen
2011-01-10 19:58:18 UTC
Permalink
Oh, I forgot to mention, that when you do "Free(pPointer)", Gambas doesn't
occupy that memory address for you anymore.
And therefore some other process can write something to it, and result is
not necessarily human readable.

Jussi
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7
and the second after Free(pFunc) gives this
This has something to do how strings are handled in Gambas, I don't think
this is bug.
Dim pPointer As Pointer
Dim hMemory As Stream
Dim s As String
pPointer = Alloc(4) ' Alloc for four characters, last one means 'end of
string'.
hMemory = Memory pPointer For Write
Write #hMemory, Asc("a") As Byte
Write #hMemory, Asc("b") As Byte
Write #hMemory, Asc("c") As Byte
Write #hMemory, Asc("\0") As Byte
pPointer is content of this string.
Print s ' This will print out; "abc"
Seek #hMemory, 0
Write #hMemory, Asc("x") As Byte ' These two lines will access to address
pPointer and change first character in there.
Print s ' Therefore string changes to "xbc". Variable 's' is just
representation of content in some (in this case pPointer) address.
Post by Demosthenes Koptsis
b) if i use Free(pBuf)
i get signal #6
I don't think this should happen... Benoit?
Post by Demosthenes Koptsis
here i get correct results
but if i use Free(pFunc) i get signal #11
This shouldn't happen, it is bug.
However I couldn't reproduce it with Gambas 3 rev 3451,
but I got signal 6 from it.
Jussi
Demosthenes Koptsis
2011-01-10 21:05:55 UTC
Permalink
Post by Jussi Lahtinen
Oh, I forgot to mention, that when you do "Free(pPointer)", Gambas doesn't
occupy that memory address for you anymore.
And therefore some other process can write something to it, and result is
not necessarily human readable.
Jussi
Ok, understood.
But i have put the contents of memory address to a variable.

sWorkingDirectory = Str@(pFunc)

if i free pFunc why variable sWorkingDirectory is affected?

if this is happend i must know it, so to use variables before free any
pointer.
Post by Jussi Lahtinen
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7
and the second after Free(pFunc) gives this
This has something to do how strings are handled in Gambas, I don't think
this is bug.
Dim pPointer As Pointer
Dim hMemory As Stream
Dim s As String
pPointer = Alloc(4) ' Alloc for four characters, last one means 'end of
string'.
hMemory = Memory pPointer For Write
Write #hMemory, Asc("a") As Byte
Write #hMemory, Asc("b") As Byte
Write #hMemory, Asc("c") As Byte
Write #hMemory, Asc("\0") As Byte
pPointer is content of this string.
Print s ' This will print out; "abc"
Seek #hMemory, 0
Write #hMemory, Asc("x") As Byte ' These two lines will access to address
pPointer and change first character in there.
Print s ' Therefore string changes to "xbc". Variable 's' is just
representation of content in some (in this case pPointer) address.
Yes this is happens!

This is behaved like s variable acts as a pointer to pPointer.
As it beahaves seems s is linked with pPointer.
I think that s should not point to pPointer but copy its value to s
variable.

Result = String@ ( Pointer AS Pointer )

Result i think is a value, so this value is assigned to s var.
After assignation s has a value and should not change it if i Free a
pointer.

If i write to pPointer or Free it, the s var is placed in other adress
normally in RAM.

As i understand the case,

pPointer = Alloc(4) allocate a memory address1
Dim s as string allocate a memory address2

if i use
s=Str@(pPointer)

this means
address1=adress2 ?

s holds a value, Str@(pPointer) returns a value not its address and
should not be linked s by addresses as pointers

Am i wrong?


For now i will upgrade Gambas to latest revision and try again.
Post by Jussi Lahtinen
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
b) if i use Free(pBuf)
i get signal #6
I don't think this should happen... Benoit?
Post by Demosthenes Koptsis
here i get correct results
but if i use Free(pFunc) i get signal #11
This shouldn't happen, it is bug.
However I couldn't reproduce it with Gambas 3 rev 3451,
but I got signal 6 from it.
Jussi
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-10 22:26:46 UTC
Permalink
i made some examples in c++ and i get the same behavior when i free a
pointer the variable that holds its value is unusable.

ok, sorry i must study about pointers i think i mixed up some things in
the last post... sorry
Post by Demosthenes Koptsis
Post by Jussi Lahtinen
Oh, I forgot to mention, that when you do "Free(pPointer)", Gambas doesn't
occupy that memory address for you anymore.
And therefore some other process can write something to it, and result is
not necessarily human readable.
Jussi
Ok, understood.
But i have put the contents of memory address to a variable.
if i free pFunc why variable sWorkingDirectory is affected?
if this is happend i must know it, so to use variables before free any
pointer.
Post by Jussi Lahtinen
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
first print gives the correct output
/home/user/Documents/Development/Gambas3/Pointers7
and the second after Free(pFunc) gives this
This has something to do how strings are handled in Gambas, I don't think
this is bug.
Dim pPointer As Pointer
Dim hMemory As Stream
Dim s As String
pPointer = Alloc(4) ' Alloc for four characters, last one means 'end of
string'.
hMemory = Memory pPointer For Write
Write #hMemory, Asc("a") As Byte
Write #hMemory, Asc("b") As Byte
Write #hMemory, Asc("c") As Byte
Write #hMemory, Asc("\0") As Byte
pPointer is content of this string.
Print s ' This will print out; "abc"
Seek #hMemory, 0
Write #hMemory, Asc("x") As Byte ' These two lines will access to address
pPointer and change first character in there.
Print s ' Therefore string changes to "xbc". Variable 's' is just
representation of content in some (in this case pPointer) address.
Yes this is happens!
This is behaved like s variable acts as a pointer to pPointer.
As it beahaves seems s is linked with pPointer.
I think that s should not point to pPointer but copy its value to s
variable.
Result i think is a value, so this value is assigned to s var.
After assignation s has a value and should not change it if i Free a
pointer.
If i write to pPointer or Free it, the s var is placed in other adress
normally in RAM.
As i understand the case,
pPointer = Alloc(4) allocate a memory address1
Dim s as string allocate a memory address2
if i use
this means
address1=adress2 ?
should not be linked s by addresses as pointers
Am i wrong?
For now i will upgrade Gambas to latest revision and try again.
Post by Jussi Lahtinen
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
b) if i use Free(pBuf)
i get signal #6
I don't think this should happen... Benoit?
Post by Demosthenes Koptsis
here i get correct results
but if i use Free(pFunc) i get signal #11
This shouldn't happen, it is bug.
However I couldn't reproduce it with Gambas 3 rev 3451,
but I got signal 6 from it.
Jussi
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Jussi Lahtinen
2011-01-11 18:42:43 UTC
Permalink
Post by Demosthenes Koptsis
if i use
this means
address1=adress2 ?
Kind of... even this doesn't change the situation:

sTmp = Str@(pPointer)
s = sTmp

I'm not sure this is good thing, may cause weird bugs and confusion!
I think this is because strings are objects in Gambas...
maybe Str@() should do internal copy of data, instead of association object1
= object2?

What do you think Benoit?

Jussi
Benoît Minisini
2011-01-11 18:47:42 UTC
Permalink
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
if i use
this means
address1=adress2 ?
s = sTmp
I'm not sure this is good thing, may cause weird bugs and confusion!
I think this is because strings are objects in Gambas...
object1 = object2?
What do you think Benoit?
Jussi
That both of you should read the wiki page about String@ (or Str@ or StrPtr).
:-)

It returns a constant string. A constant string is just a pointer (and a
length), no data is copied or allocated. String@ assumes that the pointer
points at constant data that will not change.

Regards,
--
Benoît Minisini
Jussi Lahtinen
2011-01-11 18:58:53 UTC
Permalink
Hah! OK, I didn't guess there would be so detailed information!
So, I think this is OK then!

Jussi
Post by Benoît Minisini
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
if i use
this means
address1=adress2 ?
s = sTmp
I'm not sure this is good thing, may cause weird bugs and confusion!
I think this is because strings are objects in Gambas...
object1 = object2?
What do you think Benoit?
Jussi
:-)
It returns a constant string. A constant string is just a pointer (and a
points at constant data that will not change.
Regards,
--
Benoît Minisini
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
Demosthenes Koptsis
2011-01-17 08:33:43 UTC
Permalink
Here is a last case about pointers i found.

CASE 1
-------
When a C++ function does not malloc any memory it may return a pointer.
In this case we can do

DIM pPointer as Pointer
pPointer = Alloc(4096)' alloc 4096 bytes

pPointer = getwd(pBuf)

'use pPointer with Str@() or other xxx@()

Free(pPointer)
pPointer=0

OK!

CASE 2
-------
When a c++ function itself allocate memory with malloc() what can we do?

Example.
--------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = Alloc(4096)
Print pFunc '0xc12938

pFunc = get_current_dir_name()
Print pFunc '0xc147a0

'now we cannot Free(pFunc) it is not 0xc12938 anymore
'get_current_dir_name() malloc itself memory and return a new pointer

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory

' Free(pFunc)
'if i unrem Free i get a crash, because i try to free the 0xc147a0
pFunc = 0

End

What can we do in such cases when a function itself allocates memory?
Should we free it? Can we?

May be we can use pointers without Alloc()?

Example, no Alloc(), no Free, just DIM pPointer
---------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name

pFunc = get_current_dir_name()
Print pFunc '0xc147a0

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory

pFunc = 0

End
Benoît Minisini
2011-01-17 10:08:51 UTC
Permalink
Post by Demosthenes Koptsis
Here is a last case about pointers i found.
CASE 1
-------
When a C++ function does not malloc any memory it may return a pointer.
In this case we can do
DIM pPointer as Pointer
pPointer = Alloc(4096)' alloc 4096 bytes
pPointer = getwd(pBuf)
Free(pPointer)
pPointer=0
OK!
CASE 2
-------
When a c++ function itself allocate memory with malloc() what can we do?
Example.
--------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4096)
Print pFunc '0xc12938
pFunc = get_current_dir_name()
Print pFunc '0xc147a0
'now we cannot Free(pFunc) it is not 0xc12938 anymore
'get_current_dir_name() malloc itself memory and return a new pointer
Print sWorkingDirectory
' Free(pFunc)
'if i unrem Free i get a crash, because i try to free the 0xc147a0
pFunc = 0
End
What can we do in such cases when a function itself allocates memory?
Should we free it? Can we?
May be we can use pointers without Alloc()?
Example, no Alloc(), no Free, just DIM pPointer
---------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc '0xc147a0
Print sWorkingDirectory
pFunc = 0
End
What must be done with with a pointer returned by a C / C++ function depends
on the function semantic.

You must read and *understand* the function documentation to know.

Some functions return a pointer that you must free yourself with Free().

Some functions return a pointer that you must not free.

Some functions return a pointer that you must free by calling a dedicated
function, and not Free().

... Everything is possible.

Regards,
--
Benoît Minisini
Demosthenes Koptsis
2011-01-17 10:27:12 UTC
Permalink
Post by Benoît Minisini
Post by Demosthenes Koptsis
Here is a last case about pointers i found.
CASE 1
-------
When a C++ function does not malloc any memory it may return a pointer.
In this case we can do
DIM pPointer as Pointer
pPointer = Alloc(4096)' alloc 4096 bytes
pPointer = getwd(pBuf)
Free(pPointer)
pPointer=0
OK!
CASE 2
-------
When a c++ function itself allocate memory with malloc() what can we do?
Example.
--------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4096)
Print pFunc '0xc12938
pFunc = get_current_dir_name()
Print pFunc '0xc147a0
'now we cannot Free(pFunc) it is not 0xc12938 anymore
'get_current_dir_name() malloc itself memory and return a new pointer
Print sWorkingDirectory
' Free(pFunc)
'if i unrem Free i get a crash, because i try to free the 0xc147a0
pFunc = 0
End
What can we do in such cases when a function itself allocates memory?
Should we free it? Can we?
May be we can use pointers without Alloc()?
Example, no Alloc(), no Free, just DIM pPointer
---------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc '0xc147a0
Print sWorkingDirectory
pFunc = 0
End
What must be done with with a pointer returned by a C / C++ function depends
on the function semantic.
You must read and *understand* the function documentation to know.
ok...
Post by Benoît Minisini
Some functions return a pointer that you must free yourself with Free().
Some functions return a pointer that you must not free.
Some functions return a pointer that you must free by calling a dedicated
function, and not Free().
so in case of get_current_dir_name(),

i should use the free function of c++ ?

that is i understand form manual.
"get_current_dir_name() will malloc(3) an array big enough to hold the
absolute pathname of the current working directory. If the environment
variable PWD is set, and its value is correct, then that value will be
returned. The caller should free(3) the returned buffer.'
Post by Benoît Minisini
... Everything is possible.
Regards,
--
Regards,
Demosthenes Koptsis.
Jussi Lahtinen
2011-01-09 14:00:39 UTC
Permalink
Post by Demosthenes Koptsis
Here is another question-case.
i Alloc 12 bytes.
i set in memory an integer 4 bytes
then i set in memory a float 8 bytes.
i can read the memory stream with READ, ok!
I haven't try this in gambas, but in C/C++ variables are in memory in
straight row without any metadata.
I assume this is also case in gambas, so;
iFirstInteger = Integer@(pPointer)
iNextInteger = Integer@(pPointer + 4)


Jussi
Demosthenes Koptsis
2011-01-09 19:54:25 UTC
Permalink
This is what is said here
http://gambasdoc.org/help/howto/extern?v3
"More On Pointers"
READ #mypointer, var1_4byte
mypointer += 4
READ #mypointer, var2_4byte


i tried
Print "\nRead the pointer"
Print "Integer read:"; Integer@(pPointer)
pPointer += 4
Print "Float read:"; Float@(pPointer)

But this raises an error, "Type mismatch, wanted number get pointer instead"
Is this a bug?

As i see there is no intrinsic function of Gambas3 for moving in memory
forward or backward from a starting memory address.
Is it useful to be one? what do you say?

As i see there is only a way to move inside a memory stream with SEEK.
Post by Jussi Lahtinen
Post by Demosthenes Koptsis
Here is another question-case.
i Alloc 12 bytes.
i set in memory an integer 4 bytes
then i set in memory a float 8 bytes.
i can read the memory stream with READ, ok!
I haven't try this in gambas, but in C/C++ variables are in memory in
straight row without any metadata.
I assume this is also case in gambas, so;
Jussi
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web. Learn how to
best implement a security strategy that keeps consumers' information secure
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes
Jussi Lahtinen
2011-01-09 21:20:18 UTC
Permalink
pPointer += 4
Post by Demosthenes Koptsis
But this raises an error, "Type mismatch, wanted number get pointer instead"
Is this a bug?
Probably, I'll look that more closely later...
Post by Demosthenes Koptsis
As i see there is no intrinsic function of Gambas3 for moving in memory
forward or backward from a starting memory address.
Is it useful to be one? what do you say?
As i see there is only a way to move inside a memory stream with SEEK.
Quickly thinking I don't see any need for anything else than "SEEK",
"pPointer + something"
and of course automatically with read/write.

Jussi
Benoît Minisini
2011-01-10 22:57:14 UTC
Permalink
Post by Demosthenes Koptsis
This is what is said here
http://gambasdoc.org/help/howto/extern?v3
"More On Pointers"
READ #mypointer, var1_4byte
mypointer += 4
READ #mypointer, var2_4byte
i tried
Print "\nRead the pointer"
pPointer += 4
But this raises an error, "Type mismatch, wanted number get pointer
instead" Is this a bug?
I have implemented pointer arithmetic in revision #3452, so you should not
have that error anymore.

Regards,
--
Benoît Minisini
Demosthenes Koptsis
2011-01-10 23:07:23 UTC
Permalink
That's nice!
I ll test again tomorrow.
Post by Benoît Minisini
Post by Demosthenes Koptsis
This is what is said here
http://gambasdoc.org/help/howto/extern?v3
"More On Pointers"
READ #mypointer, var1_4byte
mypointer += 4
READ #mypointer, var2_4byte
i tried
Print "\nRead the pointer"
pPointer += 4
But this raises an error, "Type mismatch, wanted number get pointer
instead" Is this a bug?
I have implemented pointer arithmetic in revision #3452, so you should not
have that error anymore.
Regards,
--
Regards,
Demosthenes
Demosthenes Koptsis
2011-01-11 16:38:11 UTC
Permalink
In the following example, i get "illegal instruction" at line 9
Print MkPointer$(pPointer)

--------------
' Gambas module file

Public Sub Main()

Dim pPointer As Pointer

pPointer = Alloc(1)

Print MkPointer$(pPointer)

Free(pPointer)

End
--------------
--
Regards,
Demosthenes Koptsis.
Demosthenes Koptsis
2011-01-17 10:50:17 UTC
Permalink
Benoit sorry if you feel that i discuss C++ cases in a Gambas list but i
try to describe some cases of pointers and the usage of them in Gambas.

For me and as i think for any Gambas developer the subject of pointers
and reading man 3 is a new thing.

Same questions may be come also in the future from other users which
they try to use external functions so please be patient and give help,
as you can, to newbies.

On the other hand if you think that this C++ cases are not subjects
about Gambas please tell me to stop, i will understand and sorry.
--
Regards,
Demosthenes Koptsis.
Ian Haywood
2011-01-17 11:00:26 UTC
Permalink
On Mon, Jan 17, 2011 at 9:50 PM, Demosthenes Koptsis
Post by Demosthenes Koptsis
Benoit sorry if you feel that i discuss C++ cases in a Gambas list but i
try to describe some cases of pointers and the usage of them in Gambas.
IMHO the issue is C pointers aren't an easy topic and good conceptual
understanding
is important. Anyone who contemplates interfacing with a C API using
pointers would need to sit down and read a standard C textbook to get a proper
understanding: it's not something where you can give a few "quick tips" in a
Gambas manual and then people can actually go away and use them.
Similarly I can see how it's hard for Benoit and others to answer your questions
without launching into a ten-page tutorial on C pointers.

More importantly they have no other use in the language, so there's no point
learning about gambas pointers unless you are prepared to learn C anyway.

My advice would be to make a choice:
1. sit down and learn basic C and how to use C pointers, then come
back to this problem
2. leave Gambas pointers alone and concentrate on other sections

Ian
Demosthenes Koptsis
2011-01-17 11:20:49 UTC
Permalink
Post by Ian Haywood
On Mon, Jan 17, 2011 at 9:50 PM, Demosthenes Koptsis
Post by Demosthenes Koptsis
Benoit sorry if you feel that i discuss C++ cases in a Gambas list but i
try to describe some cases of pointers and the usage of them in Gambas.
IMHO the issue is C pointers aren't an easy topic and good conceptual
understanding
is important. Anyone who contemplates interfacing with a C API using
pointers would need to sit down and read a standard C textbook to get a proper
understanding: it's not something where you can give a few "quick tips" in a
Gambas manual and then people can actually go away and use them.
Similarly I can see how it's hard for Benoit and others to answer your questions
without launching into a ten-page tutorial on C pointers.
Yes and this will be the answers for any newbie who will start with
external functions.

i dont think that C++ programmers will come to Gambas to use external
functions, the opposite i think will be.

Gambas users will learn the basics of C++ and start use Gambas.

My case is that.

On the other hand i am not completely unaware of C++.
i just have not used to man(3) yet as it is expected.
Post by Ian Haywood
More importantly they have no other use in the language, so there's no point
learning about gambas pointers unless you are prepared to learn C anyway.
1. sit down and learn basic C and how to use C pointers, then come
back to this problem
i have already did this basic steps.
now i study other things as how c++ functions implements in Gambas.
The answer of Benoit was right about how to free a pointer,

cases which use Free() instruction of Gambas or
cases which call external functions to free a pointer which is declared
in Gambas.

This is a detail not easily shown and i must describe it for a Gambas
user, it is good to know it.
Post by Ian Haywood
2. leave Gambas pointers alone and concentrate on other sections
Ian
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
so, in previous example i think it works like this,

' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
'void free(void *ptr);
Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"


Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory

free_ptr(pFunc)
pFunc = 0

End

and i hope these examples and these cases will be helpful for anyone.
--
Regards,
Demosthenes Koptsis.
Benoît Minisini
2011-01-17 11:27:15 UTC
Permalink
Post by Demosthenes Koptsis
i dont think that C++ programmers will come to Gambas to use external
functions, the opposite i think will be.
We are talking about C. Calling C++ functions from Gambas is almost
impossible.
--
Benoît Minisini
Demosthenes Koptsis
2011-01-17 11:37:32 UTC
Permalink
sorry my wrong expression.
Post by Benoît Minisini
Post by Demosthenes Koptsis
i dont think that C++ programmers will come to Gambas to use external
functions, the opposite i think will be.
We are talking about C. Calling C++ functions from Gambas is almost
impossible.
--
Regards,
Demosthenes Koptsis.
Benoît Minisini
2011-01-17 12:33:38 UTC
Permalink
Post by Demosthenes Koptsis
sorry my wrong expression.
No problem.

Another point: at the moment, Alloc() and Free() are not the equivalent of
malloc() and free() in C. So you cannot use Free() to free a memory allocation
returned as a pointer by a C function. You must use the free() function
located in the C library directly.

Regards,
--
Benoît Minisini
Demosthenes Koptsis
2011-01-17 19:36:38 UTC
Permalink
Post by Benoît Minisini
Post by Demosthenes Koptsis
sorry my wrong expression.
No problem.
Another point: at the moment, Alloc() and Free() are not the equivalent of
malloc() and free() in C. So you cannot use Free() to free a memory allocation
returned as a pointer by a C function. You must use the free() function
located in the C library directly.
Regards,
About that,

The next example uses free() function of C to free a pointer.

Example1
------------
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
'void free(void *ptr);
Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"


Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory

free_ptr(pFunc)
pFunc = 0

End
------------
free_ptr(pFunc)
is the free() function of C and here frees a pointer which was allocated
internal by get_current_dir_name().

But free() function of C, cannot free a pointer allocated by Alloc() of
Gambas. May be this is obvious or expected but is good to say it.

Example2
--------------
' Gambas module file
'void free(void *ptr);
Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"

Public Sub Main()

Dim pPointer As Pointer

pPointer = Alloc(4)

free_ptr(pPointer)

End

example2 gives signal #6.
--
Regards,
Demosthenes
John Spikowski
2011-01-17 21:59:05 UTC
Permalink
Post by Demosthenes Koptsis
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc
Print sWorkingDirectory
free_ptr(pFunc)
pFunc = 0
End
Isn't pFunc a Gambas variable (memory location) which is maintained by
the Basic? Why would you free it externally? I would think Gambas would
complain about that.
Demosthenes Koptsis
2011-01-18 08:08:32 UTC
Permalink
Post by John Spikowski
Post by Demosthenes Koptsis
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = get_current_dir_name()
Print pFunc
Print sWorkingDirectory
free_ptr(pFunc)
pFunc = 0
End
Isn't pFunc a Gambas variable (memory location) which is maintained by
the Basic? Why would you free it externally? I would think Gambas would
complain about that.
Yes pFunc is a pointer. A pointer is a variable that holds a memory
address.

At line
Dim pFunc As Pointer
i declare the variable.

But i do not use Alloc() to alocate a memory address with Gambas3.

i do this because the external function get_current_dir_name() allocates
a memory address internal.

This is described in manual
http://manpages.ubuntu.com/manpages/dapper/en/man3/get_current_dir_name.3.html

"get_current_dir_name, which is only prototyped if _GNU_SOURCE is
defined, will malloc(3) an array"

to free a pointer in such a case i have to use free() function of C.

Next is the same example but with an error.
The error is that i allocate memory with Gambas3 and try to Free() the
memory later. But when i call get_current_dir_name() this function set
again a new pointer and the address from Gambas3 Alloc() is not
accesible any more. So when i try to Free() the pointer in fact i try to
free an unlocated memory!

' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"

Public Sub Main()

Dim pFunc As Pointer
Dim sWorkingDirectory As String

'get_current_dir_name
pFunc = Alloc(4096)
Print pFunc '(Pointer 0x185b918)

pFunc = get_current_dir_name()
Print pFunc '(Pointer 0x185d780)

'notice that address was changed
' from now on the address 0x185b918
'is unreachable
'and the new address 0x185d780
'is not allocated by Gambas3 !
'so we cannot free both of them
'with Free() function of Gambas3!

sWorkingDirectory = Str@(pFunc)
Print sWorkingDirectory

Free(pFunc)
pFunc = 0

End
Post by John Spikowski
------------------------------------------------------------------------------
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
https://lists.sourceforge.net/lists/listinfo/gambas-user
--
Regards,
Demosthenes Koptsis.
John Spikowski
2011-01-19 06:08:28 UTC
Permalink
Post by Demosthenes Koptsis
Post by John Spikowski
Isn't pFunc a Gambas variable (memory location) which is maintained by
the Basic? Why would you free it externally? I would think Gambas would
complain about that.
Yes pFunc is a pointer. A pointer is a variable that holds a memory
address.
At line
Dim pFunc As Pointer
i declare the variable.
But i do not use Alloc() to alocate a memory address with Gambas3.
i do this because the external function get_current_dir_name() allocates
a memory address internal.
This is described in manual
http://manpages.ubuntu.com/manpages/dapper/en/man3/get_current_dir_name.3.html
"get_current_dir_name, which is only prototyped if _GNU_SOURCE is
defined, will malloc(3) an array"
to free a pointer in such a case i have to use free() function of C.
Next is the same example but with an error.
The error is that i allocate memory with Gambas3 and try to Free() the
memory later. But when i call get_current_dir_name() this function set
again a new pointer and the address from Gambas3 Alloc() is not
accesible any more. So when i try to Free() the pointer in fact i try to
free an unlocated memory!
' Gambas module file
'char *get_current_dir_name(void);
Extern get_current_dir_name() As Pointer In "libc:6"
Public Sub Main()
Dim pFunc As Pointer
Dim sWorkingDirectory As String
'get_current_dir_name
pFunc = Alloc(4096)
Print pFunc '(Pointer 0x185b918)
pFunc = get_current_dir_name()
Print pFunc '(Pointer 0x185d780)
'notice that address was changed
' from now on the address 0x185b918
'is unreachable
'and the new address 0x185d780
'is not allocated by Gambas3 !
'so we cannot free both of them
'with Free() function of Gambas3!
Print sWorkingDirectory
Free(pFunc)
pFunc = 0
End
I was thinking that you were freeing pFunc (using it's VARPTR) rather
than freeing the memory associated to the pointer being returned. I have
to agree with Benoît that working with external memory (variables) and
structures are a 'at your own risk' task in Gambas. (rules vary by what
your trying to access)
Demosthenes Koptsis
2011-01-19 07:27:39 UTC
Permalink
Post by John Spikowski
I was thinking that you were freeing pFunc (using it's VARPTR) rather
than freeing the memory associated to the pointer being returned. I have
to agree with Benoît that working with external memory (variables) and
structures are a 'at your own risk' task in Gambas. (rules vary by what
your trying to access)
Hmm.. ok i see maybe the name of external function made a confusion.

Extern free_ptr(ptr As Pointer) In "libc:6" Exec "free"

the name of C function is free() but this name is also a Gambas3 word.

so i declare the external function with different name.

i choose as name free_ptr(ptr As Pointer), a better name would be
free_pointer(pointer As Pointer) or free_a_c_pointer(pointer As
Pointer)

Here there is no relevance with VarPtr.

Ptr is just an abbreviation for Pointer word which we can use in code.
--
Regards,
Demosthenes Koptsis.
Loading...