Andy Lin
Andy Lin

Reputation: 447

Unable to understand context in book "OOP in C" by Axel Schreiner

I don't know if this is suitable to ask in here, because this question is much more specific rather than general.

I've been reading this book: OOP in C - Axel Schreiner but found that it's hard to understand.

For example, in section 1.7: An Implementation — Set

If an object stores no information and if every object belongs to at most one set, we can represent each object and each set as small, unique, positive integer values used as indices into an array heap[]. If an object is a member of a set, its array element con- tains the integer value representing the set. Objects, therefore, point to the set containing them.

alongside with

#if ! defined MANY || MANY < 1
#define MANY 10
#endif
static int heap [MANY];
void * new (const void * type, ...)
{    int * p; /* & heap[1..] */
     for (p = heap + 1; p < heap + MANY; ++ p)
         if (! * p)
             break;
     assert(p < heap + MANY);
     * p = MANY;
     return p;
}

I cannot connect this two together.

What does "object stores no information" mean?

What does "every object belongs to at most one set" mean?

What does "If an object is a member of a set, its array element con- tains the integer value representing the set" mean?

I read it so hard but still cannot get it. Thanks.

Upvotes: 0

Views: 134

Answers (3)

Andy Lin
Andy Lin

Reputation: 447

Response to myself.

If an object stores no information and if every object belongs to at most one set, we can represent each object and each set as small, unique, positive integer values used as indices into an array heap[].

Let's say that a integer value can represent a set or an object. E.g:

0 represents an apple object

1 represents a banana object

2 represents an apple set

3 represents a banana set

In this case, object is merely an integer value, thus cannot store any information.

Also, integer values(0, 1, 2, 3) above can be used as indices of heap array.

If an object is a member of a set, its array element contains the integer value representing the set. Objects, therefore, point to the set containing them.

Following above example, 0(object apple) is a member of 2(set apple).

Therefore, heap[0] = 2.

Note: The conclusion heap[0] = 2 is only held under the condition that every object belongs to at most one set.

Upvotes: 0

Alain Merigot
Alain Merigot

Reputation: 11557

Assume the objects can be fruits apples, bananas or oranges

What does "object stores no information" mean?

You just want to know the set of the object (is it an apple), and do not need to store other informations (where and when has it been produced, its wieght, etc).

What does "every object belongs to at most one set" mean?

An object cannot be at the same time an apple and an orange

What does "If an object is a member of a set, its array element contains >the integer value representing the set" mean?

Then you can just use an index in an array to describe the object.

Actually, you can just use any unique int identifier to describe the object, and the given code is uselessly complex to do that.

Upvotes: 2

bruno
bruno

Reputation: 32596

If I well understand :

What does "object stores no information" mean?

only the object itself is useful, you can compare it with others (== or !=), but its value by itself is not relevant

What does "every object belongs to at most one set" mean?

What does "If an object is a member of a set, its array element con- tains the integer value representing the set" mean?

When you look what he proposes you see new just search in heap for a null value, when an element of heap is 0 that means the corresponding object is not yet used, so he marks the element setting it with MANY (any null value is ok in fact) and return the address of the element.

The code doesn't manage the case when there is no free element.

You do not give the free but that one will certainly just reset the value to 0


All of that is anyway a little strange

Upvotes: 2

Related Questions