473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

memset() and portability

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

Nov 13 '05 #1
10 6775
copx wrote:
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.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #2
In <bf**********@murdoch.hpl.hp.com> Chris Dollin <ke**@hpl.hp.com> writes:
copx wrote:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

"copx" <in*****@invalid.com> wrote in message

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.
Nov 13 '05 #4

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <bf**********@news7.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"copx" <in*****@invalid.com> wrote in message

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.

--
BC
Nov 13 '05 #5
In article <bf**********@imsp212.netvigator.com>, "Burne C" <a@b.com>
wrote:
"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <bf**********@news7.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"copx" <in*****@invalid.com> wrote in message
>
> 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.
Nov 13 '05 #6
In <bf**********@imsp212.netvigator.com> "Burne C" <a@b.com> writes:

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <bf**********@news7.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
> "copx" <in*****@invalid.com> wrote in message
> >
> > 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
Burne C wrote:

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <bf**********@news7.svr.pol.co.uk>,
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"copx" <in*****@invalid.com> wrote in message
>
> 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.

--
Er*********@sun.com
Nov 13 '05 #8
Dan Pop wrote:

In <bf**********@imsp212.netvigator.com> "Burne C" <a@b.com> writes:

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?

--
Er*********@sun.com
Nov 13 '05 #9

On Mon, 28 Jul 2003, Eric Sosman wrote:

Dan Pop wrote:

In <bf**********@imsp212.netvigator.com> "Burne C" <a@b.com> writes:

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?


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
Nov 13 '05 #10
"Arthur J. O'Dwyer" wrote:

On Mon, 28 Jul 2003, Eric Sosman wrote:

Dan Pop wrote:

In <bf**********@imsp212.netvigator.com> "Burne C" <a@b.com> writes:

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?


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.

--
Er*********@sun.com
Nov 13 '05 #11

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

Similar topics

17
by: Nollie | last post by:
Say you have a struct: struct MYSTRUCT { int x; int y; int w; int h; };
2
by: Mantorok Redgormor | last post by:
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; ...
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...
17
by: Frederick Ding | last post by:
Hi, guys,I met a problem, Please look at the problem below: int* bit = (int*)malloc(10000*sizeof(int)); memset(bit, 1, 10000*sizeof(int)); printf("%d %d %d\n", bit,bit, bit); Output: ...
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...
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.