Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 12, 2002 Mysterious Code | ||||
---|---|---|---|---|
| ||||
the following program works correctly except that it prints out "Error: Access Violation" upon exiting. The program computes the GCD of two numbers, x and y ======================================== import c.stdio; int main(char [][] args) { short x, y; printf("Enter x y:\n"); scanf("%d", &x); scanf("%d", &y); printf("gcd(%d, %d) = %d\n", x, y, gcd(x, y)); return 0; } // Euclidean algorithm for computing GCD short gcd(short x, short y) { short r; if(y > x) { r = x; x = y; y = r; } while(y > 0) { r = x % y; x = y; y = r; } return x; } ======================================== |
March 12, 2002 Re: Mysterious Code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Payam | "Payam" <payamchee@yahoo.com> schrieb im Newsbeitrag news:a6jrgj$4qs$1@digitaldaemon.com... > the following program works correctly except that it prints out "Error: > Access Violation" upon exiting. > The program computes the GCD of two numbers, x and y > ======================================== > import c.stdio; > > int main(char [][] args) > { > short x, y; > printf("Enter x y:\n"); > scanf("%d", &x); > scanf("%d", &y); > printf("gcd(%d, %d) = %d\n", x, y, gcd(x, y)); %d expect long. > return 0; > } > > // Euclidean algorithm for computing GCD > short gcd(short x, short y) gcd returns short. This is the reason, printf should be banished from phobos... no type safety... Imi |
March 12, 2002 Re: Mysterious Code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Immanuel Scholz | ah crap. that a most certainly a dumb mistake to use shorts... erm. ok. but this DOES point to a larger issue: D should have a printf function because it's such a universal function BUT it should have another, "standard" way to output text to the console that IS typesafe. EG: cout << or println & print with a ~ operator that calls the toString operator of a class. e.g.: println("The object foo is currently has value " ~ foo); and the ~ between the string and foo would first call foo.toString() and then concatenate it to the string. I think this would be a powerful addition to the language. "Immanuel Scholz" <digitals-mars@kutzsche.net> wrote in message news:a6l07l$kh7$1@digitaldaemon.com... > > "Payam" <payamchee@yahoo.com> schrieb im Newsbeitrag news:a6jrgj$4qs$1@digitaldaemon.com... > > the following program works correctly except that it prints out "Error: > > Access Violation" upon exiting. > > The program computes the GCD of two numbers, x and y > > ======================================== > > import c.stdio; > > > > int main(char [][] args) > > { > > short x, y; > > printf("Enter x y:\n"); > > scanf("%d", &x); > > scanf("%d", &y); > > printf("gcd(%d, %d) = %d\n", x, y, gcd(x, y)); > > %d expect long. > > > return 0; > > } > > > > // Euclidean algorithm for computing GCD > > short gcd(short x, short y) > > gcd returns short. > > > This is the reason, printf should be banished from phobos... > > no type safety... > > Imi > > > |
March 12, 2002 Re: Mysterious Code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Payam | "Payam" <payamchee@yahoo.com> wrote in message news:a6lf5u$r0m$1@digitaldaemon.com... > ah crap. that a most certainly a dumb mistake to use shorts... erm. ok. > > but this DOES point to a larger issue: > D should have a printf function because it's such a universal function BUT > it should have another, "standard" way to output text to the console that IS > typesafe. EG: cout << or println & print with a ~ operator that calls the > toString operator of a class. e.g.: > println("The object foo is currently has value " ~ foo); This was discussed thousands of times, I believe =) The problem is, language currently doesn't have any mechanism to allow for typesafe and easy-to-use input/output mechanism. One solution could be operator overloading and a pair of special operators, "input" and "output". Other approach would be to use variants and paramarrays, to implement something like BASIC "PRINT" or Pascal "Write". > and the ~ between the string and foo would first call foo.toString() and then concatenate it to the string. I think this would be a powerful addition > to the language. This works for objects; and what if foo is an int, for example? Also, ~ concatenates arrays, NOT strings. So, in your case, I guess foo will be converted to char (if it is possible), and concatenated with the string as such... |
March 12, 2002 Re: toString() as output-stream-operator (was: Mysterious Code) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | Pavel Minayev wrote: > "Payam" <payamchee@yahoo.com> wrote in message news:a6lf5u$r0m$1@digitaldaemon.com... >> ah crap. that a most certainly a dumb mistake to use shorts... erm. ok. >> >> but this DOES point to a larger issue: >> D should have a printf function because it's such a universal >> function BUT it should have another, "standard" way to output text >> to the console that > IS >> typesafe. EG: cout << or println & print with a ~ operator that >> calls the toString operator of a class. e.g.: >> println("The object foo is currently has value " ~ foo); Hm. I like the idea... its pragmatic, its simple and its typesafe... (and its 99% of what you need in a debug-enviroment. Maybe calling it "toDebugString()" to clearify its usage? ;-) built-in exceptions can take advantage of this too, just like in Java. > This was discussed thousands of times, I believe =) > The problem is, language currently doesn't have any mechanism to > allow for typesafe and easy-to-use input/output mechanism. One > solution could be operator overloading and a pair of special > operators, "input" and "output". > > Other approach would be to use variants and paramarrays, to implement something like BASIC "PRINT" or Pascal "Write". I have another idea: If global-function-overloading is allowed (and I assume it is), you can write this magic "toString" by yourself, defining a set of global functions like: char[] toString(int); char[] toString(char[]); char[] toString(double); char[] toString(MyClass); char[] toString(MyClass* c) {return toString(*c);} // as example char[] toString(int); And best: no extension to the language is needed... Imi |
Copyright © 1999-2021 by the D Language Foundation