memset() and portability

C

copx

I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

copx
 
C

Chris Dollin

copx said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

memset( myArray, 0, sizeof (myArray) );

seems, to me at least, to be more reliable, if myArray is indeed an
array and not a pointer.

For int arrays there are in-principle portability traps, unless
someone's found a proof from the Standard that treating an array
of ints as (unsigned) chars and filling it with (char) zeroes will
guarantee that it gets filled with (int) zeroes.
 
D

Dan Pop

In said:
copx said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

memset( myArray, 0, sizeof (myArray) );

seems, to me at least, to be more reliable, if myArray is indeed an
array and not a pointer.

It's also more readable, especially if you drop the unneeded parentheses,
to expose the fact that sizeof is an operator and not a function:

memset(myArray, 0, sizeof myArray);
For int arrays there are in-principle portability traps, unless
someone's found a proof from the Standard that treating an array
of ints as (unsigned) chars and filling it with (char) zeroes will
guarantee that it gets filled with (int) zeroes.

The official answer to a Defect Report guarantees that. Hopefully, TC2
will contain the relevant change to the current standard.

But, since C89 doesn't have the problem in the first place and C99 is
practically unimplemented, there is no issue worth worrying about ;-)

Dan
 
M

Malcolm

copx said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));
sizeof(char) always equals one.

memset() has a few irritating portability traps. For instance, a NULL
pointer is almost always all bits zero, but this is not guaranteed. (It is
guaranteed to always compare equal to 0). A floating point number is almost
always 0 when all bits are zero, but again this is not guaranteed.

The problem is that your code will work happily, and then suddenly break
when someone transfers it to one of these weird formats.
 
B

Burne C

Christian Bau said:
Malcolm said:
copx said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));
sizeof(char) always equals one.

"char" plays two roles in C. A "char" is the basic storage unit;
everything is measured in units of "char", and therefore sizeof (char)
must be 1. On the other hand, "char" is also a type of its own. If I
need an array of integers, and I know that all values are from 0 to 127,

No, the range of char is -128 to 127, and 0 to 255 for unsigned char.
 
C

Christian Bau

"Burne C" <[email protected]> said:
Christian Bau said:
Malcolm said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

sizeof(char) always equals one.

"char" plays two roles in C. A "char" is the basic storage unit;
everything is measured in units of "char", and therefore sizeof (char)
must be 1. On the other hand, "char" is also a type of its own. If I
need an array of integers, and I know that all values are from 0 to 127,

No, the range of char is -128 to 127, and 0 to 255 for unsigned char.

I hope you don't have to port any code that relies on this.
 
D

Dan Pop

Christian Bau said:
Malcolm said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

sizeof(char) always equals one.

"char" plays two roles in C. A "char" is the basic storage unit;
everything is measured in units of "char", and therefore sizeof (char)
must be 1. On the other hand, "char" is also a type of its own. If I
need an array of integers, and I know that all values are from 0 to 127,

No, the range of char is -128 to 127, and 0 to 255 for unsigned char.

An object declared as type char is large enough to store any member
of the basic execution character set. If a member of the required
source character set enumerated in $2.2.1 is stored in a char object,
its value is guaranteed to be positive.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Have you ever heard of a character set called EBCDIC?

Dan
 
E

Eric Sosman

Burne said:
Christian Bau said:
Malcolm said:
I want to fill char/int arrays with zeros.
Is something like this portable/save:

char myArray[30][80];

memset(myArray,0,(30*80) * sizeof(char));

sizeof(char) always equals one.

"char" plays two roles in C. A "char" is the basic storage unit;
everything is measured in units of "char", and therefore sizeof (char)
must be 1. On the other hand, "char" is also a type of its own. If I
need an array of integers, and I know that all values are from 0 to 127,

No, the range of char is -128 to 127, and 0 to 255 for unsigned char.

The range of `signed char' is at least -127 through +127,
possibly wider. The range of `unsigned char' is at least 0
through 255, possibly wider (although the minimum value will
necessarily be zero). The range of plain `char' is the same
as one or the other of `signed char' or `unsigned char', at
the whim of the implementation. Thus, the range 0 through 127
is exactly the set of values that can be guaranteed to fit in
a plain `char' on every C implementation, however bizarre.
To put it another way, any attempt to store a value outside
this range in a plain `char' is non-portable.[*]

[*] Of course, constructs like `char c1 = CHAR_MIN;' and
`char c2 = -1;' are "portable" in the sense that they
work everywhere. However, the values of `c1' and `c2'
are implementation-dependent, so the constructs are
"non-portable" in that they don't give the same
results everywhere.
 
E

Eric Sosman

Dan said:
An object declared as type char is large enough to store any member
of the basic execution character set. If a member of the required
source character set enumerated in $2.2.1 is stored in a char object,
its value is guaranteed to be positive.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Have you ever heard of a character set called EBCDIC?

Haven't seen anyone else respond to this one, so I'll bite:
Yes, I've heard of (and used) EBCDIC, and I remember nothing
about it that contradicts the quoted text from the Standard.
What have I missed?
 
A

Arthur J. O'Dwyer

Haven't seen anyone else respond to this one, so I'll bite:
Yes, I've heard of (and used) EBCDIC, and I remember nothing
about it that contradicts the quoted text from the Standard.
What have I missed?

If your system uses EBCDIC, then CHAR_MAX must be bigger than 128,
which means either that 'char' must be an unsigned type or must have
more than 8 bits, or both. You've snipped the part Dan was responding
to, so I don't know which of these points applies to the original
conversation.

-Arthur
 
E

Eric Sosman

Arthur J. O'Dwyer said:
If your system uses EBCDIC, then CHAR_MAX must be bigger than 128,
which means either that 'char' must be an unsigned type or must have
more than 8 bits, or both. You've snipped the part Dan was responding
to, so I don't know which of these points applies to the original
conversation.

Aha! What I missed was who wrote what: I somehow thought
Dan was objecting to a piece of Standard quoted by "Burne C,"
but in fact Dan quoted the Standard to refute "Burne C." Sorry
for the (my) confusion.
 

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

Similar Threads

memset 24
Adding adressing of IPv6 to program 1
a = b or memset/cpy? 17
what about memset? 34
memset 21
memset() on a struct or union 21
Fibonacci 0
C pipe 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top