memset a member array of a packed structure

P

parag.kanade

I have a packet structure which has a field of Integer arrays, that is

packed struct
{
int a;
char b;
int count[100];
}foo;

I need to initialize all the entries of count to a particular value,
how do I do it?

struct foo *temp= (struct foo*)malloc(sizeof(struct foo));
memset(temp->count,0,sizeof(temp->count))

Gives compiler error
Error: C2906E: <argument 1 to 'memset'>: implicit cast of pointer loses
'__packed' qualifier
 
K

Keith Thompson

I have a packet structure which has a field of Integer arrays, that is

packed struct
{
int a;
char b;
int count[100];
}foo;

I need to initialize all the entries of count to a particular value,
how do I do it?

struct foo *temp= (struct foo*)malloc(sizeof(struct foo));
memset(temp->count,0,sizeof(temp->count))

Gives compiler error
Error: C2906E: <argument 1 to 'memset'>: implicit cast of pointer loses
'__packed' qualifier

Don't cast the result of malloc(); it's unnecessary and can mask
errors such as failing to include <stdlib.h> or using a C++ compiler
to compile C code. The recommended form for your malloc() call is:

struct foo *temp = malloc(sizeof *tmp);

Note that you only have to specify the type in one place, which could
avoid errors if you ever change the type.

But your real problem is the "packed" (or "__packed") qualifier.
There is no such qualifier in standard C; it appears to be an
implementation-specific extension. The fact that the error message
refers to "__packed" rather than "packed" probably indicates that
you've included a header that has something like:

#define packed __packed

(The identifier "__packed" can legally be used as an
implementation-specific keyword; "packed" cannot, unless you use an
implementation-specific header.)

Since the message complains about an "implicit cast" (more on that in
a moment), it's likely that an explicit cast would avoid the error
message:

memset((void*)temp->count,0,sizeof temp->count);

*But* keep in mind that this cast is simply a way of telling the
compiler, "Shut up, I know what I'm doing". Before you try this make
sure you really do know what you're doing. We have no way of knowing
exactly what "packed" means, so we can't guess whether the cast will
cause problems. Consult your compiler's documentation.

Using memset() to zero an array of int is ok, but subtly so. It's
guaranteed that all-bits-zero is a valid representation for value 0 in
any integer type. The standard doesn't actually say so, but the
committee has made a ruling in response to defect report; I'm
reasonably sure this is ok in all existing implementations, so you can
safely count on it.

Finally, there is no such thing as an "implicit cast". The error
message should refer to an "implicit conversion". A cast is an
operator that performs a conversion; it can't be implicit.
 
R

Robert Gamble

Keith Thompson wrote:

[snip]
Using memset() to zero an array of int is ok, but subtly so. It's
guaranteed that all-bits-zero is a valid representation for value 0 in
any integer type. The standard doesn't actually say so, but the
committee has made a ruling in response to defect report;

TC2 item #9 says:
Page 39, 6.2.6.2
Append to paragraph 5:
"For any integer type, the object representation where all the bits are
zero shall be a
representation of the value zero in that type."

So this has been standardized behavior for over a year now.

Robert Gamble
 
R

Robert Gamble

Keith Thompson wrote:

[snip]
Using memset() to zero an array of int is ok, but subtly so. It's
guaranteed that all-bits-zero is a valid representation for value 0 in
any integer type. The standard doesn't actually say so, but the
committee has made a ruling in response to defect report;

TC2 item #9 says:
Page 39, 6.2.6.2
Append to paragraph 5:
"For any integer type, the object representation where all the bits are
zero shall be a
representation of the value zero in that type."

So this has been standardized behavior for over a year now.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top