Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   memset doubt (http://www.velocityreviews.com/forums/t439235-memset-doubt.html)

srivatsan_b 08-31-2005 05:43 AM

memset doubt
 
Hi,

Can somebody explain whether an explicit typecast is mandatory while
calling memset function for a structure? like in the following code
snapshot.....
struct some_structure x;
memset((some_structure*)&x,0,sizeof(some_structure ));
Will memset(&x,0,sizeof(some_structure)); cause some issues?

Thanks in advance
Srivatsan


Peter Nilsson 08-31-2005 05:59 AM

Re: memset doubt
 
srivatsan_b wrote:
> Hi,
>
> Can somebody explain whether an explicit typecast is mandatory while
> calling memset function for a structure? like in the following code
> snapshot.....
> struct some_structure x;
> memset((some_structure*)&x,0,sizeof(some_structure ));
> Will memset(&x,0,sizeof(some_structure)); cause some issues?


Slightly better is... memset(&x, 0, sizeof x);

In C, there is an issue, but it's not the issue you're asking about.

In C, any object pointer can be implicitly converted to a pointer
to void [subject to cv-qualifiers.] So the answer to your question
is no. C++ is different, but you should ask in a C++ group if you
ere actually using a C++ compiler.

Where you may have problems is that setting all the bits of the
structure contents to zero, may not do what you want. If your
structure contains pointers for instance, then setting the bits to
0 need not set them to null pointers.

If you want to zero initialise a structure (or indeed any other
object type), then you simply need to do...

struct some_structure x = { 0 };

No memset is required.

--
Peter


Keith Thompson 08-31-2005 06:01 AM

Re: memset doubt
 
"srivatsan_b" <srivatsanbs@gmail.com> writes:
> Can somebody explain whether an explicit typecast is mandatory while
> calling memset function for a structure? like in the following code
> snapshot.....
> struct some_structure x;
> memset((some_structure*)&x,0,sizeof(some_structure ));
> Will memset(&x,0,sizeof(some_structure)); cause some issues?


Assuming that you've remembered to #include <string.h>, which declares
the memset() function, the cast is unnecessary. Any object pointer
type can be implicitly converted to void*, and vice versa.

There are a couple of problems with your code, one serious and one
cosmetic.

Given a declaration of "struct some_structure", there is no type
called "some_structure"; the struct keyword is necessary unless you've
also declared it as a typedef. (Some would argue that typedefs for
struct types are poor style.)

<OT>
Note that the rules are different for C++. If I recall correctly, a
declaration of "struct some_structure" makes "some_structure" visible
as a type name, and there is no implicit conversion to void*. That
shouldn't be relevant unless you're using a C++ compiler -- and if you
are, you're in the wrong newsgroup.
</OT>

Also, using the size of the object makes for cleaner code than using
the size of the type. For example:

struct some_structure x;
memset(&x, 0, sizeof x);

Not only is this shorter, it avoid errors if the type of x is changed.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Barry Schwarz 08-31-2005 06:25 AM

Re: memset doubt
 
On 30 Aug 2005 22:43:54 -0700, "srivatsan_b" <srivatsanbs@gmail.com>
wrote:

>Hi,
>
>Can somebody explain whether an explicit typecast is mandatory while
>calling memset function for a structure? like in the following code
>snapshot.....
> struct some_structure x;
> memset((some_structure*)&x,0,sizeof(some_structure ));
>Will memset(&x,0,sizeof(some_structure)); cause some issues?
>


Firstly, &x already has type pointer to struct some_structure.
Therefore, casting any value to the same type it already has is by
definition redundant.

Secondly, memset expects the first argument to have type pointer to
void. If you were going to cast the value, that is what you would
cast it to.

Thirdly, pointer to void is "compatible" with any other unqualified
object pointer type. This means that the compiler can implicitly
convert one to the other whenever it needs to without requiring a
cast.

The only problem with your final question is that for C89 compilers
the cast must include the keyword struct. The use of the structure
tag without the keyword did not become standard until C99 and there
are very few compilers of that grade in use.


<<Remove the del for email>>

Keith Thompson 08-31-2005 06:36 AM

Re: memset doubt
 
Barry Schwarz <schwarzb@deloz.net> writes:
> On 30 Aug 2005 22:43:54 -0700, "srivatsan_b" <srivatsanbs@gmail.com>
> wrote:

[...]
> The only problem with your final question is that for C89 compilers
> the cast must include the keyword struct. The use of the structure
> tag without the keyword did not become standard until C99 and there
> are very few compilers of that grade in use.


No, a declaration of "struct foo" doesn't make just "foo" visible as a
type name in either C89 or C99. (It does in C++.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Emmanuel Delahaye 08-31-2005 03:52 PM

Re: memset doubt
 
srivatsan_b wrote on 31/08/05 :
> Can somebody explain whether an explicit typecast is mandatory while


It's not. Be sure that <string.h> is in scope and that you are using a
C compiler...

> calling memset function for a structure? like in the following code
> snapshot.....
> struct some_structure x;
> memset((some_structure*)&x,0,sizeof(some_structure ));


Plain wrong. You meant:

struct some_structure x;
memset((struct some_structure*)&x,0, sizeof(struct some_structure));

> Will memset(&x,0,sizeof(some_structure)); cause some issues?


No, as long as 'x' has the type 'some_structure'. This is why I prefer
to use the size of the the object itself.

memset (&x, 0, sizeof x);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.



Tim Rentsch 08-31-2005 05:04 PM

Re: memset doubt
 
Barry Schwarz <schwarzb@deloz.net> writes:

> Thirdly, pointer to void is "compatible" with any other unqualified
> object pointer type. This means that the compiler can implicitly
> convert one to the other whenever it needs to without requiring a
> cast.


Just a minor correction. The word "compatible" is
used in the standard document following a particular
definition (section 6.2.7 p1, and others).

In the sense of this definition, void pointers and
non-void pointers are not "compatible". They can be
used interchangeably in many contexts, but they are
not "compatible" in the sense that the standard
document uses the word.

Default User 08-31-2005 05:29 PM

Re: memset doubt
 
Peter Nilsson wrote:


> In C, any object pointer can be implicitly converted to a pointer
> to void [subject to cv-qualifiers.] So the answer to your question
> is no. C++ is different, but you should ask in a C++ group if you
> ere actually using a C++ compiler.


C++ is not different in this case. The difference is that void* can't
be converted to an object pointer without a cast.



Brian

Default User 08-31-2005 05:31 PM

Re: memset doubt
 
Keith Thompson wrote:


> <OT>
> Note that the rules are different for C++. If I recall correctly, a
> declaration of "struct some_structure" makes "some_structure" visible
> as a type name,


Yes.

> and there is no implicit conversion to void*.


No. There is no implicit conversion FROM void*. So malloc() needs a
cast, memset() doesn't.

> </OT>





Tim Rentsch 08-31-2005 06:36 PM

Re: memset doubt
 
Lawrence Kirby <lknews@netactive.co.uk> writes:

> On Wed, 31 Aug 2005 10:04:29 -0700, Tim Rentsch wrote:
>
> > Barry Schwarz <schwarzb@deloz.net> writes:
> >
> >> Thirdly, pointer to void is "compatible" with any other unqualified
> >> object pointer type. This means that the compiler can implicitly
> >> convert one to the other whenever it needs to without requiring a
> >> cast.

> >
> > Just a minor correction. The word "compatible" is
> > used in the standard document following a particular
> > definition (section 6.2.7 p1, and others).
> >
> > In the sense of this definition, void pointers and
> > non-void pointers are not "compatible". They can be
> > used interchangeably in many contexts, but they are
> > not "compatible" in the sense that the standard
> > document uses the word.

>
> Perhaps the term "assignment compatible" is appropriate here. While not
> strictly a standard term its meaning is clear.


I propose "implicitly convertible". It's descriptive,
and accurate.

Besides the confusion around "compatible", the term
"assignment compatible" suggests that the conversion
happens only for assignment (or function arguments);
whereas void pointers can also be used with non-void
pointers in other contexts, eg, equality comparisons,
or as one side of a ?: result set.


All times are GMT. The time now is 12:21 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.