general protection fault
general protection fault
I am trying to write a program that uses a linked list as a data structure. I am using callocate and pointers. The program runs sometimes and gives a GPF sometimes. A dump of the data structure shows it is linked properly. It is not easy to diagnose a GPF since it cannot be trapped.
Linked list GFP
Even if the list is correctly linked it could be due to a 'wild' access with an erroneous pointer.....
Regards.
Luis
Regards.
Luis
general protection fault
I compiled my program with -g switch and ran in GDB. It failed immediately with this message ...
SIGSEGV segmentation fault
0x00009dac in free()
SIGSEGV segmentation fault
0x00009dac in free()
general protection fault
ok i figured out how to operate the debugger
the program does not fail every time
when it does fail, it is at a line like this ...
type cell
as string ptr value
as cell ptr prevcell
as cell ptr nextcell
end type
...
btm = callocate(len(cell))
btm->value = callocate(3)
*btm->value= "btm"
the program does not fail every time
when it does fail, it is at a line like this ...
type cell
as string ptr value
as cell ptr prevcell
as cell ptr nextcell
end type
...
btm = callocate(len(cell))
btm->value = callocate(3)
*btm->value= "btm"
The way you have a String ptr, I don't think you can do that in the way you want, String is actually a Type, that is transparent in use, so its better to use a ZString ptr
My suggestion would be to do this,
Note the +1 in order to store the null byte that marks the end of the string.
If your string will contain any chr(0), this will fail, and you'd have to do a method like this:
This way has the advantage that it will not matter if the string contains chr(0), which is the end of string marker in a zstring.
My suggestion would be to do this,
Code: Select all
type cell
as ZString ptr value
as cell ptr prevcell
as cell ptr nextcell
end type
...
Dim somestring As String
somestring = "foo"
btm = callocate(sizeof(cell))
btm->value = Allocate(Len(somestring) + 1)
*btm->value = somestring
Print *btm->value
If your string will contain any chr(0), this will fail, and you'd have to do a method like this:
Code: Select all
type cell
as Any ptr value
as Integer value_len
as cell ptr prevcell
as cell ptr nextcell
end type
...
Dim somestring As String
somestring = "foo"
btm = callocate(sizeof(cell))
btm->value_len = Len(somestring)
btm->value = Allocate(btm->value_len)
memcpy(btm->value, strptr(somestring), btm->value_len)
' Then to get it back...
Dim temp As String
temp = Space(btm->value_len)
memcpy(strptr(somestring), btm->value, btm->value_len)
Print temp
Nah, you can callocate and use strings, the problem is you're not allocating enough, you're only allocating 3 bytes.
needs to become
EDIT: oops i was mixed up in my original post ;)
Code: Select all
btm->value = callocate(3)
Code: Select all
btm->value = callocate(len( string ))
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
You don't need to allocate space for anything more than just the descriptor. When you give the string a value, then FB will allocate memory for the string's data elsewhere, and use that.
When you deallocate the string's descriptor later, you should empty the string by setting it to "" first. This will make FB deallocate the memory taken up by the string's data. Otherwise, you won't be able to deallocate it later, and you'll have a memory leak.
When you deallocate the string's descriptor later, you should empty the string by setting it to "" first. This will make FB deallocate the memory taken up by the string's data. Otherwise, you won't be able to deallocate it later, and you'll have a memory leak.
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
-
- Posts: 605
- Joined: Feb 18, 2006 13:30
- Location: Alexandria / Egypt
- Contact:
well, adding a new (internal) quirk isn't that bad, especially if it's for a good cause ;)v1ctor wrote:Unless the STRING type become a real object, that may not be simple to support, making it an object isn't a simple task, due the quirks that are handled by the compiler internally, unfortunately.
If it was a class, the users could choose between left( foo, 1 ) and foo.left( 1 ) etc.
introducing a new constructor/destructor for strings wouldn't be a problem IMO, it doesn't have to become a full object, only allow new/delete to work normally with strings (just like other datatypes), fair deal i guess. :)