It makes me a bit confused

Started by psilwen, November 09, 2017, 03:21:27 AM

Previous topic - Next topic

psilwen

> (struct 'A "char*")
A
> (pack A "HELLO")
"176154d00"
> (unpack A (pack A "HELLO"))
("HELLO")
> (setq a (pack A "HELLO"))
"0154d00"
> (unpack A a)
("06") ; <------------
> (setq b "HELLO")
"HELLO"
> (setq a (pack A b))
"p154d00"
> (unpack A a)
("HELLO")
(reverse \"newlisp\")

rrq

#1
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(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.