Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2 questions: speed of memset() and pointer to multi-arrays

Reply
Thread Tools

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

 
 
k-man
Guest
Posts: n/a
 
      12-16-2003
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-16-2003
"k-man" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-16-2003
On 16 Dec 2003 13:08:24 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
(E-Mail Removed)
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      12-17-2003
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.
 
Reply With Quote
 
Shelley Hebert
Guest
Posts: n/a
 
      12-18-2003
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 - (E-Mail Removed)
Engineering Research Center - Mississippi State University
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM



Advertisments