memset
can do the job.
See the illustrative example where 14 elements are zeroed.
#include <stdio.h>
#include <string.h> // for memset
void print_array(unsigned long *array, int len)
{
int i;
for (i = 0; i<len; i++ )
{
printf("%lu ", array[i]);
};
printf("\n");
}
int main()
{
unsigned long st[25];
// initialize all elements in the table (25 of them)
for(int i = 0; i < 25; ++i)
st[i] = i;
// Assign zeroes from position 10 to position 24 (14 zeroes)
// Note: The last position of the st[] table is not zeroed on purpose!
// remember of the type of the array `sizeof(unsigned long)`:
memset( st+10, 0, 14*sizeof(unsigned long) );
print_array(st, 25);
return 0;
}
OUTPUT:
0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24
Assembly for both cases x86-64 gcc 7.2 (no optimization!):
int main()
{
unsigned long st[25];
memset( st+10, 0, 15*sizeof(unsigned long) );
return 0;
}
main:
push rbp
mov rbp, rsp
sub rsp, 208
lea rax, [rbp-208]
add rax, 80
mov edx, 120
mov esi, 0
mov rdi, rax
call memset
mov eax, 0
leave
ret
int main()
{
unsigned long st[25];
for(int i = 10; i < 25; ++i) st[i] = 0;
return 0;
}
main:
push rbp
mov rbp, rsp
sub rsp, 88
mov DWORD PTR [rbp-4], 10
.L3:
cmp DWORD PTR [rbp-4], 24
jg .L2
mov eax, DWORD PTR [rbp-4]
cdqe
mov QWORD PTR [rbp-208+rax*8], 0
add DWORD PTR [rbp-4], 1
jmp .L3
.L2:
mov eax, 0
leave
ret