Array boundaries in C/C++

Started by
8 comments, last by CutterSlade 21 years, 5 months ago
Is there any way to specify negative boundaries for a built-in C array? Say I wanna do Screen[-20 to 20] or something like that, is it possible? Thanks in advance There are 10 kinds of people: those who know binary and those who don''t
--------------------------------------------------------- There are 10 kinds of people: those who know binary and those who don't
Advertisement
char array_thingy[41];char *ptr_thingy = array_thingy[20];for (int i = -20; i <= 20; ++i) {  ptr_thingy[i] = something;} 


i think something like that would work but it's probably not the best idea in the world. why don't you want to go from 0 to 40 (or whatever value, as the case may be)?

scott

edit: square bracket thingies...



[edited by - LizardAl on November 7, 2002 10:12:41 AM]
That´s what I had thought myself. The reason I want to do this is... I don´t remember anymore! I know there was a reason, but can´t remember! Anyway, thanks for the reply

There are 10 kinds of people: those who know binary and those who don''t
--------------------------------------------------------- There are 10 kinds of people: those who know binary and those who don't
quote:Original post by LizardAl
i think something like that would work

No it won't. It will invoke undefined behaviour. The -ve index will be converted to an unsigned value of high magnitude, meaning you will be well off the end of the array. The simple answer to the OP's question is: no it can't be done. The more esoteric answer is: it can be simulated via a level of indirection. I.e. you write a layer of abstraction around the array which accepts -ve indices and correctly translates them into the corresponding unsigned value.

[edited by - SabreMan on November 7, 2002 10:35:31 AM]

  template< class T>class TWeirdArray{T *m_theTs;int m_startBound, m_endBound;public:TWeirdArray( int startBound, int endBound )   : m_startBound( startBound ), m_endBound( endBound ){  m_theTs = new T[ m_endBound - m_startBound + 1 ];  // Note: if you don''t want this to go bananas on you, you  // should add checking to see if endbound > startbound etc.}T& operator[]( int idx ){return m_theTs[ idx - m_startBound ];}};  


Quick hack Not perfect, but it shows the possibilities.
it''s a feature of ruby
quote:Original post by SabreMan
Original post by LizardAl
i think something like that would work

No it won''t. It will invoke undefined behaviour. The -ve index will be converted to an unsigned value of high magnitude, meaning you will be well off the end of the array. The simple answer to the OP''s question is: no it can''t be done. The more esoteric answer is: it can be simulated via a level of indirection. I.e. you write a layer of abstraction around the array which accepts -ve indices and correctly translates them into the corresponding unsigned value.

[edited by - SabreMan on November 7, 2002 10:35:31 AM]

But it would work if the [ ] operator was replaced with a plain old ptr + offset (because the -ve offset points at valid memory)? So you are saying the indexing operator casts to unsigned?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
quote:Original post by Paradigm Shifter
But it would work if the [ ] operator was replaced with a plain old ptr + offset (because the -ve offset points at valid memory)?

How do you figure that one out?
quote:
So you are saying the indexing operator casts to unsigned?

Yes.
if you want, have a function
int translateIndex(int index)array[translateIndex(index)]; 


i wouldn''t bother trying to be clever with indexing, it''ll just put an overhead on people understanding your code (that includes you in a fortnight''s time)
quote:Original post by SabreMan
Original post by Paradigm Shifter
But it would work if the [ ] operator was replaced with a plain old ptr + offset (because the -ve offset points at valid memory)?

How do you figure that one out?

… if we have
T array[ 100 ];

isn''t this valid…
T* midptr = array + 50;
* ( midptr - 50 ) = 0; // same as array [ 0 ] = 0;

(I did miss out the * earlier).

quote:
So you are saying the indexing operator casts to unsigned?

Yes.



"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement