On Sat, 26 Jan 2008 13:02:39 -0800 (PST), Gaijinco
<> wrote in comp.lang.c:
> I'm having a headache using memset()
>
> Given:
>
> int v[1000000];
> memset((void*)v, 1, sizeof(v));
No need for the cast to void pointer here, the conversion is
automatic. But do make sure to include <string.h>. Also there is no
need for the parentheses abound the 'v' as an operand to the sizeof
operator, as 'v' is an object, not a type.
> Can I be 100% positive than v[i] = 1 for i > 0, or there is something
> else I have to do?.
No you can't, and on most implementations it will not be. memset()
will place the value 1 into every byte that makes up the array, and
there are 1,000,000 times sizeof (int) bytes in the array.
There are a few implementations, mostly digital signal processors,
where CHAR_BIT is 16 and char and int have the same size and
representation, where every int in the array will be set to the value
1.
But on most platforms, especially anything in a desktop computer, you
won't get that value.
On old 16-bit implementations for MS-DOS, each int in the array will
wind up with the value 257.
On 32-bit Windows, Macintosh, or Linux platforms using x86 processors,
each int in the array will have the value 16843009.
On 64-bit operating systems using x86, the result will be
72340172838076673.
> In various computers that I have tried it seems that the only value I
> can copy to a range with memset() that works is 0.
It works for a value of 0 because memset() with a value of 0 will set
every bit in every byte to all bits 0. C guarantees that any integer
type with a value of all bits 0 has the value 0. This is not
guaranteed for floating point types or pointers.
As to how to do it, you need to use a loop:
#define ARRAY_SIZE 1000000
int v [ARRAY_SIZE];
size_t index;
for (index = 0; index < ARRAY_SIZE]; ++index)
{
v [index] = 1;
}
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html