1. [GEN] multiple pointers
- Posted by cense <cense at MAIL.RU>
Oct 14, 2000
-
Last edited Oct 15, 2000
Hey all,
i asked a similar question earlier but no one seemed to respond so here is my
second attempt at help.
how can euphoria handle multiple indirection? how can i get the value at
the end of the pointer chain? in my case the the variable is a
// in C
char **list;
i dont know all that much about multiple indirection in C/C++ but i have read
up on the subject just now and still really dont fully understand it. i know
that this variable is kind of like an array of pointers to char. but how do
get to those "values" not the addresses?
in euphoria i have
-- in Eu
atom list
and this is equivalent to the above statement in C. i tried to do this to get
the value but it Seg Faulted.
atom list, ptr, value
ptr = peek( list ) -- Seg Fault happens here
value = peek( { ptr, sizeof( ptr ) } ) -- sizeof from mixedlib.e by Bernie
Is there another way to do this that is safe and does not seg fault? can
euphoria handle something of this case? it must, i just probably dont know
jack.
thanks to anyone who can help in *any* way.
--
evil, corruption and bad taste
^cense
2. Re: [GEN] multiple pointers
Hi Cense
I replied but perhaps what I suggested was dumb.
You can't get pointers to your Euphoria atoms. You need to allocate memory
to hold the values you require. You then save a pointer to the memory (which
will need to be a certain size to hold the value(s) you need).
(You probably knew all this anyway!)
I assume "list" is a memory location that holds a pointer to memory?
I can't see why it would seg fault on a peek but I dont *think* that you
will get what you are looking for pointer wise if you use a simple peek
to get at your pointer.
I think peek will only return one byte of the address you require.
When you do
ptr = peek( list ) -- Seg Fault happens here
you might only be getting the first byte of the pointer because peek only
returns a single byte value from the specified location. If that location
holds
a 32-bit pointer that you also want to peek then you won't get the whole
pointer.
When you then use that (incorrect?) pointer to get at your array of char's
I *think* you will be peeking the totally wrong location.
Perhaps you should try peek4u instead to get the whole pointer value.
perhaps something like (untested code...sorry Jiri!)........
atom list, ptr_to_list
sequence all_values_in_list
ptr_to_list = allocate(4) -- 32-bit location to hold location of list
list = allocate(50) -- 50 byte sized values (chars?)
poke4(ptr_to_list, list) -- ptr_to_list now contains the location of the
list
-- to get at the list using your pointer to the list
ptr = peek4u(ptr_to_list) -- should give us the pointer to the list
all_values_in_list = peek({ptr, 50}) -- read all values in the list into a
sequence
Could you let us know what you are working on?
If I have got this totally wrong please let me know.
All the best
Mark
3. Re: [GEN] multiple pointers
----- Original Message -----
From: "cense" <cense at MAIL.RU>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, October 15, 2000 4:41 PM
Subject: [GEN] multiple pointers
> how can euphoria handle multiple indirection? how can i get the value at
> the end of the pointer chain? in my case the the variable is a
> // in C
> char **list;
In C, "char **list" is read as "list is a pointer to a pointer which points
to a character". Often this situation is called a "handle". It is common in
Windows programming and extremely common in Macintosh and Amiga programming.
In C, a "char *" usually means a character string which is terminated by a
zero byte.
So, in the example above, if "list" is an atom containing a 32-bit machine
address then I'd do the following...
atom list
atom charptr
integer char
sequence string
-- Get the pointer to the character data
charptr = peek4u(list)
-- Initialize the sequence
string = ""
-- Get the first character
char = peek(charptr)
-- Repeat until we find the terminating zero
while char do
-- Append the character just picked up
string &= char
-- Point to the next character
charptr += 1
-- Grab it from memory
char = peek(charptr)
end while
-- See what we got.
? string
...but... if "list" is some other form of machine address, this method will
not work and you just might get a segmentation address.
------
Derek Parnell
Melbourne, Australia
(Vote [1] The Cheshire Cat for Internet Mascot)
4. Re: [GEN] multiple pointers
On Sun, 15 Oct 2000, Derek Parnell wrote:
>> ----- Original Message -----
>> From: "cense" <cense at MAIL.RU>
>> To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
>> Sent: Sunday, October 15, 2000 4:41 PM
>> Subject: [GEN] multiple pointers
>>
>>
>>
>> > how can euphoria handle multiple indirection? how can i get the value at
>> > the end of the pointer chain? in my case the the variable is a
>>
>> > // in C
>> > char **list;
>>
>> In C, "char **list" is read as "list is a pointer to a pointer which points
>> to a character". Often this situation is called a "handle". It is common in
>> Windows programming and extremely common in Macintosh and Amiga programming.
>> In C, a "char *" usually means a character string which is terminated by a
>> zero byte.
>>
>> So, in the example above, if "list" is an atom containing a 32-bit machine
>> address then I'd do the following...
>>
>> atom list
>> atom charptr
>> integer char
>> sequence string
>>
>> -- Get the pointer to the character data
>> charptr = peek4u(list)
>>
>> -- Initialize the sequence
>> string = ""
>>
>> -- Get the first character
>> char = peek(charptr)
>>
>> -- Repeat until we find the terminating zero
>> while char do
>>
>> -- Append the character just picked up
>> string &= char
>>
>> -- Point to the next character
>> charptr += 1
>>
>> -- Grab it from memory
>> char = peek(charptr)
>>
>> end while
>>
>> -- See what we got.
>> ? string
>>
>> ...but... if "list" is some other form of machine address, this method will
>> nnot work and you just might get a segmentation address.
>>
>> ------
>> Derek Parnell
>> Melbourne, Australia
>> (Vote [1] The Cheshire Cat for Internet Mascot)
Thanks Derek! That elaborates a little on what Mark had to say but i now
believe my *major* problem was easlier in my code. But that does not mean
the problem i was trying to get solved here is wasted, i will need this
solution a little later.
--
evil, corruption and bad taste
^cense