By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"k-man" <ph*******@hotmail.com> wrote...
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
Jul 22 '05 #2

P: n/a
On 16 Dec 2003 13:08:24 -0800, ph*******@hotmail.com (k-man) wrote:
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.

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

Appreciate a lot,

Thanks,

Philippe

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3

P: n/a
k-man wrote:
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.

--
Pull out a splinter to reply.
Jul 22 '05 #4

P: n/a
On Wed, 17 Dec 2003, Peter Ammon wrote:
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][b][C][...][Z] ;
int brr[m][A][b][C][...][Z] ;

have a common pointer type that could point to either.
--
Shelley Hebert - he****@erc.msstate.edu
Engineering Research Center - Mississippi State University
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.