0

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.

Community
  • 1
  • 1
Andy Lin
  • 397
  • 1
  • 2
  • 21
  • 2
    I don't know the context, but all the code does is finding the next available slot in an array, using the value `0` as sentinel for an available slot. It relies on static initialization setting everything to zero (which is guaranteed, but bad practice and generally dangerous). I would **not** recommend this book, it does everything in needlessly convoluted ways. As we can see from this code, the author even fails to iterate over an array. A sane person would write `for(size_t i=1; i – Lundin Jan 21 '19 at 11:56
  • you have extremely wron way of using whitespace: `! * p` =? `!*p` even better `!(*p)` `* p` => `*p` . it is very hard to read – 0___________ Jan 21 '19 at 11:58
  • @P__J__ This is copy/paste from the book. Blame Schreiner. – Lundin Jan 21 '19 at 11:59
  • @Lundin So your comment is even more easy to understand :) I am going to write a book about North Korea ballet. Have same knowledge and experience :) `MANY` - amazing name :) There are only 10 so it should be `FEW` – 0___________ Jan 21 '19 at 12:02
  • Is this book lame enough to this point? I found people recommend it in this topic: https://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c – Andy Lin Jan 22 '19 at 02:11
  • The purpose I want to learn OO concept is that I want to organize large program in more efficient, smart way. Can everyone recommend some book? – Andy Lin Jan 22 '19 at 02:13
  • See [the C tag wiki](https://stackoverflow.com/tags/c/info) and https://stackoverflow.com/q/562303/9254539 – eesiraed Jan 23 '19 at 02:30

3 Answers3

2

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

Community
  • 1
  • 1
bruno
  • 32,421
  • 7
  • 25
  • 37
2

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.

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
  • What I understood in this sentence **"we can represent each object and each set as small, unique, positive integer values used as indices into an array heap"** is that, for example, index 0 represents an apple object, index 1 represents a banana object, index 2 represents an apple set, and index 3 represents a banana set. So index of heap, not heap itself, can represents, but not stores, not only object but also set. – Andy Lin Jan 22 '19 at 11:24
  • And, **"If an object is a member of a set, its array element contains the integer value representing the set."** This give me an idea that heap[0] is 2. Index 0(apple object) is a member of a apple set, its array element(heap[0]) contains 2(integer value representing the apple set) – Andy Lin Jan 22 '19 at 11:29
  • Finally, **"Objects, therefore, point to the set containing them."** means: apple object(index 0) always points to apple set(index 2) because array connects them(object and set). – Andy Lin Jan 22 '19 at 11:34
0

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.

Andy Lin
  • 397
  • 1
  • 2
  • 21