Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. From where should the index start?

From where should the index start?

Scheduled Pinned Locked Moved The Lounge
questionphpdatabasedesignhelp
114 Posts 31 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BobJanova

    Iteration requires looking at all the items, sometimes you don't want to do that. For example? You've got a list of purchase records in some form of ID-indexed structure, and presented to the user is a list of their titles. When they select one, you want to generate a report of the details of the record they selected (i.e. records[record_id]). You've got a network protocol where the last byte of a message (bytes[bytes.length - 1]) is a checksum. Various string manipulations as currently done with Substring methods (extracting particular fields from a combined text string of some kind). One of the parameters to that method in all conventional languages is an 'index' (actually an offset in most of them).

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #44

    Intriguing discussion. :)

    BobJanova wrote:

    When they select one, you want to generate a report of the details of the record they selected (i.e. records[record_id]).

    Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow; ?

    BobJanova wrote:

    You've got a network protocol where the last byte of a message (bytes[bytes.length - 1]) is a checksum.

    Been there. I submit that the packet can be coded into a memory map (I did this as a union ages ago) to a structure, so I could write packet.Checksum OK, granted the packet was a fixed length. :)

    BobJanova wrote:

    One of the parameters to that method in all conventional languages is an 'index'

    Yeah, which I find annoying. If the fields within the string are fixed length, then they can be put into a structure. If the fields aren't fixed length, then I submit that the language (you did ask originally, if I was defining a language) should allow for specifying, say, delimiters (which a variable length string of sub-fields would almost always need unless there was some encoding, like in compression, that determines the length) so that again, the programmer would be isolated from using indices. Marc

    My Blog

    B 1 Reply Last reply
    0
    • N Nagy Vilmos

      Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #45

      Nagy Vilmos wrote:

      Which is easier to compute?

      Neither. p+(n-1)*s equals (p-s)+n*s where (p-s) is a constant, just like p is, so all it takes is a different origin, the computational effort is exactly the same. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      N 1 Reply Last reply
      0
      • W Wjousts

        nikunjbhatt84 wrote:

        the function can return Zero and thus making the IF condition false.

        No, because using zero as false is dangerous. That's why strongly typed languages (such as C#) have an actual bool class. So you don't accidentally mix up using an int as a bool when it was supposed to be an int.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #46

        Wjousts wrote:

        using zero as false is dangerous

        Correct. Zero should be true. :-D I further agree that programming languages need a definite boolean type. Back when D was still being ironed out, I was one who argued in favor of a boolean type. It didn't happen, I don't use D.

        1 Reply Last reply
        0
        • L Lost User

          gumi_r@msn.com wrote:

          From your post you seem to be implying we should just do without levels of abstraction.

          Not at all. I am implying there is no reason to abstract it. A programmer should understand the bases of what they are programming in (for efficiency). Yes the compiler can handle it. But when it comes to dynamic allocations you end up in the run time. Now it is about perfomance of the app (rather than speed of the build) so not such a good thing. When you count Apples you DO start with 0. You used the word APPLE No? In computers that is where 0 starts, The definition. In Human tounge it is an implicit deffinition. Computers must work in concrete terms. You can't change that. Yes you can abstract it. But at a cost of efficiency.

          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

          G Offline
          G Offline
          gumi_r msn com
          wrote on last edited by
          #47

          Collin Jasnoch wrote:

          When you count Apples you DO start with 0. You used the word APPLE No? In computers that is where 0 starts, The definition. In Human tounge it is an implicit deffinition. Computers must work in concrete terms. You can't change that. Yes you can abstract it. But at a cost of efficiency.

          That is a really wierd way to undersant it. We live in a 1 based indexing world. You said it yourself, we have the luxury of being able to omit the 0th element in our life precisely because it means NOTHING to us. Quite the contrary in the programming world, where the 0th element or the nth element are equally meaningful.

          L 1 Reply Last reply
          0
          • N Nagy Vilmos

            Simply put, it makes sense to have it start at 0. The array variable will be a pointer to a memory address p and each item is an offset from there so, for item n where each element is size s the memory address is p+n*s. If you want 1 based arrays then the element would be found at p+(n-1)*s. Which is easier to compute?


            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #48

            Nagy Vilmos wrote:

            The array variable will be a pointer to a memory address

            That's just a detail of implementation, this is a conceptual discussion.

            1 Reply Last reply
            0
            • N Nagy Vilmos

              BobJanova wrote:

              It would be simple enough for a compiler to calculate the offsets to put into the byte code, anyway.

              How? See my comment above, by reducing the number of steps, you produce more efficient programs.


              Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #49

              Nagy Vilmos wrote:

              by reducing the number of steps, you produce more efficient programs

              Who cares? :confused:

              1 Reply Last reply
              0
              • B BobJanova

                Did you read the rest of the post? As I said there, "Only in the case of indexing a fixed-memory array directly by specifying a calculated index would there be an extra operation". And most of the times you have to do that now are because of a language deficiency. And finally one instruction is so trivial that who cares anyway (on a 4Ghz machine it will cost you 0.25ns, and that's if getting the data your algorithm is working on onto the core cache isn't the limiting factor already) – language design should not be constrained by such trivia.

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #50

                Where we can, we should not be using indexes today. When we do, need to respect how things are stored.


                Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                N 1 Reply Last reply
                0
                • N Nagy Vilmos

                  Do some research Bubba. The point of OO is to provide another level of abstraction from the machine. The amount of code in an OO language based solution is much greater than for a traditional procedural language. However by using classes, the amount of code in one place can be reduced and, more importantly, the code is more tightly connected to the data. The languages are abstracting away the complexity of pointers, but every object is a pointer, we are helped by the fact that a lot of the handling that we had to do by hand in a language like C is now done 'for free' by C# for example. This does not get away from the fact that, at some point, the instruction has to be reduced to finding the memory address containing the item you need. By using zero based arrays, there is one calculation removed, a single machine step. So, this goes back to my original comment. Zero based makes sense because it is better once everything is compiled down. Now which part don't you understand?


                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                  N Offline
                  N Offline
                  Nikunj_Bhatt
                  wrote on last edited by
                  #51

                  I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.

                  N D E 3 Replies Last reply
                  0
                  • L Luc Pattyn

                    Nagy Vilmos wrote:

                    Which is easier to compute?

                    Neither. p+(n-1)*s equals (p-s)+n*s where (p-s) is a constant, just like p is, so all it takes is a different origin, the computational effort is exactly the same. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    N Offline
                    N Offline
                    Nagy Vilmos
                    wrote on last edited by
                    #52

                    I disagree that (p-s) is constant. It does not change much, but as p can be changed, re-assign the array for instance, the pointer cannot be taken as being to a constant place.


                    Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                    L 1 Reply Last reply
                    0
                    • B BobJanova

                      This makes no sense. Counting and indexing are completely different things – even the 0 based computing world accepts that a collection of things A, B and C has 3 items, even if it calls them '0', '1' and '2'. The fact is if a person is counting objects they do use 0, it is just omitted. That doesn't mean anything either. A count of 0 is when there are no things to count. But if there are 6 apples, it doesn't matter what indexing system you use, or how many you count at once, there are always 6. What 'indexing' is about it labelling. Ask anyone to label those apples with numbers and everybody (unless they're being deliberately silly) will call them '1', '2', '3', '4', '5' and '6'. Look at things which are labelled in the real world: bases on a rounders/baseball field, quarters or halves in a sports match, aisle numbers in the supermarket. Even in the computing world this is often true; look at the MRU on Excel or Word, it numbers your recently used files from the first (1) up. Well starting the index at 1 for computers is inefficient As I'm discussing with Nagy above, technically true in a small number of cases (many of which are due to language deficiencies regarding array operations in C family languages) but so small as to be irrelevant as a consideration in the modern world. The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #53

                      You are missing the step that computers have to do. You placed those apples somewhere no? How is the computer suppose to know where that is? We as humans SEE them. A computer needs the reference. That reference is the what??? Thats right, the 0th index. If you switch it you are now forcing the computer to subract 1 off of every access.

                      Apples[] myAppleCollection; //In human terms the apple collection sitting right in front of me

                      By using the "Label" as you put it as the accessor the computer is able to be more efficient and access the first apple because it is the what? Yes, now you are getting it. The 0th index. If I want the second apple I reference it by adding the size of the apple to the collection. You can say this is the 'old' way of doing it, but under the hood it will always do it this way. It has to. If you can not grasp it you should not program.

                      BobJanova wrote:

                      The fundamental question is really: do you want a language for the computer, or one for the people who write it? I would prefer to write one for people and let the compiler or interpreter deal with the translation.

                      This may be the question but your answer is not complete. I want one that allows people to speak easily but at no cost to performance, i.e. the person still needs to understand what is being compiled. You can't get around that. I don't know the details of how a microwave works, but I know not to stick my fork in it. I also know that it will cook my cat (did anyone see that article.. I mean WTF?). If you do not understand the BASE technology of what you are using, you should not use it.

                      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                      B 1 Reply Last reply
                      0
                      • B BobJanova

                        It's pretty difficult to work without lists, and you need to get things out of a list. Unless you count '1', '2' etc as 'meaningful symbols' in which case you still have indexing really. You can help a lot by having enumerations (foreach doesn't require indexing) and array-based functionality (so you can add two vectors without needing to loop around them), but a lot of real world problems would be much more difficult without being able to look things up by index in arrays or collections.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #54

                        BobJanova wrote:

                        foreach doesn't require indexing

                        You don't know what's going on in the background.

                        N B 2 Replies Last reply
                        0
                        • W Wjousts

                          Collin Jasnoch wrote:

                          In human languages we tend to use 0 explicityly.

                          I think you mean implicitly.

                          Collin Jasnoch wrote:

                          Example, I want to order 5 Chicken strips.

                          Damn, now I'm hungry. Is it lunch time yet?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #55

                          You are correct. I will fixy. Thank you:)

                          Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                          1 Reply Last reply
                          0
                          • G gumi_r msn com

                            If it were to be a clean start, I'd definitely have an index start with '1'. As to why, the reason is simple, it's they way we count since we're 2 years old. How does a small kid learn his numbers? I find it hard to imagine some little boy counting 0, 1, 2, ... We start at 1, and do so even when we are grown ups. Zero based indexing is just a legacy of array pointers and offsets. It doesn't help in any useful way in modern languages, it only makes things more confusing, its cumbersome to referrence the last item of an array and it doesn't agree with how we normally count things; when you go to a grocery store and you ask for 10 apples, you expect to get ten apples, them being apple nº1, apple nº2, ... , apple nº10, definitely not apple nº0, ..., apple nº9. So why when you "buy" a 10 item array you get the latter and not the former? WTF?!?

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #56

                            gumi_r@msn.com wrote:

                            it's they way we count

                            How many elephants are on the moon? Did you count them? You may not think about it, but you always count from zero.

                            1 Reply Last reply
                            0
                            • G gumi_r msn com

                              As I posted before you'r reasoning IMHO is not solid. The fact is that array[0] is a meaningful item in a zero based indexing system, while in our everyday life, 0 is the contrary; it can be omitted, as you well say, precisely because it is not meaningful, we live in a 1-index based world like it or not. And I absolutely disagree with the supposedly overhead paid when the compiler has to correctly interpret array[1] as the first item of the array (that is, offset zero in the uderlying pointer) when it spits out machine code, CIL or what have you. Why should there be any performance issue at all? Let the compiler transform everything to zero based indexing that the computer natively understands at compile time (we may have a 1 ms compile time overhead in there somewhere...). Or should we also start writing our programming in assembly code just to make the computers understand us better? Obviously this is of course a hyothetical discussion. We are stuck with zero based indexing and it is after all trivial to understand how it works.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #57

                              Dynamic allocations make it so that performance hit could go out to runtime. Furthermore, you can NOT say 1ms. It competely depends on the application. In some cases you could shatter the performance down to a crawl (arrays of arrays of arrays of arrays ......) For a simple GUI sure, no loss. But thats why MS has VB. You want a simple GUI that does little computation and can't be maintained with out turning into a gobbldy gook of sludge. Fine, program in VB. For the rest of the world and the programers that understand why it is 0 based and are not the slightest bit confused by it, we will continue to make our applications that out perform anything that could be made by this nonsence 1 based index language.

                              Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                              1 Reply Last reply
                              0
                              • M Marc Clifton

                                Intriguing discussion. :)

                                BobJanova wrote:

                                When they select one, you want to generate a report of the details of the record they selected (i.e. records[record_id]).

                                Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow; ?

                                BobJanova wrote:

                                You've got a network protocol where the last byte of a message (bytes[bytes.length - 1]) is a checksum.

                                Been there. I submit that the packet can be coded into a memory map (I did this as a union ages ago) to a structure, so I could write packet.Checksum OK, granted the packet was a fixed length. :)

                                BobJanova wrote:

                                One of the parameters to that method in all conventional languages is an 'index'

                                Yeah, which I find annoying. If the fields within the string are fixed length, then they can be put into a structure. If the fields aren't fixed length, then I submit that the language (you did ask originally, if I was defining a language) should allow for specifying, say, delimiters (which a variable length string of sub-fields would almost always need unless there was some encoding, like in compression, that determines the length) so that again, the programmer would be isolated from using indices. Marc

                                My Blog

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #58

                                Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table? OK, granted the packet was a fixed length That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes. Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be. I'm not convinced that every usage of Substring can be replaced so easily. After all, most modern languages have Split (or explode or something else that does the same job) for delimiter-separated fields, and people still use Substring a lot. Don't get me wrong, I agree that reducing the number of places indices (or offsets, as they are in most languages) are necessary is a good thing. I'm a big fan of enumeration and array operations. But I don't think a language without the capability of using them would be practical, particularly when you consider trying to interface with existing systems.

                                M 1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  BobJanova wrote:

                                  foreach doesn't require indexing

                                  You don't know what's going on in the background.

                                  N Offline
                                  N Offline
                                  Nagy Vilmos
                                  wrote on last edited by
                                  #59

                                  The point here is that Joe Coder doesn't need to manage the index.


                                  Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                  1 Reply Last reply
                                  0
                                  • G gumi_r msn com

                                    Collin Jasnoch wrote:

                                    When you count Apples you DO start with 0. You used the word APPLE No? In computers that is where 0 starts, The definition. In Human tounge it is an implicit deffinition. Computers must work in concrete terms. You can't change that. Yes you can abstract it. But at a cost of efficiency.

                                    That is a really wierd way to undersant it. We live in a 1 based indexing world. You said it yourself, we have the luxury of being able to omit the 0th element in our life precisely because it means NOTHING to us. Quite the contrary in the programming world, where the 0th element or the nth element are equally meaningful.

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #60

                                    That may be a wierd way to understand it, but that is reality. If I tell my child to count the apples and he starts at 1 even though there are no apples he is not grasping mathematics. If there are apples and he starts at 0 he is not grasping efficiency. Computers can not ommit details. They have concrete logic, not fuzzy and everything need be explicityly defined. If that is not there basis then one one of a program with a locked data set could result in a different outcome over time. This would be chaotic and unproductive. And you want this because understanding the 0th index is too hard?

                                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                                    1 Reply Last reply
                                    0
                                    • N Nikunj_Bhatt

                                      I think you are still thinking ONLY about pointers. Suppose I have a listbox and I want to loop through all of its items (elements). I can start a loop like this in VB.NET "For i=0 to ListBox1.Items.Count-1" in Zero based index. Here you can see that there is one more calculation performed (ListBox1.Items.Count-1). If the index is based on One, the loop can be transformed to "For i=1 to ListBox1.Items.Count" which is using one less calculation than Zero based (Actually, this also depends on number of items in the listbox. The loop will need to calculate "Count-1" up to the number of items in the listbox.) In common sense, if there is "nothing" it means Zero and everything starts with One. And programmers are not computers, programming languages can be designed to use starting index as Zero or One or anything most preferred. The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same. More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.

                                      N Offline
                                      N Offline
                                      Nagy Vilmos
                                      wrote on last edited by
                                      #61

                                      Using VB, is the problem there :-D. Seriously in C# it'd be:

                                      for (int i = 0, i < ListBox1.Items.Count, i++) {

                                      ListBox1.Items\[i\].property = stuff;
                                      

                                      }

                                      But also you can use foreach construct:

                                      foreach (Object obj in ListBox1.Items) {
                                      o0bj.property = stuff;
                                      }

                                      nikunjbhatt84 wrote:

                                      The compiled code can address anything using the zero based index but this must not be necessary for a programmer/programming language to use the same.

                                      So if you don't like indexes, don't use them.

                                      nikunjbhatt84 wrote:

                                      More and more programming languages feature are added just to ease programming, otherwise programming can be done directly in Binary.

                                      If you'd actually done any machine code, you'd understand the problem with this statement. Higher level languages abstract mnore and more of the machine away, but saying that means you don't need to understand what is happening is stoopid.


                                      Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                      1 Reply Last reply
                                      0
                                      • N Nagy Vilmos

                                        I disagree that (p-s) is constant. It does not change much, but as p can be changed, re-assign the array for instance, the pointer cannot be taken as being to a constant place.


                                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #62

                                        it is a simple "translation transformation", i.e. a shift of origin: just make the pointer to an array of some type point to the zero-th element of the array, as is done in C-style languages; whether the zero-th element exists or not is immaterial. For a 1-based array of ints at address 0x1000, p would equal 0x0FFC (assuming a 4-byte int). So when allocating an int array in C, say malloc returns 0x1000 and you want that to point to an int array, subtract 4 and store the pointer. The compiler can do that for you; it also can adjust for size differences when you cast one pointer type to another (say casting a char* to an int*). In fact, the malloc case isn't special, just treat void as a type with size zero! :)

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                                        1 Reply Last reply
                                        0
                                        • B BobJanova

                                          Erm: what about DataRow selectedRow = selectionEventArgs.SelectedRow If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table? OK, granted the packet was a fixed length That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes. Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be. I'm not convinced that every usage of Substring can be replaced so easily. After all, most modern languages have Split (or explode or something else that does the same job) for delimiter-separated fields, and people still use Substring a lot. Don't get me wrong, I agree that reducing the number of places indices (or offsets, as they are in most languages) are necessary is a good thing. I'm a big fan of enumeration and array operations. But I don't think a language without the capability of using them would be practical, particularly when you consider trying to interface with existing systems.

                                          M Offline
                                          M Offline
                                          Marc Clifton
                                          wrote on last edited by
                                          #63

                                          BobJanova wrote:

                                          If the UI control is data bound to the same list/data source, perhaps. But what if you want to use something in that row as a lookup in another table?

                                          Well, a better implementation would be:

                                          Person somePerson = selectionEventArgs.Person;
                                          Address personAddress = Person.Address;

                                          One of things I would definitely design into a new language is the ability to utilize foreign key and unique key schema information to reference other tables, and that those tables and their fields could be used directly, at compile time, from the schema definition. No ORM required.

                                          BobJanova wrote:

                                          That's a very big and unreasonable thing to grant ... one would only make that restriction because of language constraints, it's not natural to say a message must be exactly X bytes.

                                          Agreed, but I would provide a mechanism in the language to specify variable length fields with the IOC option to determine lengths at runtime.

                                          BobJanova wrote:

                                          Or how about simply reading strings out of a stream? Typically the stream will encode the length first, and then the content you want is stream[headerlength + 1 ... headerlength + length]. You can't map it onto a fixed structure because you don't know how big the content is going to be.

                                          Well, by definition, a stream is read sequentially, so indexing shouldn't be done on the stream itself. And again, I'd create the language so that variable length fields can be defined in a structure into which the stream can be read. I'm just trying to think outside the box, how to eliminate indexing completely through language supported syntax and features. Marc

                                          My Blog

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups