Freethought & Rationalism ArchiveThe archives are read only. |
07-10-2003, 05:17 AM | #41 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
So far I've successfully resisted all attempts by management to move me "up" into their ranks. If they think my salary is unjustifiable as a "mere programmer", they can try to do without me. I can easily find another job in the field, but they can't easily find someone who can fill my shoes. In at least one sense, the way things stand, I have the upper hand on management, even though I'm just a "lowly programmer". |
|
07-10-2003, 05:44 AM | #42 |
Veteran Member
Join Date: May 2003
Location: On the road to extinction. . .
Posts: 1,485
|
trashing warning
I must issue a trashing warning :
nFoundOne = 0; while (nFoundOne == 0) { nIndex = (int) GetRandomNumber(0, nLetteredTiles - 1); ... } The set of solutions which use randomization to access every element in a set once, is prone to what is termed trashing. This means if one randomly picks numbers from 1..n, it is not true that n tries will eventually visit every every element. Mostly you end up with 2n or 3n computations to visit every element. The elegant solution to this is randomly choosing 85% to 90% of the indicies, then sequentially selecting every non-selected item. The complexity of this algorithm is fixed at 1.8n, and as such the overhead is fixed and known. A true randomiser may never pick one entry and this may entail your program may run into the infinity clause, even though you think it may not. Happy Computing |
07-10-2003, 05:53 AM | #43 |
Veteran Member
Join Date: May 2003
Location: On the road to extinction. . .
Posts: 1,485
|
contiguous
DNAunion, nice of you to have written a doubly linked list. The last example of doubly linked lists I saw was in the VMS kernel.
The claim elements of an array are stored linearly and contiguously is not necessarily true. The appearance of contiguity is achieved through memory mapping. |
07-10-2003, 06:00 AM | #44 |
Veteran Member
Join Date: Sep 2001
Location: Los Angeles Area
Posts: 1,372
|
So is this thread about computer programming or computer science? I see a smattering of both. Has anyone created their own minimal language and compiler? I'd love to try this one day.
|
07-10-2003, 07:04 AM | #45 |
Contributor
Join Date: Jul 2001
Location: Deep in the heart of mother-lovin' Texas
Posts: 29,689
|
Mageth: Blind luck?
DNAunion: No, because of the way memory is allocated for array variables.... No, it's blind luck. Any time you write to an address outside the allocated bounds of a variable/data structure, you are risking serious problems. The fact that your program worked is, from the programmer's perspective, blind luck. But the two subscripts are misleading…there is no real element [5, 1] in the array. That is, to access element [5, 1] the computer does not go down 6 rows and then over to the right 1. No, elements of an array are stored linearly and contiguously. You can demonstrate this for yourself by using pointer arithmetic instead of subscripts. Umm, I learned this, what, 22 years ago in my intro programming classes. And since then, I've seen many strange problems in code caused by overstepping the bounds/misaddressing of an array, even by only one element. And like sophie indicated, a programmer should never assume how the data's being stored. So the array should be the last variable that memory is reserved for and overshooting the upper bound by one element does not cause a problem. Pulling the trigger during Russian Roulette when the hammer happens to be over an empty chamber doesn't cause a problem, either. |
07-10-2003, 08:27 AM | #46 | |
Veteran Member
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
|
Re: contiguous
Quote:
A multidimensional array is actually an array of arrays. char foo[10][10]; is an array of 10 blocks of 10 contiguous bites. *Usually* these are stored contiguously, so that foo[0][9] and foo[1][0] are adjacent, but there is no such requirement in the language spec. It's perfectly valid for foo to point to an array of pointers to arrays. In fact, you can see this in the similar notation: Code:
char* x[10]; for(int i=0;i<10;i++) x[i]=new char[10]; The key difference between the two is that typeof(foo[1]) is a const char* and typeof(x[1]) is char*. This lets foo[1] be optimized into a constant offset from a memory location rather the an addressed stored in memory that must be loaded. Strictly speaking, the only assumption you can make is that the last subscript will be contiguous (eg pointer math will work within it's bounds) or it's possible that your program will break if you change compilers. |
|
07-10-2003, 08:39 AM | #47 | |
Veteran Member
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
|
Quote:
If your array is on the heap, you have similar problems. Most heaps allocate in block sizes, so you very possibly have run-off room at the end. That doesn't mean you should use it. If your array is the same as the heaps block size, then running past the end will likely overwrite the metadata for the next heap block. Once your heap is corrupted, all bets are off. Your program will run fine 9 times out of 10, but then crash hard. It all depends upon what's in memory when when the program. This is *not* easily debugged. |
|
07-10-2003, 08:59 AM | #48 | |
Senior Member
Join Date: Aug 2000
Location: San Diego, CA, USA
Posts: 913
|
Quote:
|
|
07-10-2003, 09:43 AM | #49 | ||||
Banned
Join Date: Jul 2002
Location: U.S.
Posts: 4,171
|
Quote:
Quote:
Quote:
I may program something that I will never touch again and some unknown person may pick it up. In that case, its of prime importance that the code be understandable. Quote:
DC |
||||
07-10-2003, 10:26 AM | #50 |
Senior Member
Join Date: Aug 2000
Location: San Diego, CA, USA
Posts: 913
|
If you want to see spaghetti code written by professionals, take a look at some of the examples at The International Obfuscated C Code Competition
|
Thread Tools | Search this Thread |
|