Segmentation Violation
Author |
Message |
Aange10
|
Posted: Thu Jul 26, 2012 2:40 pm Post subject: Segmentation Violation |
|
|
What is it you are trying to achieve?
I've started a little mini project to add the dictionary type to turing through a module. I'm just to a phase of being able to use the dictionary.
What is the problem you are having?
After importing the module, I initialize a flexible array with new myArray, newValue, and I get a Segmentation Violation. Which, from google, appears to mean that Turing is unable to edit the memory the array is stored in. Or something like that. How can I fix it?
Describe what you have tried to solve this problem
All I've tried is switching from .tu and .t files
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Attached
Please specify what version of Turing you are using
The second to latest one (the one without the compiler bug)
Description: |
|
Download |
Filename: |
Dictionary.rar |
Filesize: |
2.13 KB |
Downloaded: |
77 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
mirhagk
|
Posted: Thu Jul 26, 2012 2:57 pm Post subject: RE:Segmentation Violation |
|
|
I am impressed. A segmentation violation is from the olden days, these usually only come up in assembly nowadays. Basically your program is trying to alter memory that isn't it's own (might be the OS's, might be some other program) . I'll take a look for you since I'm interested now.
|
|
|
|
|
|
Aange10
|
Posted: Thu Jul 26, 2012 5:41 pm Post subject: RE:Segmentation Violation |
|
|
Let me know if you find anything
|
|
|
|
|
|
Dreadnought
|
Posted: Thu Jul 26, 2012 6:56 pm Post subject: Re: Segmentation Violation |
|
|
The problem is that forward declarations and flexible arrays don't work together in classes. I'm not sure why this is.
In your case, you don't actually need the forward declarations, just reorganize your function declarations.
However, you shouldn't need to use forward declarations in classes anyway, instead use implement_by and implement along with deferred. It also has the added bonus of making your code slightly more organized by separating the interface from the implementation.
Here's an example:
Produces segmentation violation: Turing: | class A
export getInt, double, initialize
var arr : flexible array 0 .. -1 of int
forward proc double (ind : int)
forward proc doubleHelp (ind : int)
body proc double (ind : int)
if ind >= 0 then
doubleHelp (ind)
end if
end double
body proc doubleHelp (ind : int)
arr (ind) *= 2
double (ind - 1)
end doubleHelp
fcn getInt (ind : int) : int
result arr (ind)
end getInt
proc initialize (i : int)
new arr, i
for j : 0 .. i
arr (j) := j
end for
end initialize
end A
const SIZE := 5
var a : ^A
new a
a->initialize (SIZE)
for i : 0 .. (SIZE)
put a->getInt (i)
end for
a->double (SIZE)
put ""
for i : 0 .. (SIZE)
put a->getInt (i)
end for |
Does not produce segmentation violation:
A.tu Turing: | unit
class A
implement by B
export getInt, double, initialize
var arr : flexible array 0 .. -1 of int
deferred proc double (ind : int)
deferred proc doubleHelp (ind : int)
deferred fcn getInt (ind : int) : int
deferred proc initialize (i : int)
end A |
B.tu Turing: | unit
class B
implement A
body proc initialize (i : int)
new arr, i
for j : 0 .. i
arr (j) := j
end for
end initialize
body proc double (ind : int)
if ind >= 0 then
doubleHelp (ind)
end if
end double
body proc doubleHelp (ind : int)
arr (ind) *= 2
double (ind - 1)
end doubleHelp
body fcn getInt (ind : int) : int
result arr (ind)
end getInt
end B
|
Then as a main program: Turing: | import A
const SIZE := 5
var a : ^A
new a
a->initialize (SIZE)
for i : 0 .. (SIZE)
put a->getInt (i)
end for
a->double (SIZE)
put ""
for i : 0 .. (SIZE)
put a->getInt (i)
end for |
|
|
|
|
|
|
Aange10
|
Posted: Fri Jul 27, 2012 1:50 am Post subject: RE:Segmentation Violation |
|
|
Awesome, thanks a ton you two.
|
|
|
|
|
|
|
|