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

Discussion in 'C++' started by k-man, Dec 16, 2003.

  1. k-man

    k-man Guest

    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
    k-man, Dec 16, 2003
    #1
    1. Advertising

  2. "k-man" <> 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
    Victor Bazarov, Dec 16, 2003
    #2
    1. Advertising

  3. On 16 Dec 2003 13:08:24 -0800, (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
    Bob Hairgrove, Dec 16, 2003
    #3
  4. k-man

    Peter Ammon Guest

    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.
    Peter Ammon, Dec 17, 2003
    #4
  5. 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][C][...][Z] ;
    int brr[m][A][C][...][Z] ;

    have a common pointer type that could point to either.


    --
    Shelley Hebert -
    Engineering Research Center - Mississippi State University
    Shelley Hebert, Dec 18, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. viza
    Replies:
    17
    Views:
    673
    santosh
    Jul 9, 2008
  2. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
  3. ImpalerCore

    memset pointer to 0

    ImpalerCore, Apr 15, 2010, in forum: C Programming
    Replies:
    19
    Views:
    1,598
    Phil Carmody
    Apr 20, 2010
  4. Wirianto Djunaidi
    Replies:
    2
    Views:
    195
    Wirianto Djunaidi
    Apr 29, 2008
  5. Replies:
    21
    Views:
    515
    Shao Miller
    Feb 17, 2013
Loading...

Share This Page