perils of memset

M

Mantorok Redgormor

When is it that memset caused problems?
I recall from posts in the past where someone
used memset in their code that invoked undefined
behavior.

What about the following?

char the_array[10];

memset(the_array, 'A', sizeof the_array);

does this invoked undefined behavior
or can create a trap representation?
iirc there were some issues involving
trap representations with memset usage.
 
J

Jack Klein

When is it that memset caused problems?
I recall from posts in the past where someone
used memset in their code that invoked undefined
behavior.

What about the following?

char the_array[10];

memset(the_array, 'A', sizeof the_array);

does this invoked undefined behavior
or can create a trap representation?
iirc there were some issues involving
trap representations with memset usage.

Your particular example is just fine, and has well-defined results.
Unless you later pass the array to a function that accepts a string,
since it is not '\0' terminated. You are setting each character in
your array to 'A' which is guaranteed to be a valid value for a
character.

The issue is that when using calloc(), or memset(some_object, 0,
some_size), you set the memory to all bits 0. But all bits 0 is not
necessarily what you expect it to be.

The C standard guarantees that all bits 0 produces a valid value of 0
for all three of the character types. It also guarantees that it
produces a valid value of 0 in the all of the C99 optional exact width
integer types, if the compiler has such types. Technically it does
not guarantee that it produces valid values of 0 in the rest of the
"ordinary" integer types ((un)signed short, (un)signed int, (un)signed
long, (un)signed long long), but there is a defect report to correct
that in a future update.

What the C standard does not and never will guarantee is that all bits
0 produces the value 0.0, or any valid value, in floating point types.
It also does not guarantee that it produces the value NULL, or any
valid value, in pointer types.

There are definitely architectures where a null pointer does not have
the pattern of all bits 0. There are probably implementations where
all bits 0 is not the value of 0.0 in floating pointy types.
 
C

Christian Bau

When is it that memset caused problems?
I recall from posts in the past where someone
used memset in their code that invoked undefined
behavior.

What about the following?

char the_array[10];

memset(the_array, 'A', sizeof the_array);

does this invoked undefined behavior
or can create a trap representation?
iirc there were some issues involving
trap representations with memset usage.

memset itself is fine. In your example, the contents of the_array after
the memset is fine as well. If you call memset for something that is not
an array of char, for example an array of int, then the resulting values
may be trap representations and reading them may cause undefined
behavior.
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top