Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 26 May 2012 21:43:35 -0500 Message-ID: <4FC194D5.24CA@mindspring.com> Date: Sat, 26 May 2012 22:43:33 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Open letter to Ian Collins References: Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Lines: 104 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.223.250 X-Trace: sv3-qNsuLk6z6Vs1eWun7cLo155IWUu/r68lAceuo2T6A/S2PQdnmW+/ch8/zI4GUClbmdEukj1hLSv/Vh+!H1kBIrk9OUDYy3gwPf8l/JZ/uFuBiY9o6XuYXyTF37bKTAYilZ9c8CA/2jyQvIZ0C+xdGFlhHObj!hkW3w0KToa4= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3973 Xref: csiph.com comp.lang.c:21392 Malcolm McLean wrote: > > בתאריך יום שבת, 26 במאי 2012 23:36:29 UTC+1, מאת Ike Naar: > > > > This seems dangerous. iList.Add expects a pointer to an > > int for the second argument, and it receives a pointer to > > a size_t. What if int and size_t have different sizes? > > > Yes, size_t is a menace. I've been saying that for a while. > The problem is that programmers are unwilling to use it consistently. > If you want an integer, you type int. In this case, I don't think that (size_t) was the best choice for either (i) or (d). (i) was counting nodes in a list and the standard definition of what (size_t) is supposed to be, isn't related to how many nodes can be in a list. I think I would have used the largest unsigned integer type for (i). (d) was assigned the return value of rand(), so (int) is the type that would first occur to me to use for (d). Unsigned integer types would have been the first thing to occur to me to use for the checksums. And even though nobody wants to see it, this is how I would have written a program like that in C: http://www.mindspring.com/~pfilandr/C/lists_and_files/list_lib.h http://www.mindspring.com/~pfilandr/C/lists_and_files/list_lib.c http://www.mindspring.com/~pfilandr/C/lists_and_files/list_type.h /* BEGIN new.c */ #include "list_lib.h" #include #include #define N 1000000 #define str(s) # s #define xstr(s) str(s) int int_comp(const void *a, const void *b); int main(void) { int d, *d_ptr; long unsigned i; unsigned old_checksum = 0; unsigned new_checksum = 0; list_type *head = NULL; list_type *tail = NULL; puts("/* BEGIN output new.c */\n\nN is " xstr(N) "\n"); for (i = 0; i != N; ++i) { d = rand(); old_checksum += d; tail = list_append(&head, tail, &d, sizeof d); if (tail == NULL) { puts("tail == NULL"); printf("list only contains %lu nodes,\n\n", i); break; } } tail = head = list_sort(head, int_comp); while (tail != NULL) { d_ptr = tail -> data; d = *d_ptr; new_checksum += d; tail = tail -> next; if (tail != NULL && int_comp(d_ptr, tail -> data) > 0) { puts("list is out of order\n"); break; } } list_free(head, free); head = tail = NULL; printf("old_checksum is %lu.\n", old_checksum); printf("new_checksum is %lu.\n", new_checksum); puts("\n/* END output new.c */"); return 0; } int int_comp(const void *a, const void *b) { const int *const aa = a; const int *const bb = b; return *bb > *aa ? -1 : *bb != *aa; } /* END new.c */ -- pete