English 中文(简体)
Scheme, getting the pointer from pointed struct
原标题:

Assume I have a such struct:

(define-struct node (value next))

;and making 2 nodes, parent pointing to child as next.
(define child (make-node 2 null))

(define parent (make-node 1 child))

Under PLT Scheme, while having child in my hand (and not knowing that parent is the pointer), is it possible to find the pointing node sctuct without knowing the parent?

最佳回答

No, it is not possible. And in fact, if you don t keep a reference to the parent object around anywhere, the parent object may be garbage collected. Any object which can be proven to never be reachable may be deleted at any time. If you don t store a reference to that object anywhere, then it is not reachable.

Now, there are a couple of options if you really did want to get back to the parents. You could save a reference to your parent in your child node (you would have to set it after creating your parent). A less optimal solution, but one that would work if you can t change the data structures in question for some reason, would be keep a list of all nodes that you create, and then search through that list for one which had a child that was eq? to the node in question. Or you could do the same, but save them in a hash table, with the key being the child and the value being the parent (or list of parents, if there can be more than one), for greater efficiency. I m not sure if any of these solutions would work for you, as they may depend on the language level that you are using, if you are doing this in an introductory class and not using the full language.

问题回答

No, you can t. Where did you even get the idea this might be possible?





相关问题
Nested struct variable initialization

How can I initialize this nested struct in C? typedef struct _s0 { int size; double * elems; }StructInner ; typedef struct _s1 { StructInner a, b, c, d, e; long f; char[16] s; }...

passing primitive or struct type as function argument

I m trying to write some reasonably generic networking code. I have several kinds of packets, each represented by a different struct. The function where all my sending occurs looks like: - (void)...

Error: "Cannot modify the return value" c#

I m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error ...

Sending struct over TCP (C-programming)

I have a client and server program where I want to send an entire struct from the client and then output the struct member "ID" on the server. I have done all the connecting etc and already managed ...

Scheme, getting the pointer from pointed struct

Assume I have a such struct: (define-struct node (value next)) ;and making 2 nodes, parent pointing to child as next. (define child (make-node 2 null)) (define parent (make-node 1 child)) Under ...

Changing a struct inside another struct in a foreach loop

The following code prints (when invoking MyMethod): 0 0 0 1 I would expect it to print: 0 0 1 1 Why is this? Code: private struct MyStruct { public MyInnerStruct innerStruct; } private ...

热门标签