Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > perils of memset

Reply
Thread Tools

perils of memset

 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      02-22-2004
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.



--
nethlek
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      02-22-2004
On 21 Feb 2004 20:30:02 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Mantorok Redgormor)
wrote in comp.lang.c:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      02-22-2004
In article <(E-Mail Removed) >,
(E-Mail Removed) (Mantorok Redgormor) wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The perils of misplaced decimal places Rhino Java 1 04-14-2006 07:31 AM
Interesting article by Joel Spolsky: The Perils of JavaSchools Casey Hawthorne Java 14 01-16-2006 07:17 PM
Perils of getting Paid Tom A+ Certification 4 04-15-2004 02:01 AM
perils of sizeof Mantorok Redgormor C Programming 4 01-25-2004 05:54 PM
TinyURL perils Petit Alexi Computer Support 13 11-10-2003 04:37 AM



Advertisments