Thread: memset() - question of using.

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    memset() - question of using.

    Hi,
    I want to fill up an array of int with 1. I have such problem:
    Code:
    #include <cstring>
    #include <cstdio>
    
    int     main()
    {
            int     y[4]={9,9,9,9},
                    i;
            memset(y,1,4 * sizeof(int));
            for(i=0;i<4;i++)
                    printf("%d",y[i]);
            return 0;
    }
    and when i run it:
    $ ./a.out
    16843009168430091684300916843009$
    I have no clue what's wrong, I expect something with third argument of memset() but 4 * sizeof(int) is a size of y. Can anyone help me ?
    Regards.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because the 1 fills each byte, not each int.
    Each int is now 0x01010101 (which is some large number, probably the one you're seeing)
    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.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    Hm... so how shouid i call memset?
    Regards.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This will fix your problem:

    Code:
    void memset32(void *Buffer,DWORD value,DWORD size)
    {
      asm {
        les    edi,[Buffer]
        mov  ecx,[value]
        mov  eax,[size]
        rep   stosd
        and  ecx,03h
        rep   stosb
      }
    }

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    76
    tx Bubba but is there no way to use standard memset? Btw. is this asm code portable, I mean if it depends on arch?
    Regards

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you want to set something other than bytes, then use a for loop
    Code:
    for(i=0;i<4;i++) y[i] = 1;
    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.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <algorithm>
    
    int y[4];
    
    std::fill(y,y+4,1); // Set all 4 elements of array "y" to 1
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    76
    Ok, now i have no doubt. Thank you.
    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange memset question
    By Death_Wraith in forum Game Programming
    Replies: 14
    Last Post: 09-25-2004, 12:58 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 08:58 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM