Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Elegant way to clear a struct ?

Reply
Thread Tools

Elegant way to clear a struct ?

 
 
Nils Emil P. Larsen
Guest
Posts: n/a
 
      06-29-2003
Hello

I have a pointer to a struct or an array of structs and I know the
lenght of the struct or array of structs.

I need a elegant way to clear it with zeroes.

Is it legal just to do something like

Mystruct *mystruct; // Mystruct is a typedef

for (i=0; i < sizeof(Mystruct); i++)
*(mystruct+i) = 0;

or do I have to clear each individual members ?

Thank you
Nils Emil P. Larsen
 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      06-29-2003
Nils Emil P. Larsen wrote:

> I have a pointer to a struct or an array of structs and I know the
> lenght of the struct or array of structs.
>
> I need a elegant way to clear it with zeroes.
>
> Mystruct *mystruct; // Mystruct is a typedef


If the struct contains nothing but integer (and character) elements,
you can just do
memset(mystruct, 0, array_length * sizeof(Mystruct))
Otherwise, use
const Mystruct null_mystruct; /* will be initialized with nulls */
for (i = 0; i < array_length; i++)
mystruct[i] = null_mystruct;

BTW, note that ANSI C (or ISO C89) do not accept '//' comments. Only
C++ and C99 do.

--
Hallvard
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-29-2003
Nils Emil P. Larsen wrote:

> Hello
>
> I have a pointer to a struct or an array of structs and I know the
> lenght of the struct or array of structs.
>
> I need a elegant way to clear it with zeroes.
>
> Is it legal just to do something like
>
> Mystruct *mystruct; // Mystruct is a typedef
>
> for (i=0; i < sizeof(Mystruct); i++)
> *(mystruct+i) = 0;


That looks rather confused to me. Very confused, in fact.

Given that mystruct points to an array of N Mystruct objects, you can do
this:

Mystruct blank = {0};

for(i = 0; i < N; i++)
{
mystruct[i] = blank;
}

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      06-29-2003

I wrote:

> const Mystruct null_mystruct; /* will be initialized with nulls */


Sorry, that should be 'static const null_mystruct', unless
null_mystruct has file scope.

--
Hallvard
 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      06-29-2003
Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote:
> Nils Emil P. Larsen wrote:
>
>> I have a pointer to a struct or an array of structs and I know the
>> lenght of the struct or array of structs.
>>
>> I need a elegant way to clear it with zeroes.
>>
>> Mystruct *mystruct; // Mystruct is a typedef

>
> If the struct contains nothing but integer (and character) elements,
> you can just do
> memset(mystruct, 0, array_length * sizeof(Mystruct))
> Otherwise, use
> const Mystruct null_mystruct; /* will be initialized with nulls */


What basis do you have for that comment? Did you perhaps mean either:

const Mystruct null_mystruct = { 0 };

or

static const Mystruct null_mystruct;

?
> for (i = 0; i < array_length; i++)
> mystruct[i] = null_mystruct;


- Kevin.

 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      06-29-2003
Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote:
>
> I wrote:
>
>> const Mystruct null_mystruct; /* will be initialized with nulls */

>
> Sorry, that should be 'static const null_mystruct', unless
> null_mystruct has file scope.


What use is an int variable called "null_mystruct" in this context?

- Kevin.

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-29-2003
In 'comp.lang.c', Nils Emil P. Larsen <(E-Mail Removed)> wrote:

> I have a pointer to a struct or an array of structs and I know the
> lenght of the struct or array of structs.


'length'. In fact, you mean 'size'.

> I need a elegant way to clear it with zeroes.


Mystruct *mystruct = malloc (sizeof *mystruct);

if (mystruct)
{
static const Mystruct z = {0};

*mystruct = z;
}

> Is it legal just to do something like
>
> Mystruct *mystruct; // Mystruct is a typedef
>
> for (i=0; i < sizeof(Mystruct); i++)
> *(mystruct+i) = 0;


or
mystruct[i] = 0;

Assuming 'mystruct' was initialized with an the address of an (allocated)
bloc of data, none of the two forms is legal.

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-29-2003
Nils Emil P. Larsen <(E-Mail Removed)> wrote (29 Jun 2003) in
news:(E-Mail Removed) / comp.lang.c:

> Hello
>
> I have a pointer to a struct or an array of structs and I know the
> lenght of the struct or array of structs.
>
> I need a elegant way to clear it with zeroes.
>
> Is it legal just to do something like
>
> Mystruct *mystruct; // Mystruct is a typedef
>
> for (i=0; i < sizeof(Mystruct); i++)
> *(mystruct+i) = 0;


This won't do it, since *(mystruct+i) is mystruct[i].
>
> or do I have to clear each individual members ?


Depending on the types of members a Mystruct has, memset() might do it
for you.


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      06-30-2003
"Nils Emil P. Larsen" <(E-Mail Removed)> wrote:
> I have a pointer to a struct or an array of structs and
> I know the lenght of the struct or array of structs.
>
> I need a elegant way to clear it with zeroes.
>
> Is it legal just to do something like
>
> Mystruct *mystruct; // Mystruct is a typedef
>
> for (i=0; i < sizeof(Mystruct); i++)
> *(mystruct+i) = 0;


When you add i to mystruct you are constructing another
pointer to Mystruct, which points to the i-th successive
Mystruct after the one that mystruct actually points to.

See how confusing it is to have variables and types with
names differentiated only by case? That's not a good idea.

So, what does sizeof(Mystruct) have to do with successive
Mystructs after the one pointed to by mystruct? If you
wanted to address each *byte* of the structure, you have
to cast the pointer to (unsigned char *). You would be
in essence just recreating what is done for you by memset.
But that is not a portable way to zero out most types.

This looks to me like a place where C99's compound literals
would be useful. Example code follows.

#include <stdlib.h>

typedef struct {int a; char *p;} Mystruct;

int main(void)
{
Mystruct *foo = malloc(sizeof *foo);
if(foo)
{
*foo = (Mystruct){0};
}

Mystruct bar[42];
for(size_t i = 0; i < sizeof bar / sizeof *bar; i++)
{
bar[i] = (Mystruct){0};
}

return 0;
}

This code was tested on GCC 3.2 with command line
gcc -std=c99 -pedantic -Wall -W -O2 zerostruct.c -o zerostruct

--
Simon.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-30-2003
Ravi Uday wrote:

> Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote in
> message news:<(E-Mail Removed)>...
>> I wrote:
>>
>> > const Mystruct null_mystruct; /* will be initialized with nulls */

>>
>> Sorry, that should be 'static const null_mystruct', unless
>> null_mystruct has file scope.

>
> What if i have a struct like this
> typedef struct str
> {
> int i;
> char c[20]
> char *temp;
> }my_str;
>
> int main ()
> {
> my_str st;
> static const my_str zero;
>
> st.c = 'a';
> st.temp = malloc ( 10 * sizeof char );


Either drop sizeof char altogether, or replace it with sizeof *st.temp, or
at least put () around char.

> /* Now if i say as follows */
> st = zero;
>
> /*will it free the alloc mem for temp


No.

> and then assign zero or how does it
> work*/


It will leak the memory you assigned. Solution: free the memory first.

<snip>

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Most elegant way to clear all the text fields on asp.net page? Duk Lee ASP .Net 2 07-23-2007 08:25 PM
Re: Is there an elegant way to set an unsigned vector to 1 Jan De Ceuster VHDL 5 01-13-2005 07:26 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Elegant way of returning FieldNames? Jay Balapa ASP .Net 1 08-07-2003 10:25 PM



Advertisments