Re: memset doubt
- From: "Peter Nilsson" <airia@xxxxxxxxxxx>
- Date: 30 Aug 2005 22:59:48 -0700
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
.
- References:
- memset doubt
- From: srivatsan_b
- memset doubt
- Prev by Date: Re: confused abt file operations
- Next by Date: Re: memset doubt
- Previous by thread: memset doubt
- Next by thread: Re: memset doubt
- Index(es):
Relevant Pages
|