loop vs memset to initialize array.. different results?

J

Jerry Coffin

Jerry Coffin posted:
If I was going to suggest anything, it would be that "initializing"
things to 0/0.0/null pointer is common enough that it might be worth
adding a standard algorithm specifically for that purpose.

SomePOD array[12] = {};

Note that I put "initializing" in quotes. That was meant to suggest a
situation where we want behavior a lot like initializing, but real
initialization won't work, or would be clumsy, inefficient, etc.
 
F

Frederick Gotham

Jerry Coffin posted:
Jerry Coffin posted:
If I was going to suggest anything, it would be that "initializing"
things to 0/0.0/null pointer is common enough that it might be worth
adding a standard algorithm specifically for that purpose.

SomePOD array[12] = {};

Note that I put "initializing" in quotes. That was meant to suggest a
situation where we want behavior a lot like initializing, but real
initialization won't work, or would be clumsy, inefficient, etc.


Forgive me to play devil's advocate... but could you give an example of
where initialisation wouldn't work, or where it would be clumsy or
inefficient?

If anything, we could have a template function as follows:

#include <cstddef>
#include <new>

template<class T,std::size_t len>
void InitArray(T (&arr)[len])
{
T const *const p_over = arr + len;

T *p = arr;

do ::new(p++) T();
while(p != p_over);
}
 
J

Jerry Coffin

[ ... ]
Forgive me to play devil's advocate... but could you give an example of
where initialisation wouldn't work, or where it would be clumsy or
inefficient?

Sure -- an object that's slow to construct, but much faster to zero
out, so (for example) in a loop, you would greatly prefer to zero it
out at each iteration rather than create it anew. For a concrete
example, I (long ago, so I'll admit I don't remember all the details)
wrote some code that dealt with dial-up networking. For better or
worse, the top-level object represented both a dial-up connection and
one or more connections to servers. You could "re-initialize" it, so
it forgot about existing connections to servers, but when/if you
destroyed the object, it hung up the phone. Creating a new object re-
dialed the phone.

As such, re-initializing the object took somewhere on the order of
micro- to milli-seconds, while destroying it and creating a new
object would take something like several seconds to a minute or so.

There are also times that you have something like:

X objects[whatever];

if (something) {
initialize objects
}
use objects;

If we attempt to move the definition of objects to the 'initialize'
section, it'll go out of scope before we can use it.
If anything, we could have a template function as follows:

[ code elided ]

Something like that could probably help some things -- but certainly
not others. I don't see it accomplishing anything in the situation
outlined above, for one example.

I'm not sure how this would be an improvement in any case. It looks
more complex and less versatile than what I was thinking of, which
would be more on the order of a templated version of BSD's bzero.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top