user2737926
user2737926

Reputation: 97

memset is setting value incorrectly

This function is called in my program :

int cal_addr(long file_size ,  long* block, file* isfile,unsigned long block_size;) {


        long double tmp = (long double) file_size/block_size;
        *block = ceil (tmp ) ;
        int start = us.alloc_index ;    // us.alloc_index is int

        int fd = fs.tot_alloc_file ;  //  fs.tot_alloc_file is int
        int blk = *((int*)block) ;
        size_t s =  (blk) * sizeof(int) ;
    // us.usage is global array of integers
        memset(& (us.usage[start] ), fd , s);
        us.alloc_index =  us.alloc_index + (*block) ;
        isfile->end_addr_usage = us.alloc_index;
        return 1 ;
}

// Output of gdb is below. I see that fd value is 1 still when I print the elements of us.usage[202] for eg it has this wierd value. Not 1 that I expect

(gdb) p us.usage[202]
$3 = 16843009

(gdb) p fd
$5 = 1
(gdb) 

Upvotes: 0

Views: 88

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272737

memset operates on a char-by-char basis. You cannot use it to set the values of ints like this. (Note that 16843009 == 0x01010101.)

If you want to set the value of ints, you'll need to use a loop.

Upvotes: 3

Related Questions