Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 14, 2004 Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
ORJRecordA *begin = &m_database.records[0]; ORJRecordA *end = &m_database.records[m_database.records.length]; for(; begin != end; ++begin) { . . . The process halts with an ArrayBoundsError on the second line above. The workaround for this is ORJRecordA *begin = &m_database.records[0]; ORJRecordA *end = begin + m_database.records.length; but it's hardly what I'd call a good solution. I suppose D doesn't have all the horrible pitfalls of C & C++ which mandate that &ar[N] is the only portable (between arrays and UDTs, and between compilers) syntax. But I still think this chews. I'd rather not have the checking. |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: > ORJRecordA *begin = &m_database.records[0]; > ORJRecordA *end = &m_database.records[m_database.records.length]; > > for(; begin != end; ++begin) > { > . . . > > The process halts with an ArrayBoundsError on the second line above. Well, the last element should be: m_database.records[m_database.records.length - 1]; Lars Ivar Igesund |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | In article <cd2mgr$je4$2@digitaldaemon.com>, Matthew says... > >The workaround for this is > > ORJRecordA *begin = &m_database.records[0]; And even that won't work if m_database.records.length == 0. Farmer gave us the solution to this one a while back. It is: # ORJRecordA *begin = cast (ORJRecordA *) m_database.records; Arcane Jill |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Ivar Igesund | "Lars Ivar Igesund" <larsivar@igesund.net> wrote in message news:cd2op0$n6m$1@digitaldaemon.com... > Matthew wrote: > > ORJRecordA *begin = &m_database.records[0]; > > ORJRecordA *end = &m_database.records[m_database.records.length]; > > > > for(; begin != end; ++begin) > > { > > . . . > > > > The process halts with an ArrayBoundsError on the second line above. > > Well, the last element should be: > > m_database.records[m_database.records.length - 1]; Not correct. Give it another think. :) |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cd2mgr$je4$2@digitaldaemon.com... > ORJRecordA *begin = &m_database.records[0]; > ORJRecordA *end = &m_database.records[m_database.records.length]; > > for(; begin != end; ++begin) > { > . . . > > The process halts with an ArrayBoundsError on the second line above. > > The workaround for this is > > ORJRecordA *begin = &m_database.records[0]; > ORJRecordA *end = begin + m_database.records.length; > > but it's hardly what I'd call a good solution. I suppose D doesn't have all the > horrible pitfalls of C & C++ which mandate that &ar[N] is the only portable > (between arrays and UDTs, and between compilers) syntax. But I still think this > chews. I'd rather not have the checking. An even better workaround: foreach (OBJRecordA o; m_database.records) { ... } |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <newshound@digitalmars.com> wrote in message news:cd2v21$14n3$1@digitaldaemon.com... > > "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cd2mgr$je4$2@digitaldaemon.com... > > ORJRecordA *begin = &m_database.records[0]; > > ORJRecordA *end = &m_database.records[m_database.records.length]; > > > > for(; begin != end; ++begin) > > { > > . . . > > > > The process halts with an ArrayBoundsError on the second line above. > > > > The workaround for this is > > > > ORJRecordA *begin = &m_database.records[0]; > > ORJRecordA *end = begin + m_database.records.length; > > > > but it's hardly what I'd call a good solution. I suppose D doesn't have > all the > > horrible pitfalls of C & C++ which mandate that &ar[N] is the only > portable > > (between arrays and UDTs, and between compilers) syntax. But I still think > this > > chews. I'd rather not have the checking. > > An even better workaround: > foreach (OBJRecordA o; m_database.records) > { > ... > } Doesn't work, since it gives me copies of the record structures, and I need their addresses. |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: > "Walter" <newshound@digitalmars.com> wrote in message > news:cd2v21$14n3$1@digitaldaemon.com... <snip> >> An even better workaround: >> foreach (OBJRecordA o; m_database.records) >> { >> ... >> } > > > Doesn't work, since it gives me copies of the record structures, and I need their > addresses. foreach (inout OBJRecordA o; m_database.records) { ... } surely? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote: > "Walter" <newshound@digitalmars.com> wrote in message > news:cd2v21$14n3$1@digitaldaemon.com... > >>"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message >>news:cd2mgr$je4$2@digitaldaemon.com... >> >>> ORJRecordA *begin = &m_database.records[0]; >>> ORJRecordA *end = &m_database.records[m_database.records.length]; >>> >>> for(; begin != end; ++begin) >>> { >>> . . . >>> >>>The process halts with an ArrayBoundsError on the second line above. >>> >>>The workaround for this is >>> >>> ORJRecordA *begin = &m_database.records[0]; >>> ORJRecordA *end = begin + m_database.records.length; >>> >>>but it's hardly what I'd call a good solution. I suppose D doesn't have >> >>all the >> >>>horrible pitfalls of C & C++ which mandate that &ar[N] is the only >> >>portable >> >>>(between arrays and UDTs, and between compilers) syntax. But I still think >> >>this >> >>>chews. I'd rather not have the checking. >> >>An even better workaround: >> foreach (OBJRecordA o; m_database.records) >> { >> ... >> } > > > Doesn't work, since it gives me copies of the record structures, and I need their > addresses. > My experience has been that I can always get an address(index) of an array by specifying an int counter in the foreach loop. typedef char[] ORJRecordA; struct database { ORJRecordA[] records; } void main () { database m_database; m_database.records ~= cast(ORJRecordA)"Contents @ Address 0"; m_database.records ~= cast(ORJRecordA)"Contents @ Address 1"; foreach(int address, ORJRecordA rec; m_database.records) printf("%2d: %.*s"\n,address,cast(char[])rec); } Ciao, Andrew |
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Matthew wrote:
> "Lars Ivar Igesund" <larsivar@igesund.net> wrote in message
> news:cd2op0$n6m$1@digitaldaemon.com...
>
>>Matthew wrote:
>>
>>> ORJRecordA *begin = &m_database.records[0];
>>> ORJRecordA *end = &m_database.records[m_database.records.length];
>>>
>>> for(; begin != end; ++begin)
>>> {
>>> . . .
>>>
>>>The process halts with an ArrayBoundsError on the second line above.
>>
>>Well, the last element should be:
>>
>> m_database.records[m_database.records.length - 1];
>
>
> Not correct. Give it another think. :)
>
Maybe I misunderstand something, but you get an ArrayBoundsError if you do
arr[arr.length];
Lars Ivar Igesund
|
July 14, 2004 Re: Array bounds checking causes algorithmic nasties | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars Ivar Igesund | Lars, you are correct in your assumption that this will cause an error. You're wrong in your assumption that this is the ideal behaviour when you want to implement STL style iterators... As Matthew said. Think again... :) |
Copyright © 1999-2021 by the D Language Foundation