Thread: Is {0,} okay?

  1. #31
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    void *memset(void *s, int c, size_t n);
    The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
    Take a look at the results
    Code:
    int arr[10];
    memset(arr, 300, 10);
    memset(arr, 300, sizeof(arr));

  2. #32
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by xeddiex
    Why not?
    Because memset works on bytes, not ints. It's fine if you want to initalise an int array to zero, but if you want an arbitrary value (like 2), then memset(array, 2, size); will fill every byte with a value 2. An int is generally larger than a byte.

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh?
    Code:
    #include<string.h>
    #include<stdio.h>
    int main( void )
    {
        int arr[10], x;
    
        memset( arr, 0, sizeof arr );
        for( x = 0; x < 10; x++ )
        {
            memset( arr + x, 2, 1 );
        }
    
        for( x = 0; x < 10; x++ )
        {
            printf("arr[%d] is %d\n", x, arr[x] );
        }
    
        return 0;
        
    }
    /*
    arr[0] is 2
    arr[1] is 2
    arr[2] is 2
    arr[3] is 2
    arr[4] is 2
    arr[5] is 2
    arr[6] is 2
    arr[7] is 2
    arr[8] is 2
    arr[9] is 2
    */
    Heheh.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #34
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    quzah: you should submit that tragic code to the international obfuscated C code contest Cute misuse of the function.

  5. #35
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quzah,

    Yes, I had thought you might reply with something like that

    Unfortunately, not everybody uses little endian machines

    My output from your code:
    Code:
    arr[0] is 33554432
    arr[1] is 33554432
    arr[2] is 33554432
    arr[3] is 33554432
    arr[4] is 33554432
    arr[5] is 33554432
    arr[6] is 33554432
    arr[7] is 33554432
    arr[8] is 33554432
    arr[9] is 33554432
    My more portable version:
    Code:
    #include<string.h>
    #include<stdio.h>
    
    int bigendian;
    
    int main( void )
    {
        int arr[10], x;
        int y = 1;
        if (*(char *)&y != 1)
            bigendian = 1;
    
    
        memset( arr, 0, sizeof arr );
        for( x = 0; x < 10; x++ )
        {
            int *offset = arr + x;
    
            if (bigendian)
                offset = (char *)offset + sizeof(int)-1;
    
            memset(offset, 2, 1 );
        }
    
        for( x = 0; x < 10; x++ )
        {
            printf("arr[%d] is %d\n", x, arr[x] );
        }
    
        return 0;
    }

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I thought you'd reply something like that. I did it with pointers initially, which worked as you have it. I just had to reply something to prove it could be done using memset. I was going to have 2 memset calls per loop iteration. One to set "sizeof( int ) -1" bytes to zero, and one to set the other. But this is the "optimized" version, and so the first just zeros it outside.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #37
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    True, a more efficient version like your first one that retains portability:
    Code:
    #include<string.h>
    #include<stdio.h>
    
    int bigendian;
    
    int main( void )
    {
        int arr[10], x;
        int y = 1;
        char *offset = (char *)arr;
    
        if (*(char *)&y != 1)
            offset += sizeof(int)-1;
    
    
        memset( arr, 0, sizeof arr );
    
        for( x = 0; x < 10; x++ )
        {
            memset( (int *)offset + x, 2, 1 );
        }
    
        for( x = 0; x < 10; x++ )
        {
            printf("arr[%d] is %d\n", x, arr[x] );
        }
    
        return 0;
    }
    *runs away*

  8. #38
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by quzah
    I thought you'd reply something like that. I did it with pointers initially, which worked as you have it. I just had to reply something to prove it could be done using memset. I was going to have 2 memset calls per loop iteration. One to set "sizeof( int ) -1" bytes to zero, and one to set the other. But this is the "optimized" version, and so the first just zeros it outside.


    Quzah.
    Actually all you proved is that it'd work with small int values

  9. #39
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's all they asked for. To initialize it to 2. Asked, and answered. I can give you a version that will work for all the numbers if you really want it. :P


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #40
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    yeah but in only one call to malloc? and not just malloc written once?

Popular pages Recent additions subscribe to a feed