why it doesnot works for values other than 0 and -1
i mean when i tried to print values of array 'a' after memset() then it shows incorrect values.

``````int a[100];
memset(a,0,sizeof(a));  ///it works
memset(a,-1,sizeof(a));  ///it works
memset(a,1,sizeof(a));  ///it doesnt
``````

`memset(a,1,sizeof(a));`

What were you expecting this to do and what did it do? If an int has 4 bytes (or 8 bytes), and you set the value of each byte to 1, what value will the int have? (The answer is not 1)

How many bytes in an int? On your system, 4. Let's set each byte to a value of 1. This is what you are doing with memset. Changing the value of each byte.

So here is one int, in binary format. Four bytes in the int, and you set each …

You actually write bytes, and when trying to write an int, presumably your platform int is iterpreted as 4 bytes, you will have like this:

``````byte[]
int[][][][] //4*byte
``````

As you …

now how should i set every 4th byte to '1' using memset?

memset doesn't skip bytes. That's not what it's for. How about something like this:

``````int array[100];
int i=0;
for ( ; i<100; ++i)
{
a[i] = 1;
}
``````

## All 14 Replies

`memset(a,1,sizeof(a));`

What were you expecting this to do and what did it do? If an int has 4 bytes (or 8 bytes), and you set the value of each byte to 1, what value will the int have? (The answer is not 1)

i am trying to initialize the array 'a' elements to integer 1 but it initialize with '16843009'. or some other garbage values when i tried with integer 2,3 ,4 etc.
it correctly sets values 0 and -1 only.

How many bytes in an int? On your system, 4. Let's set each byte to a value of 1. This is what you are doing with memset. Changing the value of each byte.

So here is one int, in binary format. Four bytes in the int, and you set each byte to a value of one.

00000001 00000001 00000001 00000001

What's the value of that binary number in decimal?

http://www.binaryhexconverter.com/binary-to-decimal-converter

Put the number 00000001000000010000000100000001 in the left box. Press the convert button. What number do you get in the right box? 16843009

got it.
now how should i set every 4th byte to '1' using memset?

You actually write bytes, and when trying to write an int, presumably your platform int is iterpreted as 4 bytes, you will have like this:

``````byte[]
int[][][][] //4*byte
``````

As you know from your assembly courses, a byte is represented on 8 bits (binary) that can go from 0 to 255 (for an unsigned int) or from -127 to 127 (for a signed integer).
So when you try to fill up an array of ints (which, remember, can store 4 bytes), it will fill it up with the number: [1][1][1][1]. What does that mean? It means that your array will be filled for each slot with `4*` the integer you provided. The compilers interprets it like this:

``````byte[] = 256^0
int[][][][] = 256^3 + 256^2 + 256^1 + 256^0
``````

When you provide a number (in your case 1), the result should be like this:

``````byte[1] = 1 * 256^0
int[1][1][1][1] = 1 * 256^3 + 1 * 256^2 + 1 * 256^1 + 1 * 256^0 = 16843009
``````

For a better view over the situation use this:

``````memset(a,1,sizeof(a));
printf("%x", a[1]); //printing in hex
``````

which will result in:

``````10101010 = 4 of 1's in hexa.
``````

now how should i set every 4th byte to '1' using memset?

memset doesn't skip bytes. That's not what it's for. How about something like this:

``````int array[100];
int i=0;
for ( ; i<100; ++i)
{
a[i] = 1;
}
``````

@lucaci Andrew..your answer is same as of Moschops but the link you provide solves my problem. i have to use fill_n() instead of memset();
but it works only for 1-D array. for 2-D it gives error.

@moschops i am trying to avoid that loop. thats why i am using memset which fails.

Using C++'s fill_n for 2d arrays is easy. Remember, fill_n fills an array. 2d arrays are like this:

``````[  |  |  |  |  |  ]
|[]|[]|[]|[]|[]|[]|
``````

So in order to fill up a 2d array use this:

``````#define SIZEA 5
#define SIZEB 10

int main(){
int grid[SIZEA][SIZEB];
int i;
for (i=0;i<SIZEA;i++)
std::fill_n(grid[i], SIZEB, 5); //instead of 5 you put whatever you want.
return 0;
}
``````

@moschops i am trying to avoid that loop. thats why i am using memset which fails.

What do you think memset() does? Internally it's just a loop. There might be some unrolling involved or it might be written in assembly to speed things up, but it's still a loop.

Here is a conforming (and portable) implementation of memset(). You'll notice there's not much to it, and it matches the spirit of Moschops' example.

@deceptikon does strlen() function give the length of string in O(n) or O(1)?

`strlen()` computes the size of the string by traversing it until it finds the character `'\0'`, which means the end of the string.
So basically, it has the complexity of O(n).
For other implementations, such as the `string` class from C++, the `string` object has an element which keeps track of its size, so by calling the `size()` or `length()` method on it, it would generate a complexity of O(1), but in C, using `strlen()`, it has the complexty of O(n).

Here's an implementation of strlen:

``````size_t strlen(const char *str){
size_t size = 0;
while (*str++ && ++size);
return size;
}
``````

@deceptikon does strlen() function give the length of string in O(n) or O(1)?

You really can't assume that it's better than O(n), and it would take quite an idiotic programmer to make it worse than O(n). ;) The structure of C-style strings isn't conducive for optimizations that would improve the complexity because in the end you're searching for a character in the string. The library doesn't have control over when or where that character is placed, or even if it exists at all.

thats why i am using memset which fails.

This isn't what `memset()` is supposed to be used for. It does byte-by-byte setting to a specific value. You're going to have to use the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.