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. Advertising

  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. Advertising

  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. 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. k-man
    Replies:
    4
    Views:
    4,527
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,860
    tom_usenet
    Aug 24, 2004
  3. Replies:
    17
    Views:
    6,498
    Greg Comeau
    Sep 22, 2004
  4. Guest
    Replies:
    0
    Views:
    395
    Guest
    Sep 23, 2004
  5. Guest
    Replies:
    1
    Views:
    426
    Ron Natalie
    Sep 23, 2004
Loading...

Share This Page