home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4994 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.daimi.aau.dk!liborius
  2. From: liborius@daimi.aau.dk (Per Liboriussen)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: 3 bugs in SAS/C v. 6.56
  5. Date: 7 Mar 1996 14:34:18 GMT
  6. Organization: DAIMI, Computer Science Dept. at Aarhus University
  7. Message-ID: <4hms5a$35g@gjallar.daimi.aau.dk>
  8. References: <4gjvvc$a3t@gjallar.daimi.aau.dk> <4h6ts4$ldq@oak43.doc.ic.ac.uk>
  9. NNTP-Posting-Host: silene.daimi.aau.dk
  10. Keywords: SAS/C,bug
  11.  
  12. Thus spake mdf@doc.ic.ac.uk (Martin Frost):
  13.  
  14. >In article <4gjvvc$a3t@gjallar.daimi.aau.dk>, liborius@daimi.aau.dk (Per Liboriussen) writes:
  15. >>union un {
  16. >>    const char *cp;
  17. >>    char *p;
  18. >>};
  19.  
  20. >>const char *foo(union un *unp, int n)
  21. > ^^^^^
  22. >>{
  23. >>    return (n ? unp->p : unp->cp);
  24. >>}
  25.  
  26. >By returning unp->p from foo(), you are returning a char * where const char *
  27. >is specified. The compiler should probably flag this as the error.
  28.  
  29. Whereever did you get that idea from?
  30.  
  31. Assigning the address of an object of type T to a pointer-to-const-T is
  32. always legal and safe. Such an assigment is not a guarantee that the object
  33. pointed to will never change, it is a promise that it will never be changed
  34. *through that pointer*. (Note that this has nothing to do with defining
  35. the object itself as const. That is indeed a guarantee that it will never
  36. change.)
  37.  
  38. Going the other way (assigning a pointer-to-const-T to a pointer-to-T) is
  39. legal as well, but more dangerous, and a good compiler (such as SAS/C or GCC)
  40. will warn about it. But this has nothing to do with the example above.
  41.  
  42.  
  43. >Your program is not entirely correct, so you can't really complain.
  44.  
  45. Yes it is, and yes I can!
  46.  
  47. Besides, since when has being wrong ever prevented anyone from complaining?
  48. If I had a dollar for every time somebody has blamed a bug in their code
  49. on the compiler, I would make Bill Gates look like a poor beggar.
  50. (Obviously, this case is different: I _know_ that the bug is in the compiler
  51. and not my code ;-) ;-).)
  52.  
  53.  
  54. >Try removing the const and see if the problem goes away.
  55.  
  56. The return type of foo() has nothing to do with the bug. In the original
  57. code where the bug was found the function corresponding to foo() didn't
  58. return a pointer. The only reason I wrote it that way was that I needed the
  59. conditional expression (as the comment you've deleted said), and I wanted
  60. to avoid a warning for an unused variable or a statement with no effect or
  61. anything like that.
  62.  
  63.  
  64. --
  65. Per Liboriussen
  66. liborius@daimi.aau.dk
  67.