Searching \ for 'memset' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=memset
Search entire site for: 'memset'.

Truncated match.
'memset'
2000\02\02@134001 by

Question:
Which one of the following is correct, a or b ?

long val[8];  // long = 16 bits
memset(val,0x05,sizeof(val));

a)  val[0] : 0x0505
.....
val[7] : 0x0505

b) val[0] : 0x0005
.....
val[7] : 0x0005

Peter

That memset will give you val[0] through val[3] = 0x0505 and val[4] through
val[7] = 0x0000;

the right way to do it is (assuming you want 0x0505 and not 0x0005)
memset( val, 0x05, 8 * sizeof( long ));

----- Original Message -----
To: <PICLISTMITVMA.MIT.EDU>
Sent: Wednesday, February 02, 2000 1:39 pm
Subject: memset

Question:
Which one of the following is correct, a or b ?

long val[8];  // long = 16 bits
memset(val,0x05,sizeof(val));

a)  val[0] : 0x0505
.....
val[7] : 0x0505

b) val[0] : 0x0005
.....
val[7] : 0x0005

Peter

I forgot to mention that if you want 0x0005, you can either write a version
of memset (perhaps lmemset) that assigns longs, or do each assignment
individually.

{Original Message removed}
John Pfaff schrieb:

> That memset will give you val[0] through val[3] = 0x0505 and val[4] through
> val[7] = 0x0000;

You're right, but CCS does it as version a) !

{Quote hidden}

long val[8];  // long = 16 bits
memset(val,0x05,sizeof(val));

a)  val[0] : 0x0505
.....
val[7] : 0x0505

That's the correct one.  Here's quote from a unix man page:

SunOS 5.5.1         Last change: 22 Jan 1993                    1

memory(3C)             C Library Functions             memory(3C)

memset() sets the first n bytes in  memory  area  s  to  the
value of c (converted to an unsigned char).  It returns s.

BillW

William Chops Westfield schrieb:

>     long val[8];  // long = 16 bits
>     memset(val,0x05,sizeof(val));
>
>     a)  val[0] : 0x0505
>          .....
>         val[7] : 0x0505
>
> That's the correct one.  Here's quote from a unix man page:
>
> SunOS 5.5.1         Last change: 22 Jan 1993                    1
>
> memory(3C)             C Library Functions             memory(3C)
>
>      memset() sets the first n bytes in  memory  area  s  to  the
>      value of c (converted to an unsigned char).  It returns s.
>

That's what K&R says:
Except ... the first n characters ... which is not as clear as bytes !

>
> BillW

Actually, (a) is completely correct, although the alternative below will
also work just fine, and avoids any potential confusion.

K&R defines that sizeof(val) will return the number of bytes in the array
val, not just the size of the pointer to the array, so sizeof(val) returns
16 as required.

This is why CCS got it right.

It's these subtle bits about C that cause many bugs!

Cheers,

Andy

{Original Message removed}
Andy Baker schrieb:

> Actually, (a) is completely correct, although the alternative below will
> also work just fine, and avoids any potential confusion.
>
> K&R defines that sizeof(val) will return the number of bytes in the array
> val, not just the size of the pointer to the array, so sizeof(val) returns
> 16 as required.
>
> This is why CCS got it right.
>
> It's these subtle bits about C that cause many bugs!

no, only one bug - the next one !

>
>
> Cheers,
>
> Andy
>
> {Original Message removed}

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...