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

.



Relevant Pages

  • Re: Garbage Collection in C
    ... since if the number of DLLs that the ... casting an object pointer to a pointer with a less strict alignment ... the result of converting a pointer ...
    (comp.lang.c)
  • Re: what is the this keyword
    ... What Patricia means is that when a compiler is reading the ... techniques can vary between compilers and Java virtual machines. ... along a pointer to the object as a sort of hidden parameter. ... Follow the object pointer, and look at a predetermined offset to ...
    (comp.lang.java.help)
  • Re: null function pointer?
    ... >>> converts an object pointer value to a function pointer. ... the object pointer expression in this ... >>is a pointer to an incomplete type, ...
    (comp.lang.c)
  • Re: Change a TextBox Font
    ... > I would suspect something is happening to corrupt the object pointer ... > you are passing then. ... I've run a test here using textboxes and ...
    (microsoft.public.vb.winapi)
  • Re: man malloc
    ... % foo.c:14: warning: ISO C forbids conversion of function pointer to object pointer type ...
    (freebsd-questions)