Because data is the foundation of computing, it is very important to understand how they work. Some of the most notorious hacks through hacker history have occurred because the programmer made an invalid assumption of the data. Take C's scanf() function. What happens when a user inputs a set of characters that is too large for the buffer? Well, the application will crash because there is pointer that could point to an invalid place in memory (that the application doesn't own).
The other day, I walked in and sat down at my computer to see that mostyfriedman has left me a unique challenge that I did get correct, but it certainly demonstrates my point VERY well. In the following application, will the program ever print "Less than 0"?
The answer is yes. It could. Due to the maximum size of ints, this application could print less than. The maximum size for ints is 2,147,483,647. Thus, if I made x = 2,147,483,646 and y = 2, then suddenly, the int wraps around to the lowest possible value -2,147,483,648, which is much less than 0.
See all the fundamental Java types here:
http://download.orac.../datatypes.html
Now, I am indicating that this is Java. However, languages like Python, Haskell, Clojure, and other dynamic languages take care of this issue by extending that range to the limits of the computer memory, but in lower-level languages, you must be far more careful.
So, the moral of this story? Know your data and know your datatypes.
Happy Programming!
The other day, I walked in and sat down at my computer to see that mostyfriedman has left me a unique challenge that I did get correct, but it certainly demonstrates my point VERY well. In the following application, will the program ever print "Less than 0"?
public void print(int x, int y) { if (x < 0 || y < 0) return; int sum = x + y; if (sum < 0) { System.out.println("Less than 0"); } else { System.out.println("Greater than 0"); } }
The answer is yes. It could. Due to the maximum size of ints, this application could print less than. The maximum size for ints is 2,147,483,647. Thus, if I made x = 2,147,483,646 and y = 2, then suddenly, the int wraps around to the lowest possible value -2,147,483,648, which is much less than 0.
See all the fundamental Java types here:
http://download.orac.../datatypes.html
Now, I am indicating that this is Java. However, languages like Python, Haskell, Clojure, and other dynamic languages take care of this issue by extending that range to the limits of the computer memory, but in lower-level languages, you must be far more careful.
So, the moral of this story? Know your data and know your datatypes.
Happy Programming!
6 Comments On This Entry
Page 1 of 1

gregwhitworth
16 April 2011 - 11:25 AM
Wait a second. So I can give respect for a comment but not a blog post. That doesn't make much sense.
Page 1 of 1
← January 2021 →
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Recent Entries
-
-
-
-
-
Know Your Datatypes
on Apr 15 2011 07:38 PM
Search My Blog
Recent Comments
My Blog Links
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)