void myqsort(void *base, int n, int size, int (*comp)()) にたいして、 double a[N]; myqsort(a, N, sizeof(a[0]), cmp); と呼び出した場合、内部で、&a[i] をもとめるのに、 base + i * size としているのが問題になっています。 void *base なので、sizeof(void) がわからないというエラーのため、base + i * size が計算できないのです。
26 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
>282 C89でもそうなっていったわ 3.1.2.5 Types A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. Other pointer types need not have the same representation or alignment requirements. まさかのキャラポ、まぁ感覚的には気持ち悪いが規格が正義だからいいや
>288 なんか書いてないかなぁと思ってRationale読んだら書いてあった 3.1.2.5 Types A pointer to void must have the same represen- tation and alignment as a pointer to character; the intent of this rule is to allow existing programs which call library functions (such as memcpy and free) to con- tinue to work. やっぱりそうだったか
§3.9.2 4 Objects of cv-qualified (3.9.3) or cv-unqualified type void* (pointer to void), can be used to point to objects of unknown type. A void* shall be able to hold any object pointer. A cv-qualified or cvunqualified (3.9.3) void* shall have the same representation and alignment requirements as a cv-qualified or cv-unqualified char*.
はいいのだが
§C.1.2.4.10 Change: Converting void* to a pointer-to-object type requires casting char a[10]; void *b=a; void foo() { char *c=b; } ISO C will accept this usage of pointer to void being assigned to a pointer to object type. C + + will not. Rationale: C + + tries harder than C to enforce compile-time type safety. Effect on original feature: Deletion of semantically well-defined feature. Difficulty of converting: Could be automated. Violations will be diagnosed by the C++ translator. The fix is to add a cast For example: char *c = (char *) b; How widely used: This is fairly widely used but it is good programming practice to add the cast when assigning pointer-to-void to pointer-to-object. Some ISO C translators will give a warning if the cast is not used.
/* * subbuf_read_actor - read up to one subbuf's worth of data */ static int subbuf_read_actor(size_t read_start, struct rchan_buf *buf, size_t avail, read_descriptor_t *desc, read_actor_t actor) { void *from; int ret = 0;