473,473 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

perils of memset

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
Nov 14 '05 #1
2 4047
On 21 Feb 2004 20:30:02 -0800, ne*****@tokyo.com (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
Nov 14 '05 #2
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
9
by: divya_rathore_ | last post by:
Consider the dynamic allocation of a 2D array: int **temp; temp = new int*; for (y=0; y<height; y++) temp = new int; I have 2 Questions: Does new initializes memory to 0?
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
18
by: dykeinthebox | last post by:
Consider the following program: #include <stdlib.h> #include <string.h> int main( void ) { void *p = malloc( 4 ); if ( p ) {
18
by: Gaijinco | last post by:
I'm having a headache using memset() Given: int v; memset((void*)v, 1, sizeof(v)); Can I be 100% positive than v = 1 for i 0, or there is something else I have to do?.
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.