Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: Thomas David Rivers Newsgroups: comp.lang.c++ Subject: Re: Problem with array objects Date: Wed, 25 May 2011 20:02:27 -0400 Organization: Aioe.org NNTP Server Lines: 289 Message-ID: <4DDD9893.9060502@dignus.com> References: <9PDzp.2011$Am5.1296@newsfe05.ams2> <846dndOds94DPEbQnZ2dnUVZ8ridnZ2d@giganews.com> <5cadnQfIzuppjUHQnZ2dnUVZ8kidnZ2d@giganews.com> <805Dp.26220$7H.26163@newsfe08.ams2> <4DDD0AE0.9080001@dignus.com> <4DDD34A8.6030005@dignus.com> <4VdDp.17340$h35.4417@newsfe19.ams2> NNTP-Posting-Host: 5vmandCOS2XnoG0+Ic8whg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.13) Gecko/20061027 X-Accept-Language: en-us, en X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:5567 Paul wrote: > > "Thomas David Rivers" wrote in message > news:4DDD34A8.6030005@dignus.com... > >> Paul wrote: >> >>> >>> An array object is not the same as a single variable. >> >> >> >> Well - this is likely a mistake.. but.... >> >> >> >> So - to begin with some basic principles; let's remove the complications >> of templates, function overloading, etc... and let's just talk >> about some really simple/straight-forward examples and how things >> actually work "under the covers" for those simple situations. >> > So basically you want to consider an array as a simple pointer. No - not at all - I provided my suggested definition of the term 'array'. Basically, in that, an 'array' is a contiguous set of elements with a defined length, where each element can be accessed individually. > >>> >>> An array is a sequence of objects that cannot be addressed with a >>> single instruction, unless its size one or soemthing silly. >>> An object of array type is an object that strores the pointer to the >>> array , for example arr in the following: >>> >>> int arr[5]; >> >> >> >> Hmm... those sound like nice definitions... but, I think I would prefer >> some simpler ones. I'm terribly confused by this "object of array >> type" >> you have there, and why you would need one... > > > Hold on lets clear this confusion up. > In asm an array is basically a pointer, in C++ its an object of array > type (which I see as a non modifiable pointer with added typeinfo). Actually, in 'asm' - an array is a contiguous set of elements of the same type. The concept does not change because the implementation language changes. The array remains a contiguous set of elements of the same type. Certainly, in 'asm', it might be convenient to have a pointer to that allocated memory, but the array is still the entire set of elements. Nothing more, nothing less. That is, a pointer is a pointer, an array is an array. In C/C++, there is not this 'object of array type' you mention, there is the idea of an 'array'. An array is simply a contiguous set of elements of the same type, each element can be accessed. When the array is allocated, all of the memory required for the entire set is allocated. When the array is deallocated, the entire array "goes away". Let's just back up and start with that simple idea; then we can proceed from there and build up the ideas of accessing the array and/or accessing elements of the array. So - to reiterate... An array is simply a contigous set of like-typed elements. How does that sound for a start? I've added some more comments below - just to clarify a little. However, we really need to agree on this definition... then, I think we can build up from there and hopefully clarify this a little. > > So according to C++ with the deifnition: > int arr[5]; > > The object arr is not a pointer , it is a non modifiable object of > array type. > If you want to forget about all the typeinfo etc and simply regard > this as a pointer for the sake of the dscussion I can go with that. > No, I don't think so.. the object 'arr' is not a pointer - it is a contiguous set of 5 elements of type 'int'. Note that I keep using the word 'element'... arrays have elements (just like structures have members, for instance.) This will be important later. > > >> >> So - let me offer these... >> >> Let's say that an array is a contiguous sequence of elements. > > > Aha now you are shifting the focus, to the actual objects of the > array. Lets be clear about these two definintions of array. > We have an array of 5 ints like so: > [int][int][int][int][int] > > To access this array we need to have a pointer so we have: > [ptr_arr]...................[int][int][int][int][int] Actually - if you will follow along with me and we can agree on some really basic ideas, then it will just so happen that multi-dimensional arrays "just work", without these other pointers you are suggestion. Because of that, I'd like to defer the discussion of multi-dimensional arrays until we can get to the basic understanding of C/C++ simple arrays. If you don't mind... > > The term array can be applied to a contiguous sequence of integer > objects or to the identifier(pointer) for these objects. Actually - that's not what I provided in my definition. The term 'array' simply means the entire set of the contiguous elements; nothing more, nothing less. It's important to understand that there is no separate pointer to the objects, there is simply the entire set of objects. There is a context in which a reference to the entire set of objects is converted to a pointer to the first element... but that is different. It is not a separate object that contains the address. That might be a good idea, and I mention at least one other language that does take that approach below.. but, it's simply not how C/C++ is defined. > Going back to the old arr , arr is a pointer object that points to the > sequence of integer objects. This is a different object from any of > the five integer objects > 1) An array is a pointer such as arr. > 2) An array is a sequence of objects. > Note: Assuming we're agreeing to use this "pointer" terminology > instead of the C++ "object of array type" terminology. I don't think so... an array is not a pointer. An array is a contiguous set of elements of the same type. In some situations, references to this set are converted to the address of the first element of the set. That really is all there is to it. > > >> Arrays may be indexed, which results in a single element. (Let's not >> get >> into multi-dimension issues for the moment; just one dimension.) An >> array >> defines the number of elements it comprises, starting at element #0 and >> continuing to n-1 elements. >> >> Does that sound good? >> >> If so - let's continue with this example: >> >> int arr[5]; >> >> If we agree on the basic definition, then this would indicate that, >> somewhere at runtime, there is a contiguous set of 5 'int' elements. >> The name of that contigous set of elements is 'arr'. >> >> Does that sound good? > > > No because arr is the name of a pointer object that points to the > array of 5 ints. Actually - no, 'arr' is the name of the start of the contiguous set of 5 int elements. That's all 'arr' is - nothing more, nothing less. However, in some situations, the reference to the set of elements is converted to the address of the first element. 'arr' is not a pointer to another location. It is the actual location of the 5 ints. > > You could consider it the name of the array of integer objects , ofc, > but I dont think it is suitable to ignore that pointer object named > arr as this seems to be what the discussion is all about. I was trying to get to the idea that there is no "pointer object named arr". There actually is not one of those. 'arr' is the name of the contiguous set of 5 int elements. It is not a pointer to the 5 int elements; it actually is the 5 int elements. When you use the name 'arr' in expression contexts where the value is required, the 'decay' as you put it occurs, and the result of that expression is a pointer to the first element of the array. This is the subtle point... Thus, in the snippet: int *ip; int arr[5]; ip = arr; what happens semantically? In this statement above, the name 'arr' is used in an expression that requires its value. In that case, the semantics indicate that the result of the expression would be the address of the first element. Thus; these two statements are completely, totally, and utterly (in the context of the declarations above) identical: ip = arr; ip = (&arr[0]); They are identical in every aspect; because of the rule that an array name used in an expression context is treated as the address of the first element. This is the definition of C/C++. This is really how it works. There is not other 'magic' behind it.. no other objects-pointing-to-objects or anything else. That's all there is to this... nothing more... There does not exist a separate object that points to the 5 bytes... Unfortunately - this is not a matter to be debated. This just happens to be the way C and C++ are defined. Other languages (e.g. PL/I) use approaches similar to what you are considering.. it's called a "dope vector" and can be very helpful.. but, that's not how C and C++ are defined. Again, there is not a "pointer object" that points to the array; there is only the array. And, an array is not a pointer... however, in some situations, the name of the array does not indicate the array in-total, but refers to the address of the first element. I'm afraid that's not really open to discussion - it simply is true by definition. It really, actually, truly is the way it works and the way its defined. Now - I realize others have tried to explain this - I was hoping to add clarification to that. Starting with some really basic definitions and "working up" from there. But - we have to agree that an array is the contiguous set of elements of the same type. That there is nothing more to it. If it is something about which you disagree, then I'll have give up on the tutorial. Which would be a shame, because it's really kinda cool how it all works out. - Dave Rivers - -- rivers@dignus.com Work: (919) 676-0847 Get your mainframe programming tools at http://www.dignus.com