Unix Programming - memset

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2005 > memset





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author memset
Roman Mashak

2005-06-24, 2:48 am

Hello, All!

What is the correct and standard-compliant way to zero out the structure:

struct sockaddr_in s_in;
memset(&s_in, 0, sizeof(s_in));

or

memset(&s_in, '\0', sizeof(s_in));

And the same question related to arrays.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Artie Gold

2005-06-24, 2:48 am

Roman Mashak wrote:
> Hello, All!
>
> What is the correct and standard-compliant way to zero out the structure:
>
> struct sockaddr_in s_in;
> memset(&s_in, 0, sizeof(s_in));
>
> or
>
> memset(&s_in, '\0', sizeof(s_in));


[note: The parens around the argument to the `sizeof' operator are
superfluous -- they are only necessary when you are taking the size of a
*type*.]
>
> And the same question related to arrays.
>


The two are the *same* (assuming this is C code; both 0 and '\0' are of
type int).

Of course,

struct sockaddr_in s_in = {0};

is probably better.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Barry Margolin

2005-06-24, 2:48 am

In article <3i1c7hFjepriU1@individual.net>,
Artie Gold <artiegold@austin.rr.com> wrote:

> Roman Mashak wrote:
>
> [note: The parens around the argument to the `sizeof' operator are
> superfluous -- they are only necessary when you are taking the size of a
> *type*.]
>
> The two are the *same* (assuming this is C code; both 0 and '\0' are of
> type int).


And even in C++, where they're different types, both will work because
the char '\0' will automatically be cast to int because of the prototype
of memset(). Then inside the function it will be cast back to char....

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Roman Mashak

2005-06-24, 2:48 am

Hello, Artie!
You wrote on Thu, 23 Jun 2005 22:15:59 -0500:

??>> And the same question related to arrays.
??>>
AG> The two are the *same* (assuming this is C code; both 0 and '\0' are of
AG> type int).

AG> Of course,

AG> struct sockaddr_in s_in = {0};
Does it mean, 's_in' is zero'd out at initialization time?
AG> is probably better.


With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Ari Lukumies

2005-06-24, 7:52 am

Roman Mashak wrote:
> Hello, Artie!
>
> AG> struct sockaddr_in s_in = {0};
> Does it mean, 's_in' is zero'd out at initialization time?


Unfortunately I don't have the Standard at hand so bear with me, but I
disagree with that somewhat. Initialization of the first element of an
array as in:

int arr[10] = {0};

zeroes the rest of the elements, also, but in the case of a structure,
only the first element of the structure gets zeroed; so much more if
it's in a C++ program, where the constructor is due.

-atl-
--
A multiverse is figments of its own creations
Alan Balmer

2005-06-24, 6:00 pm

On Fri, 24 Jun 2005 16:07:06 +0300, Ari Lukumies
<ari.lukumies@gmail.com> wrote:

>Roman Mashak wrote:
>
>Unfortunately I don't have the Standard at hand so bear with me, but I
>disagree with that somewhat. Initialization of the first element of an
>array as in:
>
> int arr[10] = {0};
>
>zeroes the rest of the elements, also, but in the case of a structure,
>only the first element of the structure gets zeroed; so much more if
>it's in a C++ program, where the constructor is due.


In C, the remainder of the structure will be initialized the same as
if it were static. However, this begs the question as to whether the
OP needs only initialization or is trying to clear the structure at
runtime.

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
William Ahern

2005-06-24, 6:00 pm

Ari Lukumies <ari.lukumies@gmail.com> wrote:
> Roman Mashak wrote:
[vbcol=seagreen]
> Unfortunately I don't have the Standard at hand so bear with me, but I
> disagree with that somewhat. Initialization of the first element of an
> array as in:


> int arr[10] = {0};


> zeroes the rest of the elements, also, but in the case of a structure,
> only the first element of the structure gets zeroed; so much more if
> it's in a C++ program, where the constructor is due.


Are you confusing this with unions, where only the first member can be
initialized?

When you statically initialize a structure any member not explicitly set is
done so using an implicit `0'.

- Bill
Brian Raiter

2005-06-24, 6:00 pm

> Of course,
>
> struct sockaddr_in s_in = {0};
>
> is probably better.


Yes, and the reason that this is better is that things like pointers
and floating-point numbers will still be initialized to "zero"
correctly, even when zero is not represented by all-bits-zero.

Unfortunately, the above construct will cause many versions of gcc
with all warnings on to complain about missing initializers. Very
annoying.

b
Roman Mashak

2005-06-27, 2:48 am

Hello, Alan!
You wrote on Fri, 24 Jun 2005 09:32:01 -0700:

??>> also, but in the case of a structure, only the first element of the
??>> structure gets zeroed; so much more if it's in a C++ program, where
??>> the constructor is due.

AB> In C, the remainder of the structure will be initialized the same as
AB> if it were static. However, this begs the question as to whether the
AB> OP needs only initialization or is trying to clear the structure at
AB> runtime.
All I need is correct initialization and clearing of structure.
If I my understanding is right 'struct sockaddr_in s_in = {0}' is cleared at
initialize time?

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Alan Balmer

2005-06-27, 5:53 pm

On Mon, 27 Jun 2005 11:12:38 +0900, "Roman Mashak" <mrv@tusur.ru>
wrote:

>Hello, Alan!
>You wrote on Fri, 24 Jun 2005 09:32:01 -0700:
>
> ??>> also, but in the case of a structure, only the first element of the
> ??>> structure gets zeroed; so much more if it's in a C++ program, where
> ??>> the constructor is due.
>
> AB> In C, the remainder of the structure will be initialized the same as
> AB> if it were static. However, this begs the question as to whether the
> AB> OP needs only initialization or is trying to clear the structure at
> AB> runtime.
>All I need is correct initialization and clearing of structure.
>If I my understanding is right 'struct sockaddr_in s_in = {0}' is cleared at
>initialize time?
>

Yes.

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com