Re: testing for size of pointer
- Posted by Matt Lewis <matthewwalkerlewis at gmail.com> Aug 19, 2005
- 477 views
Damien wrote: > > how do you get the size of a pointer? or rather the size of the value the > pointer points > to? > > say I am using a function from a C library that returns a pointer, the pointer > points > to a string, now when I try to peek() the pointer to get the string it only > returns > the first letter in that string. How do I get the size of the string being > pointed > to? You have to know what sort of data you're pointing at. For a null terminated string, you can just scan memory and look for a zero. Some types of strings (and other data structures) store their size as part of the structure. Your string is probably of the null-terminated type. A typical null-termination routine looks like this:
function peek_string( atom string ) atom offset while peek(offset) do offset += 1 end while return peek( {string, offset-string}) end function
In general, you need to look at the documentation for whatever the data is. For complex structures, this may require knowing (or figuring out) sizes for lots of different types of data, and how the compiler handles offsets. Matt Lewis