Setting all elements of an array to zero

This is a discussion on Setting all elements of an array to zero within the C Programming forums, part of the General Programming Boards category; Hi, int a[1000] = { 0 }; this initializes all elements of the array a to 0. let's say i ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    25

    Setting all elements of an array to zero

    Hi,

    int a[1000] = { 0 };

    this initializes all elements of the array a to 0. let's say i have changed some elements, and now want to set all items to 0 again. should i go through all elements with a loop and set them to zero one by one? is there a quicker way to achieve it?

    thanks in advance

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    memset?

    I'm not quite sure if that's quicker or more efficient, however...
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    yeah that is exactly what i'm looking for, another method, but as you say, is that faster? anyone knows about the performance of memset over going over all elements?

    thanks

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    IMO, memset would be slower in this case since it steps through the memory one byte at a time. The other loop would step through and set 4 bytes at a time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Not all zeros are equal.
    http://c-faq.com/malloc/calloc.html

    There's no guarantee of all-bits-zero representation for pointers and floats.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    memset is very efficient. Even if it only sets one byte at a time (and there's no reason it has to), it's probably optimised with assembly language.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    So then I guess the point is it would really depend on 1) what library we're talking about and 2) what linker and how each handle memset vs. stepping through an array.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think you should use memset. That's what it's for, after all.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >anyone knows about the performance of memset over going over all elements?
    It's fast enough for most purposes. If memset isn't fast enough for you, you're looking at some fairly heady and non-portable optimizations. Do you really need to squeeze every possible cycle out of this? Too many people optimize when they really don't need to.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    25
    nope, i don't wanna get into any hairy stuff, im just looking for the fastest safe method possible, which can possibly be, in this context, looping through each element and memset, i agree with you that it must satisfy my needs.

    thanks for all the help and advices you guys have given

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21