www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Even worse Array stuff

reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
  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 ...
Jul 14 2004
next sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"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 ...
Jul 14 2004
parent "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
"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. ;/
Jul 14 2004
prev sibling parent Sean Kelly <sean f4.ca> writes:
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
Jul 14 2004