Freethought & Rationalism ArchiveThe archives are read only. |
07-10-2003, 10:29 AM | #51 |
Contributor
Join Date: Jul 2001
Location: Deep in the heart of mother-lovin' Texas
Posts: 29,689
|
Originally posted by DigitalChicken
Well of course. That's why I used non-absolute terms. I understood that you knew that; my comment was meant to clarify the point to others. What you will find is that in a great number of cases.. if not a vast majority... those considerations are irrelevant in this day and age. That's true; most of my bad experiences with optimizing compilers came a few years back. Yes there is. The social context in which the code is written. 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. In my comment, I should have said "However, there's often no reason...". And I absolutely agree that the code should be understandable; I just don't think it's necessarily true that code must be made more complex to be more understandable. (I think we agree on this). That may be the case but its not always the case by far. It also depends on what is meant by "complex." For example, seperating a multi-step process into individual code blocks which are commented well may be less "complex" by one definition than another case where the same multi-step process might be written in a single "elegant" code block. I agree, I think. By "simplifying and streamlining" code, I mean removing unnecessary statements and complexity (note that I don't mean squeezing the code down as short as possible), and also clarifying the code (preferably making it easier to understand and more easily modifiable - an important goal which has been implied but not mentioned) by performing such actions as separating a multi-step process into individual code blocks where appropriate. |
07-10-2003, 12:51 PM | #52 |
Veteran Member
Join Date: May 2003
Location: On the road to extinction. . .
Posts: 1,485
|
contiguous
NialScorva, Of course you are right.
When I was professional I used to call it virtual contiguity. |
07-10-2003, 07:53 PM | #53 | ||||||||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Code:
// ArrayContig.cpp #include <iostream> using std::cout; using std::endl; int main() { const int ELEMENTS = 20; int lcv, *ptrInt; int myInts[ELEMENTS]; for (lcv = 0; lcv < ELEMENTS; ++lcv) { myInts[lcv] = lcv; } ptrInt = myInts; for (lcv = 0; lcv < ELEMENTS; ++lcv) { cout << *(ptrInt + lcv); cout << " stored at " << (ptrInt + lcv) << endl; } cout << endl; return 0; } Code:
// ArrayContig.cpp #include <iostream> using std::cout; using std::endl; int main() { const int ROWS = 5; const int COLS = 3; int row, col; int myInts[ROWS][COLS]; for (row = 0; row < ROWS; ++row) { for (col = 0; col < COLS; ++col) myInts[row][col] = row * COLS + col; } for (row = 0; row < ROWS; ++row) { for (col = 0; col < COLS; ++col) { cout << myInts[row][col]; cout << " stored at " << &myInts[row][col] << endl; } } cout << endl; return 0; } Quote:
thisElementsMemoryLocation = memoryAddressOfBaseOfArray + [((thisElementsRowIndex * COLUMNS) + thisElementsColIndex) * sizeof(oneElement)] ***************************************** DNAunion: In addition to the above statements by me, here are some authors in the fields saying the same. Let’s start with the simplest, most straightforward quotes. Quote:
Quote:
Quote:
Quote:
Quote:
DNAunion: And finish off with some that are a little more detailed/involved. Quote:
Quote:
Quote:
|
||||||||||
07-10-2003, 08:05 PM | #54 | ||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
For people who can’t read code that well, I included comments (perhaps you should have read them). Just above the executable statement prior to the code you extracted, we see the following: Quote:
Trashing charge rejected. Quote:
|
||||
07-10-2003, 08:10 PM | #55 | ||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
Quote:
Quote:
Perhaps what you are trying to say is that when writing code I shouldn’t rely upon being able to get by with one-off errors because of the linear and contiguous arrangement of array elements. So? I never claimed I should either. Remember, I FOUND THE OFF-BY-ONE ERROR IN MY OWN CODE. You know, just like millions of other programmers do. The difference here is that I could not modify my code to correct it because these boards have a 120-minute maximum edit window. I mean, come on, it’s not like I intentionally overran the array by 1. Oh yea, let me point out again that the error – which I myself found - was introduced by hastily translating between languages. There is no off-by-one error in the original. |
||||
07-10-2003, 08:13 PM | #56 | ||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
Quote:
Quote:
As frequently explained, a frameshift mutation occurs when a single nucleotide is inserted into a gene, throwing off the reading frame that processes the (triplet) codons. As an analogy, the codons in a gene could be represented as the individual three-letter words in the following, meaningful sentence: |THE BIG RED FOX ATE THE BIG FAT HEN| A frameshift mutation would occur if a single nucleotide (in the analogy, a single letter) were inserted into the sequence, as follows: |XTH EBI GRE DFO XAT ETH EBI GFA THE| N In this example, the single off-by-one shift turns every codon into nonsense (well, for one of the 9 it “merely” changes its meaning completely). A perfectly sensible sentence is transformed into complete “garbage”. My question was aimed at the kind of catastrophe. The error that I myself found in my hastily done translation (note that the error was not present in the actual, original program), basically inserted a single blank element at the beginning of the array (the “gene”) made up of rows each consisting of three elements (triplet “codons”). Why then didn’t a “frameshift mutation” catastrophe occur? The reason, as I stated, is that the two-dimensional array exists as a two-dimensional array only conceptually. In actuality, the elements are store contiguously in linear memory. Going back to the biology analogy that used a sentence, it is more a matter of: |THE BIG RED FOX ATE THE BIG FAT HEN| Becoming |_THE BIG RED FOX ATE THE BIG FAT HE| N In addition to an insertion being made, the processing of the “codons” was also shifted by the same amount, keeping the individual triplets together. Unlike in the former frameshift mutation example, were there was no compensating change, here, “96%” of the original meaning is still retained. It’s not a catastrophe. The only problem that needs to be considered, as I said, is the very last element. (Concerning that last element, originally, I didn’t analyze the possibilities thoroughly enough and someone else spotted something I overlooked. That’s a different issue that I will address in a separate post). Perhaps now you can see why I replied “No” when you offered just “Blind luck?” as the explanation. There’s an actual, logical, theoretical, explainable, honest-to-goodness, real reason that underlies the non-catastrophe: blind luck isn’t it. |
||||
07-10-2003, 08:43 PM | #57 | ||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
You are changing the topic. Each element of my array is a single instance of a single primitive data type and the memory for the entire array is allocated at one time, at compile time, in one chunk of – shall I say it? Yes! – contiguous memory locations. What you in your “they don’t have to be contiguous” section have is the first dimension containing pointers and each of the second set of arrays allocated dynamically, at runtime, using the new operator. That’s like comparing apples and oranges. Quote:
Quote:
And, each of the 10-char arrays that is allocated dynamically with new also consists of 10 contiguous elements. The only “array things” that are not (or at least may not be) stored contiguously in memory are those 10 individual, completely separate 10-char arrays generated at different times, on the fly. Quote:
Code:
#include <iostream> using std::cout; using std::endl; int main() { int i; // purely fictitious person char *record[][2] = { "fname", "Arnold", "mi", "G.", "lname", "Miller", "occupation", "auto mechanic.", "weight", "155 pounds", "height", "5' 2\"", "address", "96-X Frontage Road", "city", "Los Angeles", "state", "ME", "", "" }; for(i = 0; *record[i][0]; i++) { cout << &record[i][0] << " "; cout << record[i][1] << endl; } return 0; } |
||||
07-10-2003, 09:17 PM | #58 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
|
|
07-10-2003, 09:41 PM | #59 | ||||||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
But now you go way off course. Quote:
Of course, neither of our statements deals with the actual facts of the situation. In other words, my "random number" function does NOT access the array so your "point" is imaginary. If anyone is going to offer up negative comments on my code, they at least need to deal with reality, not sensationalism. Quote:
You are as much as stuffing code into my mouth. You aren’t offering hypotheticals when you say “you’re overwriting” – that’s stating a fact (which, of course, isn’t a fact); and when you say “you have no idea where code will be executed” – again, stated as fact when it is not even possible for my code to do. And even we accept them as hypotheticals (which we shouldn't considering how many times you state things as "fact"), they don't even pertain to my code and so are irrelevant. Quote:
Quote:
Quote:
Quote:
|
||||||||
07-11-2003, 04:39 AM | #60 |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
DNAunion: Good, no one else spotted this yet. Here's another example of my finding my own error but not being able to correct it because of the 120-minute window for editing.
The following code does not do what I suggest. I don't have time to rewrite it now (I was just shaving and thought to myself, "Hey, that code doesn't do what I said it did" so rushed here to point it out before anyone else did.). Here's the code that should not be criticized. **************************************** DNAunion: Anyone with an ANSI Standard-compliant C++ compiler can run this code and see that the elements of the first dimension – which stores pointers – are stored contiguously in memory. Code:
#include <iostream> using std::cout; using std::endl; int main() { int i; // purely fictitious person char *record[][2] = { "fname", "Arnold", "mi", "G.", "lname", "Miller", "occupation", "auto mechanic.", "weight", "155 pounds", "height", "5' 2\"", "address", "96-X Frontage Road", "city", "Los Angeles", "state", "ME", "", "" }; for(i = 0; *record[i][0]; i++) { cout << &record[i][0] << " "; cout << record[i][1] << endl; } return 0; } |
Thread Tools | Search this Thread |
|