C question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

C question

Doug Brewer
Hello,

I have the following lines in C:

struct foo *bar[10];

If I want to clear out that structure, should I use

memset(bar, 0, sizeof(bar));
or
memset(bar, 0, sizeof(*bar));

Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: C question

James Hartley
On Wed, Nov 9, 2011 at 6:39 PM, Doug Brewer <[hidden email]> wrote:

> If I want to clear out that structure, should I use
>
> memset(bar, 0, sizeof(bar));
> or
> memset(bar, 0, sizeof(*bar));
>

The purpose of this list is not to focus on remedial programming support.
You can help answer your own question by studying & playing with the
following along with studying the memset(3) manpage:

#include <stdio.h>

struct foo {
    int i;
    float f;
 };

int main()
{
    struct foo *bar[10];
    printf("sizeof(struct foo) = %d\n", sizeof(struct foo));
    printf("sizeof(struct foo*) = %d\n", sizeof(struct foo*));

    return 0;
}

The art of programming is learned by writing lots of little programs which
answer questions like these.

Reply | Threaded
Open this post in threaded view
|

Re: C question

Ted Unangst-6
In reply to this post by Doug Brewer
On Thu, Nov 10, 2011, Doug Brewer wrote:
> I have the following lines in C:
>
> struct foo *bar[10];
>
> If I want to clear out that structure, should I use
>
> memset(bar, 0, sizeof(bar));
> or
> memset(bar, 0, sizeof(*bar));

Your question is quite ambiguous as to "that structure".  There
are (presumably) 10 structures, somewhere.  And here is an array of
pointers.

Neither of your lines will clear out whatever "that structure" can
reasonably be interpreted to mean.

Reply | Threaded
Open this post in threaded view
|

Re: C question

Gerhard Hoffmann
In reply to this post by Doug Brewer
Hi Doug,

you should consult a C-book first, you will definitely find it.

Its the first one. Look:

struct foo *bar[10];

So 'bar' is an array of 10 pointers to 'struct foo'.
sizeof(bar) will give you 40 on an 32-bit machine,
and 80 on an 64-bit machine (assuming that a pointer
on an 32-machine is 4 bytes, and on an 64-bit machine
its 8 bytes).

memset(bar, 0, sizeof(*bar));

If you use the name of an array
in an expression, the compiler will automatically
convert it into a pointer (to the first element).
So 'bar' is now a pointer, and *bar will give you
the size of a pointer to 'struct foo' (which
is the first element and which will be 4 or 8 bytes).

But keep in mind that 'sizeof' is a compile-time operator.
If you have a pointer like

what-ever-type *foo;

then

sizeof(*foo);

will always give you the size of 'what-ever-type'.

That means that

sizeof(**bar);

will give you the size of 'struct foo'.

A last comment: better not use 'memset' here.
The reason is that a NULL-pointer isn't something
which has a 0-bytes-pattern in memory. Its only
the case that most C-implementations do it that way.
But a C-implementation is free to use whatever it likes
to represent a NULL-pointer internally.

So while 'memset' will work on some platforms
(and compilers), on other platforms you will have
unintialized pointers, and not NULL-pointers.

Hope this helps.


On Thu, Nov 10, 2011 at 10:39:29AM +0800, Doug Brewer wrote:

> Hello,
>
> I have the following lines in C:
>
> struct foo *bar[10];
>
> If I want to clear out that structure, should I use
>
> memset(bar, 0, sizeof(bar));
> or
> memset(bar, 0, sizeof(*bar));
>
> Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: C question

Marc Espie-2
In reply to this post by Ted Unangst-6
On Wed, Nov 09, 2011 at 11:09:25PM -0500, Ted Unangst wrote:

> On Thu, Nov 10, 2011, Doug Brewer wrote:
> > I have the following lines in C:
> >
> > struct foo *bar[10];
> >
> > If I want to clear out that structure, should I use
> >
> > memset(bar, 0, sizeof(bar));
> > or
> > memset(bar, 0, sizeof(*bar));
>
> Your question is quite ambiguous as to "that structure".  There
> are (presumably) 10 structures, somewhere.  And here is an array of
> pointers.
>
> Neither of your lines will clear out whatever "that structure" can
> reasonably be interpreted to mean.
And note that even memset(bar, 0, sizeof(bar));
will not do anything portable, as you're just zeroing bytes that happen to
be pointers. The result may or may not be valid null pointers...