It makes me a bit confused

Q&A's, tips, howto's

It makes me a bit confused

Postby psilwen » Thu Nov 09, 2017 11:21 am

Code: Select all
> (struct 'A "char*")
A
> (pack A "HELLO")
"\176\154d\000"
> (unpack A (pack A "HELLO"))
("HELLO")
> (setq a (pack A "HELLO"))
"0\154d\000"
> (unpack A a)
("\006") ; <------------
> (setq b "HELLO")
"HELLO"
> (setq a (pack A b))
"p\154d\000"
> (unpack A a)
("HELLO")
(reverse "newlisp")
psilwen
 
Posts: 21
Joined: Thu Jul 03, 2014 5:25 am

Re: It makes me a bit confused

Postby ralph.ronnquist » Sat Nov 11, 2017 1:02 am

I'm not sure which part is confusing for you. I think the story would be something like the following:

A char* is a pointer to a char, and not the array of char that it points to.

Thus, with
Code: Select all
(pack A "HELLO")
a char* record created with a pointer that points to the temporarily allocated char array "HELLO", which then incidentally survives to the next prompt, but not as much as being able to be unpacked. The pointer points into the heap, to an address that is reused with the next input.

When b is set however, the char array is also preserved, as the value of b, and therefore the second packing yields a pointer record with a pointer to that preserved character array.
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests