I have a malloc'd array as follows:
int* buf;
buf = (int*)malloc(sizeof(int) * N)
where N is the number of elements in the integer array. I am trying to set all N elements in the array to a specific value, say 4 (so buf = [4, 4, 4, ..., 4]).
For intents and purposes, this is mostly an experiment, and so I am trying to only use memset, without for loops. Is this possible?
With memset, I do:
memset(buf, 4, sizeof(int)*N);
which I assumed places a 4 everywhere in the array in memory, but this doesn't seem to work as expected. Suppose N = 20, then the output is:
[1,6,8,4,3,0,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,,0,0,0,0]
which is not what I expected, but what I do notice is it has set the 4th element (which does correspond to sizeof(int)*N) to 4. I thought memset would set it all to 4, similar to the string case?