473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

memset a member array of a packed structure

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

Jan 18 '06 #1
3 12093
pa**********@gmail.com writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 18 '06 #2

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

Jan 18 '06 #3

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

Jan 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
5
by: prcpani | last post by:
I have a program that will read as follows #define MS(x) memset(x.arr,'\0',sizeof(x.arr­)) void memset_var() { int cnt =0;
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
9
by: divya_rathore_ | last post by:
Consider the dynamic allocation of a 2D array: int **temp; temp = new int*; for (y=0; y<height; y++) temp = new int; I have 2 Questions: Does new initializes memory to 0?
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
9
by: Sikandar | last post by:
Hi, I am beginner in C. Pls let me know what is packed array in C. Where is it used? Thanks, Sikandar
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.