Re: fshort-wchar
Re: fshort-wchar
- Subject: Re: fshort-wchar
- From: Ron Hunsinger <email@hidden>
- Date: Sun, 15 Apr 2012 18:33:54 -0700
You're missing a level of indirection.
> const wchar_t *a=(const wchar_t *)ap, *b=(const wchar_t *)bp;
should be
const wchar_t *a=*(const wchar_t * const*)ap, *b=*(const wchar_t * const*)bp;
That is, when qsort wants to compare your items, it passes the addresses of the items, each of which is a pointer to wchar_t. Thus, the nominally void* parameters to compare are pointers to pointers to wchar_t
-Ron Hunsinger
On Apr 13, 2012, at 5:36 AM, Anders Markussen wrote:
> There seems to be a problem in clang with integral promotions on wchar_t with -fshort-wchar.
> Using Xcode 4.2.1 / clang 3.0
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int compare(void const* ap, void const* bp) {
> const wchar_t *a=(const wchar_t *)ap, *b=(const wchar_t *)bp;
> while (*a && *b && (*a == *b)) {
> ++a; ++b;
> }
> return *a-*b;
> }
>
> int main() {
> const wchar_t* fruit[] = { L"apricot",L"banana" };
> printf("%d %d\n", L'a'-L'b', compare(fruit[0], fruit[1]));
> qsort(fruit, 2, sizeof(wchar_t*), compare);
> // At this point, in clang, the array has been mis-sorted.
> return 0;
> }
>
>
> $ g++ -Wall -fshort-wchar -o wchar wchar.cpp
> $ ./wchar
> -1 -1
>
> $ clang++ -Wall -fshort-wchar -o wchar wchar.cpp
> wchar.cpp:14:11: warning: conversion specifies type 'int' but the argument has
> type 'wchar_t' [-Wformat]
> printf("%d %d\n", L'a'-L'b', compare(fruit[0], fruit[1]));
> ~^ ~~~~~~~~~
> 1 warning generated.
> $ ./wchar
> 65535 65535
>
> Known? Intended? It hurts our transition from llvm-gcc 4.2 to clang.
>
> - Anders Markussen
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >fshort-wchar (From: Anders Markussen <email@hidden>) |