on memset

Discussion in 'C++' started by Vincent SHAO, Apr 16, 2008.

  1. Vincent SHAO

    Vincent SHAO Guest

    int distance[MAX_X][MAX_Y];
    memset(distance,-2,MAX_X*MAX_Y*sizeof(int));

    Academically, the buffer "distance" should have been filled with -2
    after these two line,
    but, memset just fail to do the trick, they are still a garbage value
    "-16843010"

    why?

    Thank you.
     
    Vincent SHAO, Apr 16, 2008
    #1
    1. Advertisements

  2. Vincent SHAO

    Ian Collins Guest

    Vincent SHAO wrote:
    > int distance[MAX_X][MAX_Y];
    > memset(distance,-2,MAX_X*MAX_Y*sizeof(int));
    >
    > Academically, the buffer "distance" should have been filled with -2
    > after these two line,
    > but, memset just fail to do the trick, they are still a garbage value
    > "-16843010"
    >

    This isn't garbage, it's the correct result.

    What units does memset work with?

    --
    Ian Collins.
     
    Ian Collins, Apr 16, 2008
    #2
    1. Advertisements

  3. Vincent SHAO

    Vincent SHAO Guest

    On 4ÔÂ16ÈÕ, ÏÂÎç2ʱ06·Ö, "Alf P. Steinbach" <> wrote:
    > *VincentSHAO:
    >
    > > int distance[MAX_X][MAX_Y];
    > > memset(distance,-2,MAX_X*MAX_Y*sizeof(int));

    >
    > > Academically, the buffer "distance" should have been filled with -2
    > > after these two line,

    >
    > No.
    >
    > > but, memset just fail to do the trick, they are still a garbage value
    > > "-16843010"

    >
    > > why?

    >
    > It seems your compiler creates program that use 32 bit two's complement 'int'.
    >
    > In that case -2 is represented as bit pattern 0xFFFFFFFE, and as signed char
    > value, 0xFE, and filling an 'int' with byte value 0xFE gives you 0xFEFEFEFE,
    > which represents -16843010.
    >
    > See the FAQ for more safe ways to represent matrices (or go directly to Boost
    > library).
    >
    > Cheers, & hth.,
    >
    > - Alf


    Thanks a lot.
    And that is to say, i have to use double 'for' to init the buffer if i
    want each elem in the buffer to be -2?
     
    Vincent SHAO, Apr 16, 2008
    #3
  4. Vincent SHAO wrote:
    >>> int distance[MAX_X][MAX_Y];
    >>> memset(distance,-2,MAX_X*MAX_Y*sizeof(int));


    > And that is to say, i have to use double 'for' to init the buffer if i
    > want each elem in the buffer to be -2?


    Why is that a problem?

    IMO it's a beginner's bad habit to try to make code as short as
    possible (and achieving this by use of ugly tricks and hacks). Code
    should be above all clear and well written. Shortness is completely
    secondary (and usually irrelevant).

    If you *really* want to use a "cool trick" to initialize that array
    with one single loop, you can do this:

    for(int i = 0; i < MAX_X*MAX_Y; ++i)
    ((int*)distance) = -2;

    However, that's ugly code and not recommended.
     
    Juha Nieminen, Apr 16, 2008
    #4
  5. Vincent SHAO

    Bo Persson Guest

    Juha Nieminen wrote:
    > Vincent SHAO wrote:
    >>>> int distance[MAX_X][MAX_Y];
    >>>> memset(distance,-2,MAX_X*MAX_Y*sizeof(int));

    >
    >> And that is to say, i have to use double 'for' to init the buffer
    >> if i want each elem in the buffer to be -2?

    >
    > Why is that a problem?
    >
    > IMO it's a beginner's bad habit to try to make code as short as
    > possible (and achieving this by use of ugly tricks and hacks). Code
    > should be above all clear and well written. Shortness is completely
    > secondary (and usually irrelevant).
    >
    > If you *really* want to use a "cool trick" to initialize that array
    > with one single loop, you can do this:
    >
    > for(int i = 0; i < MAX_X*MAX_Y; ++i)
    > ((int*)distance) = -2;
    >
    > However, that's ugly code and not recommended.


    It is also interesting to see what the compiler does for the
    "unoptimized" version with a double for loop:

    ; 8 : for (int i = 0; i != 1000; ++i)
    ; 9 : for (int j = 0; j != 1000; ++j)
    ; 10 : distance[j] = -2;

    00001 mov eax, -2 ; fffffffeH
    00006 mov ecx, 1000000 ; 000f4240H
    0000b mov edi, OFFSET ?distance@@3PAY0DOI@HA ; distance
    00010 rep stosd


    This code is
    - easier to read
    - faster than memset
    - correct


    Bo Persson
     
    Bo Persson, Apr 16, 2008
    #5
    1. Advertisements

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. k-man
    Replies:
    4
    Views:
    4,727
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    9,466
    tom_usenet
    Aug 24, 2004
  3. Replies:
    17
    Views:
    7,272
    Greg Comeau
    Sep 22, 2004
  4. Guest
    Replies:
    1
    Views:
    545
    Ron Natalie
    Sep 23, 2004
  5. =?ISO-8859-1?Q?Sacha_Sch=E4r?=

    Using memset instead of a proper ctor

    =?ISO-8859-1?Q?Sacha_Sch=E4r?=, Jan 24, 2005, in forum: C++
    Replies:
    12
    Views:
    656
    Dietmar Kuehl
    Jan 24, 2005
  6. copx

    memset() and portability

    copx, Jul 24, 2003, in forum: C Programming
    Replies:
    10
    Views:
    766
    Eric Sosman
    Jul 28, 2003
  7. Mantorok Redgormor

    perils of memset

    Mantorok Redgormor, Feb 22, 2004, in forum: C Programming
    Replies:
    2
    Views:
    1,173
    Christian Bau
    Feb 22, 2004
  8. Zhiqiang Ye
    Replies:
    53
    Views:
    11,253
    Dan Pop
    Jun 28, 2004
Loading...