2 questions: speed of memset() and pointer to multi-arrays

K

k-man

Hi!

I'll try to make it short:
1) is memset() optimised is some way (I suppose at various level
depending of implementations) and if so, to what level?
Is it faster to memset() 4kbytes of memory to 0 or go in a for(;;)
loop for 10 (or 100) iteration to clean up individual integers?

2) This one confuses me even after all those years. I usually avoid
it with classes but here I have no option.
int a[3][400];
int b[3][10];

Is there a way to declare a pointer that could be assigned to both?
meaning
my_p = a...

I believe not, but I'm looking for some good ol' magic, if possible.
:)

Appreciate a lot,

Thanks,

Philippe
 
V

Victor Bazarov

k-man said:
I'll try to make it short:
1) is memset() optimised is some way (I suppose at various level
depending of implementations) and if so, to what level?
Is it faster to memset() 4kbytes of memory to 0 or go in a for(;;)
loop for 10 (or 100) iteration to clean up individual integers?

That is actually unspecified, but I'd say, most likely. If it is
in fact somehow optimised, you are correct supposing that it would
depend on the implementation, then you need to ask the vendor of
a particular implementation.
2) This one confuses me even after all those years. I usually avoid
it with classes but here I have no option.
int a[3][400];
int b[3][10];

Is there a way to declare a pointer that could be assigned to both?
meaning
my_p = a...

I believe not, but I'm looking for some good ol' magic, if possible.
:)

'a' is an array of 3 arrays of 400 ints.
'b' is an array of 3 arrays of 10 ints.

So, what's common here? They are both arrays of 3 arrays of X ints.
Since the smallest (least significant) dimension is different, there
is no pointer to which you can convert both arrays. You could try
to use 'reinterpret_cast' and simply use a pointer to int:

int *my_p = reinterpret_cast<int*>(a);

Now, all elements of 'a' can be accessed using a linear indexing of
'my_p'. There are 1200 elements total, so the index ranges 0-1199.
With arrays of POD, IIRC, it's allowed.

Victor
 
B

Bob Hairgrove

Hi!

I'll try to make it short:
1) is memset() optimised is some way (I suppose at various level
depending of implementations) and if so, to what level?
Is it faster to memset() 4kbytes of memory to 0 or go in a for(;;)
loop for 10 (or 100) iteration to clean up individual integers?

Well, the absolute performance varies depending on the implementation
of your compiler and/or CRT ... but memset is easy to optimize since
you are writing to raw memory and can do it in chunks of more than one
byte in assembler, size depending on your CPU (32 vs 64 bit).

With a for-loop, you have a sequence point for each iteration unless
you have a very clever compiler which can optimize these things away.
If you are dealing with POD types such as arrays of int or char, I
think memset is the best way to go.
2) This one confuses me even after all those years. I usually avoid
it with classes but here I have no option.
int a[3][400];
int b[3][10];

Is there a way to declare a pointer that could be assigned to both?
meaning
my_p = a...

Sure ... arrays are guaranteed to have contiguous storage (section
8.3.4 in the C++ standard), and the address of an array is equivalent
to the address of its first member.
 
P

Peter Ammon

k-man said:
Hi!

I'll try to make it short:
1) is memset() optimised is some way (I suppose at various level
depending of implementations) and if so, to what level?

I've seen the naive for-loop implementation, sophisticated copy-on-write
implementations that work in constant time, and lots in between.
Is it faster to memset() 4kbytes of memory to 0 or go in a for(;;)
loop for 10 (or 100) iteration to clean up individual integers?

Try it and see. Or better, do whatever's simpler until your program
seems too slow and profiling shows that part of your code to be the
bottleneck.
2) This one confuses me even after all those years. I usually avoid
it with classes but here I have no option.
int a[3][400];
int b[3][10];

Is there a way to declare a pointer that could be assigned to both?
meaning
my_p = a...

No. Note, however, that by exchanging the sizes you can.

int a[400][3];
int b[10][3];
int (*p)[3];
p=a;
p=b;
I believe not, but I'm looking for some good ol' magic, if possible.
:)

Appreciate a lot,

Thanks,

Philippe

Hope I've been useful.
 
S

Shelley Hebert

No. Note, however, that by exchanging the sizes you can.

int a[400][3];
int b[10][3];
int (*p)[3];

In the general case of an N-dimensional array, all but the left-most size
must match, otherwise the compiler has no way of knowing how much to
"skip" to get to the next, err, row, layer, cube, hypercube,... .

int arr[n][A][C][...][Z] ;
int brr[m][A][C][...][Z] ;

have a common pointer type that could point to either.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top