1. Testing atoms if pointer

Is there a built-in function in Euphoria 4 to test if an atom is a pointer? In the moment in Windows I use the isBadxxxPtr functions to test a value e.g.

include std/dll.e 
include std/machine.e 
include std/text.e 
include std/console.e 
 
constant Kernel32 = open_dll("kernel32.dll") 
 
-- checking if read access 
 
constant isBadReadptr = define_c_func(Kernel32, "IsBadReadPtr", {C_LONG, C_LONG}, C_INT) 
------------------------------------------------------------ 
 
function is_ptr(object a) 
   -- simple check 
   if sequence(a) then 
      return 0 
   end if 
   if c_func(isBadReadptr, {a,1} ) then 
      return 0 
   end if 
   return 1 
end function 
 
procedure check_ptr(object item) 
    puts(1, "item = " & sprint(item)) 
    if is_ptr(item) then 
       puts(1, ", is pointer address\n") 
    else 
       puts(1, ", is not a pointer\n") 
    end if 
end procedure 
 
atom a = 1      check_ptr(a) 
a = allocate(4) check_ptr(a) 
free(a)         --check_ptr(a) 
a = 10          check_ptr(a) 
a = 'a'         check_ptr(a) 
a = allocate_string("This is a string") check_ptr(a) 
free(a) 
a = -1          check_ptr(a) 
 
sequence s = "Hello"    check_ptr(s) 
 
display("Enter a key to end ...") 
if wait_key() then end if 
 

Maybe there is a more general test available now for the Windows and Unix families?

Roland

new topic     » topic index » view message » categorize

2. Re: Testing atoms if pointer

RStowasser said...

Is there a built-in function in Euphoria 4 to test if an atom is a pointer? In the moment in Windows I use the isBadxxxPtr functions to test a value e.g.

Maybe there is a more general test available now for the Windows and Unix families?

No. In fact, you probably shouldn't be using isBadxxxPtr functions at all.

Matt

new topic     » goto parent     » topic index » view message » categorize

3. Re: Testing atoms if pointer

mattlewis said...
RStowasser said...

Is there a built-in function in Euphoria 4 to test if an atom is a pointer? In the moment in Windows I use the isBadxxxPtr functions to test a value e.g.

Maybe there is a more general test available now for the Windows and Unix families?

No. In fact, you probably shouldn't be using isBadxxxPtr functions at all.

Matt

Hi Matt,

thank you for the link. These were impressive and convincing arguments. So probably there is no simple solution to find out if I had allocated memory to an atom?

Roland

new topic     » goto parent     » topic index » view message » categorize

4. Re: Testing atoms if pointer

RStowasser said...

So probably there is no simple solution to find out if I had allocated memory to an atom?

There is safe.e, which is meant to track this sort of thing. It's usually more for debugging, but essentially, you just need to keep track of this stuff by storing your allocations and forgetting them when they're freed. This is basically the approach taken by safe.e.

This is probably the point at which it's a good idea to ask what you're trying to accomplish, rather than how you're trying to accomplish it. smile So, why do you want to know if you're dealing with an allocated pointer?

Matt

new topic     » goto parent     » topic index » view message » categorize

5. Re: Testing atoms if pointer

mattlewis said...

This is probably the point at which it's a good idea to ask what you're trying to accomplish, rather than how you're trying to accomplish it. smile So, why do you want to know if you're dealing with an allocated pointer?

Matt

Currently I am experimenting with the Iup dlls and trying to wrap functions with variable arguments. Basically I am doing this in a similar way like this (a little bit simplified):

Ihandle* IupHbox(Ihandle *child, ...); [in C]

--constant hIupHbox = def_c_func(hIup, "IupHbox", {P}, P) 
object hIupHbox 
public function IupHbox(sequence child) 
    -- returns id of created element, or NULL if error 
    hIupHbox = -1 
    sequence c_args = {} 
    for x = 1 to length(child) do 
    	c_args &= P 
    end for 
 
    hIupHbox = define_c_func(hIup, "IupHbox", c_args, P) 
     
    Ihandle id_element = c_func(hIupHbox, child) 
    if id_element = NULL then crash("something wrong with IupHbox") end if 
    return id_element 
end function 


This is a relative simple example. But there are some functions in the Iup library in which case I am not sure if I always can use C_POINTER. So I wondered if there would be a simple check to see if an arguement in the sequence is a pointer or a value.

Roland

new topic     » goto parent     » topic index » view message » categorize

6. Re: Testing atoms if pointer

RStowasser said...

This is a relative simple example. But there are some functions in the Iup library in which case I am not sure if I always can use C_POINTER. So I wondered if there would be a simple check to see if an arguement in the sequence is a pointer or a value.

OK, I see what you're doing. The real issue is the expected size of the argument. In a 32-bit system*, a C int, pointer or long will all be 4 bytes long. For passing parameters, the sign doesn't really matter. You need to be careful if the arguments take shorts, chars (NB not pointers to char), floats or doubles.

I'm not familiar with IUP, but I suspect that most of them can be treated like C_POINTERs

Matt

* I have a mostly working 64-bit implementation of euphoria, and for that C_INT will be a different size than C_LONG and C_POINTER.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu