Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 14, 2004 Even worse Array stuff | ||||
---|---|---|---|---|
| ||||
ORJFieldA *begin = &record.fields[0]; ORJFieldA *end = begin + m_record.fields.length; for(; begin != end; ++begin) { Now it halts on the first line, when the record has no fields. :( Of course, it's obvious in hindsight. But that's hindsight. This really chews. It means that we have to eschew well-founded, widely used and entirely safe C++ coding practices. And before anyone says, use foreach, I can't because I need a pointer to the original structure in situ, rather than a copy. I tried the following earlier, and was not surprised to be receiving the address of a temporary in the Field ctor: foreach(ORJFieldA f; record.fields) { Field field = new Field(f); . . . } What do we think of the rule that array-bounds checking is not used when taking the address of the result? By taking its address, one puts oneself in the wild vistas of the C programmer, and the Spirit of C dictates that we trust the programmer? For the moment, though, I'll just have to clutter up my code with redundant conditionals ... |
July 14, 2004 Re: Even worse Array stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cd2n2n$l45$1@digitaldaemon.com... > ORJFieldA *begin = &record.fields[0]; > ORJFieldA *end = begin + m_record.fields.length; > > for(; begin != end; ++begin) > { > > Now it halts on the first line, when the record has no fields. :( > > Of course, it's obvious in hindsight. But that's hindsight. This really chews. It > means that we have to eschew well-founded, widely used and entirely safe C++ > coding practices. > > And before anyone says, use foreach, I can't because I need a pointer to the > original structure in situ, rather than a copy. I tried the following earlier, > and was not surprised to be receiving the address of a temporary in the Field > ctor: > > foreach(ORJFieldA f; record.fields) > { > Field field = new Field(f); I could be wrong but have you tried: foreach(inout ORJFieldA f; record.fields) { ... i think this will give you the pointer to the original struct. > > . . . > } > > > What do we think of the rule that array-bounds checking is not used when taking > the address of the result? By taking its address, one puts oneself in the wild > vistas of the C programmer, and the Spirit of C dictates that we trust the programmer? > > For the moment, though, I'll just have to clutter up my code with redundant > conditionals ... > > > |
July 14, 2004 Re: Even worse Array stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | "Ivan Senji" <ivan.senji@public.srce.hr> wrote in message news:cd2on8$o63$1@digitaldaemon.com... > "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:cd2n2n$l45$1@digitaldaemon.com... > > ORJFieldA *begin = &record.fields[0]; > > ORJFieldA *end = begin + m_record.fields.length; > > > > for(; begin != end; ++begin) > > { > > > > Now it halts on the first line, when the record has no fields. :( > > > > Of course, it's obvious in hindsight. But that's hindsight. This really > chews. It > > means that we have to eschew well-founded, widely used and entirely safe > C++ > > coding practices. > > > > And before anyone says, use foreach, I can't because I need a pointer to > the > > original structure in situ, rather than a copy. I tried the following > earlier, > > and was not surprised to be receiving the address of a temporary in the > Field > > ctor: > > > > foreach(ORJFieldA f; record.fields) > > { > > Field field = new Field(f); > > I could be wrong but have you tried: > > foreach(inout ORJFieldA f; record.fields) > { > ... > > i think this will give you the pointer to the original struct. I did wonder about that, but by that stage my keyboard had translated my impatience into the now working code. ;/ |
July 14, 2004 Re: Even worse Array stuff | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | In article <cd2n2n$l45$1@digitaldaemon.com>, Matthew says... > > ORJFieldA *begin = &record.fields[0]; > ORJFieldA *end = begin + m_record.fields.length; > > for(; begin != end; ++begin) > { > >Now it halts on the first line, when the record has no fields. :( > >Of course, it's obvious in hindsight. But that's hindsight. This really chews. It means that we have to eschew well-founded, widely used and entirely safe C++ coding practices. .. >What do we think of the rule that array-bounds checking is not used when taking the address of the result? By taking its address, one puts oneself in the wild vistas of the C programmer, and the Spirit of C dictates that we trust the programmer? I had proposed this to Jill a few weeks ago in another thread. I'm not sure I like it because it seems like a hack, but it would certainly work. Another temporary solution would be this: ORJFieldA *begin = m_records.length ? &record.fields[0] : null; ORJFieldA *end = begin + m_record.fields.length; for(; begin != end; ++begin) {} In some respects I do like this approach better because it doesn't result in pointers to uninitialized memory, but the dereferencing issue will still be a problem for folks trying to port C code. Sean |
Copyright © 1999-2021 by the D Language Foundation