W
Wei Zhou
In my computer that's correct!Carson said:Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Carson
In my computer that's correct!Carson said:Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Carson
Carson said:Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
"Carson said:Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Carson said:Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Carson
Carson said:Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Jack Klein said:d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));
Tom said:Carson wrote:
Hi ,
Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
Um try something along these lines
double myarray[SIZE], tmp;
tmp = 0.0;
memcpy(myarray, &tmp, sizeof(double));
memcpy(myarray+1, &tmp, sizeof(double));
memcpy(myarray+2, myarray, sizeof(double)*2);
memcpy(myarray+4, myarray, sizeof(double)*4);
memcpy(myarray+8, myarray, sizeof(double)*8);
memcpy(myarray+16, myarray, sizeof(double)*16);
... until you cover SIZE
Tom
Jack Klein said:d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));
IMHO it won't work, unless I'm missing something very obvious.
(Values d[1], d[2]... are undetermined, and are copied
into positions d+2, d+3...)
Jack Klein said:d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));
IMHO it won't work, unless I'm missing something very obvious.
(Values d[1], d[2]... are undetermined, and are copied
into positions d+2, d+3...)
But they are not undetermined. memmove(), as opposed to memcpy(), is
specified to copy properly when the source and destination areas
overlap, as in this case. If they overlap, it is guaranteed to read
and copy the old value of each byte before overwriting the byte with
its new value.
So the first byte of d [1] will have the value of the first byte of d
[0] before it is copied into the first byte of d [2], and so on. This
is perfectly legal and has a completely defined result.
Keith Thompson said:Carson said:Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
If you called memset with the right arguments, I would expect you to
see 0.0 in all entries on almost all systems. (Did you remember to
multiply the length by sizeof(double)?) But the standard doesn't
guarantee that 0.0 is represented as all-bits-zero, so using memset
isn't strictly portable.
Probably the best solution is the simplest:
for (i = 0; i < LEN; i ++) {
arr = 0.0;
}
See whether that's fast enough before you try something more
complicated.
Dag said:Keith Thompson said:Carson said:Is there a very efficient way to set a double array to 0 ?
(I have tried memset, but the result doesn't look correct.)
If you called memset with the right arguments, I would expect you to
see 0.0 in all entries on almost all systems. (Did you remember to
multiply the length by sizeof(double)?) But the standard doesn't
guarantee that 0.0 is represented as all-bits-zero, so using memset
isn't strictly portable.
Probably the best solution is the simplest:
for (i = 0; i < LEN; i ++) {
arr = 0.0;
}
See whether that's fast enough before you try something more
complicated.
I agree. I just tested this code with VC7 and the loop was reduced to a
'REP STOSD' instruction, which clearly is faster than calling memset. In
debug mode the generated code was terrible, but that's OK.
Jack Klein said:Jack Klein said:d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));
IMHO it won't work, unless I'm missing something very obvious.
(Values d[1], d[2]... are undetermined, and are copied
into positions d+2, d+3...)
But they are not undetermined. memmove(), as opposed to memcpy(), is
specified to copy properly when the source and destination areas
overlap, as in this case. If they overlap, it is guaranteed to read
and copy the old value of each byte before overwriting the byte with
its new value.
Jack Klein said:Jack Klein said:d [0] = 0; /* or 0.0, if you prefer */
memmove(d + 1, d, sizeof(double) * (SIZE - 1));
IMHO it won't work, unless I'm missing something very obvious.
(Values d[1], d[2]... are undetermined, and are copied
into positions d+2, d+3...)
But they are not undetermined. memmove(), as opposed to memcpy(), is
specified to copy properly when the source and destination areas
overlap, as in this case. If they overlap, it is guaranteed to read
and copy the old value of each byte before overwriting the byte with
its new value.
(In real life, memmove() is likely to check whether the source and
destination overlap, choosing an order that avoids problems. There's
no portable way to do that without invoking undefined behavior, but
the memmove() implementation is free to use non-portable tricks.)
In any case, you are better off knowing your own data, and doing the
appropriate copying.
Has anyone done speed checks for mallocing a new chunk and copying the
correct data versus a memmove?
Who even does memmove? It sounds like sloppy programming to me. I mean does
the memory overlap or not?! If you don't know, why are you copying into it?
In any case, you are better off knowing your own data, and doing the
appropriate copying.
Has anyone done speed checks for mallocing a new chunk and copying the
correct data versus a memmove?
Who even does memmove? It sounds like sloppy programming to me. I mean
does the memory overlap or not?! If you don't know, why are you
copying into it?
Michael Mair said:it?
Hmmm, memmove() might be more efficient than everything you can come up
with. A naive way of implementing it is to calculate the overlap and
memcopy() just the highest non-critical number of bytes -- which might
lead to memcopy()ing all in one go. However, the system you are working
with might be in favor of a different way of doing it, say getting the
bytes to be copied up or down to a multiple of a certain number or even
more weird things, incorporating special tricks memcopy() does not
employ because they cost a couple of cycles too much but are exactly
the right thing here, etc.
So, memmove() probably is faster than repeated calls to memcopy().
Why someone might want to use it? Imagine having an array of structures.
You are quite happy with that array, need it to be an array because
a linked list or even an index array for a linked list is too slow for
your purposes. Then comes up one small nasty case where you have to
insert something into your array instead of appending it. You know there
will be only this one case, so you decide to stay with the array data
structure.
I, for one, would just memmove() the back end of the used data structure
to make room for my new entry -- provided the array is big enough, of
course. And I would not die regretting that as a programming sin.
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.