Middle parameter to memset that will achieve UCHAR_MAX

M

Martin Wells

I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

I'll be very specific about what I'm asking, so please if you don't
know shit about C or how to code then just stay quiet.

What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?

Emphasis needed so people don't start posting loops as a response. You
wouldn't think I'd need to add the emphasis but hey look at the utter
shite I got back in the other thread (not all of it was shite).

UCHAR_MAX isn't guranteed to fit in an int, but maybe we'll get a
consistent result across all implementations if we use it? I'll have
to look up the rules for signed to unsigned conversions. As far as I
know though, it's something along the lines of:

target = TARGET_TYPE_MAX + 1 - negative_number

~0 is a different value on different systems, depending on the system
used to represent negative numbers.

Any other takers for what we should give memset? I think basically we
need an int value that will convert to UCHAR_MAX on every
implementation? Maybe we might have to delve into determing the number
system via macros, but I'm just thinking out loud. In the end we'd
have:

memset(data, INT_VALUE_THAT_WILL_YIELD_UCHAR_MAX, sizeof data);

Martin
 
J

Joachim Schmitz

Martin Wells said:
UCHAR_MAX isn't guranteed to fit in an int
Says who?
char and unsigned char are guaranteed to have the same size. int is
guaranteed to have at least the size of a char, hence UCHAR_MAX fits an int.

Bye, Jojo
 
M

Martin Wells

Says who?


The Standard. It's in there, you'll find it. Google for the reason why
an "unsigned short" would possibly promote to an "unsigned int" rather
than a "signed int".

char and unsigned char are guaranteed to have the same size.


sizeof(char) == sizeof(char unsigned), yes.

int is guaranteed to have at least the size of a char, hence UCHAR_MAX fits an int.

sizeof is orthogonol is value range. You assertion is wrong.

Martin
 
F

Francine.Neary

~0 is a different value on different systems, depending on the system
used to represent negative numbers.

It has nothing to do with the system used to represent negative
numbers, only the representation of 0.

(6.3.3.3) The result of the ~ operator is the bitwise complement of
its operand (that is, each bit in the result is set if and only if the
corresponding bit in the converted operand is not set).
 
M

Martin Wells

An int whose value is -1 will convert to an unsigned char whose value
is UCHAR_MAX, as per:

| if the new type is unsigned, the value
| is converted by repeatedly adding or
| subtracting one more than the maximum
| value that can be represented in the
| new type until the value is in the
| range of the new type.

The question that needs to be asked though is whether memset will
perform this proper conversion, or whether it will just do something
like take the lower CHAR_BIT bits, in which case we'd be in trouble if
we're not using two's complement.

....actually I just found the following in the Standard:

| The memset function copies the value
| of c (converted to an unsigned char)
| into each of the first n characters
| of the object pointed to by s.

I've only got the 1999 Standard though. Can someone please confirm
whether C89 necessitates the same behaviour? If not then it seems we
have:

#define INT_FOR_UNSIGNED_CHAR_ALL_ONES (-1)

Martin
 
K

Keith Thompson

Joachim Schmitz said:
Says who?
char and unsigned char are guaranteed to have the same size. int is
guaranteed to have at least the size of a char, hence UCHAR_MAX fits an int.

Imagine the following:

CHAR_BIT == 16
UCHAR_MAX == 65535
sizeof(int) == 1
INT_MIN == -32768
INT_MAX == 32767

UCHAR_MAX will it in an unsigned int. It won't necessarily fit in a
signed int.
 
K

Keith Thompson

Martin Wells said:
I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

I'll be very specific about what I'm asking, so please if you don't
know shit about C or how to code then just stay quiet.

What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?
What argument can I give memset in order to set an entire chunk of
memory to all bits 1?

Emphasis needed so people don't start posting loops as a response. You
wouldn't think I'd need to add the emphasis but hey look at the utter
shite I got back in the other thread (not all of it was shite).
[...]

Well, if you hadn't started off by insulting the people you're asking
for help, I might have been inclined to offer an answer.
 
M

Martin Wells

Keith:
Well, if you hadn't started off by insulting the people you're asking
for help, I might have been inclined to offer an answer.


I didn't insult those from whom I seek help -- I insulted the people
who posted complete tripe.

Suppose it's up to you to decide which group you're in.

Martin
 
R

Richard Tobin

Well, if you hadn't started off by insulting the people you're asking
for help, I might have been inclined to offer an answer.
[/QUOTE]
I didn't insult those from whom I seek help -- I insulted the people
who posted complete tripe.

Suppose it's up to you to decide which group you're in.

Are you like this in real life?

-- Richard
 
C

christian.bau

I've already started a thread about this but it descended into crap
mostly because of incompetant replies.

Maybe I'll look at your posts once you manage a competent use of
English spelling.
 
P

pete

Joachim said:
Says who?
char and unsigned char are guaranteed to have the same size. int is
guaranteed to have at least the size of a char,

And if int does have the size of a char,
then there's no way that int can represent UCHAR_MAX.

If CHAR_BIT equals 16 and sizeof(int) equals one,
then INT_MAX is 0x7fff and UCHAR_MAX is 0xffff.
hence UCHAR_MAX fits an int.

Non sequitur.
 
P

pete

Martin said:
An int whose value is -1 will convert to an unsigned char whose value
is UCHAR_MAX, as per:

| if the new type is unsigned, the value
| is converted by repeatedly adding or
| subtracting one more than the maximum
| value that can be represented in the
| new type until the value is in the
| range of the new type.

The question that needs to be asked though is whether memset will
perform this proper conversion, or whether it will just do something
like take the lower CHAR_BIT bits, in which case we'd be in trouble if
we're not using two's complement.

...actually I just found the following in the Standard:

| The memset function copies the value
| of c (converted to an unsigned char)
| into each of the first n characters
| of the object pointed to by s.

I've only got the 1999 Standard though. Can someone please confirm
whether C89 necessitates the same behaviour?

ISO/IEC 9899: 1990

7.11.6.1 The memset function
Synopsis
#include <string.h>
void *memset(void *s, int c, size_t n);
Description
The memset function copies the value of c
(converted to an unsigned char) into each of
the first n characters of the object pointed to by s.
 
C

CBFalconer

Martin said:
Keith:


I didn't insult those from whom I seek help -- I insulted the
people who posted complete tripe.

Suppose it's up to you to decide which group you're in.

Well done. When does your revision of "How to Win Friends and
Influence People" come out? </sarcasm>
 
R

Richard Heathfield

Martin Wells said:
Keith:



I didn't insult those from whom I seek help -- I insulted the people
who posted complete tripe.

Suppose it's up to you to decide which group you're in.


You were doing pretty well up to now, Martin - but your latest round of
tact and diplomacy suggests that you have a bit more yet to learn about
human beings.

Do you - uh - want to kind of rewind a bit, and maybe mend some fences? I
think you may well find it to be a very sensible step at this point.
 
J

Joachim Schmitz

Keith Thompson said:
Imagine the following:

CHAR_BIT == 16
UCHAR_MAX == 65535
sizeof(int) == 1
INT_MIN == -32768
INT_MAX == 32767

UCHAR_MAX will it in an unsigned int. It won't necessarily fit in a
signed int.
signed int has the same sizes as unsigned int, so it does have the same
number of bits, so it would fit. It would not represent the same number
though.

typedef sizet_t unsigned int;
size_t size = (size_t)-1;

is legal, isn't it?

Bye, Jojo
 
M

Martin Wells

Richard:
Do you - uh - want to kind of rewind a bit, and maybe mend some fences? I
think you may well find it to be a very sensible step at this point.


I've no problem being courteous and polite to people who actually post
articles with substance, or even people who post misinformation but
who make explicit that they're not 100% on what they're saying. I
myself do it all the time, take for instance where I was wrong about
USHRT_MAX.

The people I've contempt for, and to whom I couldn't be bothered being
courteous, are the people who post absolute misinformation and
strongly assert that said information is true. Joachim has done this a
couple of times in the thread just gone.

Why competent programmers (or even people who acknowledge that their
understanding isn't grade A) would have taken offence to my posting, I
don't know.

Martin
 
J

Joachim Schmitz

Martin Wells said:
Richard:



I've no problem being courteous and polite to people who actually post
Basics of education: you should be courteous and polite to everybody, with
no exceptions whatsoever.
If you need exceptions, than at a bare minimum be courteous and polite to
everybody that didn't intentionionally offended you.
articles with substance, or even people who post misinformation but
who make explicit that they're not 100% on what they're saying. I
myself do it all the time, take for instance where I was wrong about
USHRT_MAX.

The people I've contempt for, and to whom I couldn't be bothered being
courteous, are the people who post absolute misinformation and
strongly assert that said information is true. Joachim has done this a
couple of times in the thread just gone.
In my signature I don't claim to be God or the pope. That should be enough
of a disclaimer, I think.
If you can't discuss issues, that's entirely your problem.
Why competent programmers (or even people who acknowledge that their
understanding isn't grade A) would have taken offence to my posting, I
don't know.
Your post was meant to be offending and looks like yet another sign of
missing education

Bye, Jojo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top