By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,793 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,793 IT Pros & Developers. It's quick & easy.

size of structure

P: n/a
I need to refresh my memory on this one.

If have a struct defined like this:
typedef struct
{
char guid[32] ;
char version ;
char srcId ;
char eventId ;
char * msg ;
} MyStruct ;

is the size given by :

msg ? sizeof(MyStruct) + strlen(msg) : sizeof(MyStruct)
Sep 6 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Anonymous <no******@here.comwrites:
>If have a struct defined like this:
>typedef struct
{
char guid[32] ;
char version ;
char srcId ;
char eventId ;
char * msg ;
} MyStruct ;
>is the size given by :
>msg ? sizeof(MyStruct) + strlen(msg) : sizeof(MyStruct)

The size of the structure is sizeof(MyStruct).

If the msg pointer points anywhere useful, the size or length of that
is not included in the structure's size.

For example, if msg pointed to the structure's guid,
would you count the guid's length twice? Probably not.

--
Chris.
Sep 6 '07 #2

P: n/a
On Thu, 06 Sep 2007 09:45:35 +0100, Anonymous wrote:
I need to refresh my memory on this one.

If have a struct defined like this:
typedef struct
{
char guid[32] ;
char version ;
char srcId ;
char eventId ;
char * msg ;
} MyStruct ;

is the size given by :

msg ? sizeof(MyStruct) + strlen(msg) : sizeof(MyStruct)
The size of the structure is sizeof(MyStruct).
Maybe you're thinking of the struct hack?

For example, you could write:
MyStruct *ptr = malloc(sizeof *ptr + 42);
ptr->msg = (char *)ptr + sizeof *ptr;

The original struct hack uses an array with one member as its last
member, "officially" it has undefined behavior, even if I don't
know of any implementation where it fails.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling
garbage characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 6 '07 #3

P: n/a
Anonymous <no******@here.comwrites:
I need to refresh my memory on this one.

If have a struct defined like this:
typedef struct
{
char guid[32] ;
char version ;
char srcId ;
char eventId ;
char * msg ;
} MyStruct ;

is the size given by :

msg ? sizeof(MyStruct) + strlen(msg) : sizeof(MyStruct)
As Chris said, the size of the struct is sizeof(MyStruct).

If msg points to a string, the size of the array containing the string
must be at least strlen(msg) + 1 -- but the string could easily be
contained in a much larger array.

Or msg might not point to a string at all. Without seeing the code
that sets it, we have no way of knowing whether msg points to a
string, to a single character, to an array that may not contain any
'\0' characters, to an array that may contain multiple '\0'
characters, or to nothing at all.

You're asking about "the size". The size of *what*? Without knowing
that, the answer is unknowable. If we do know that, the answer is
likely to be trivial -- but if it depends on the size of the object to
which msg points, there's no direct way to determine that from the
value of msg.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 6 '07 #4

P: n/a
On Thu, 06 Sep 2007 09:45:35 +0100, Anonymous <no******@here.com>
wrote:
>I need to refresh my memory on this one.

If have a struct defined like this:
typedef struct
{
char guid[32] ;
char version ;
char srcId ;
char eventId ;
char * msg ;
} MyStruct ;

is the size given by :

msg ? sizeof(MyStruct) + strlen(msg) : sizeof(MyStruct)
Your subject says size of structure. The size of the struct type is
given by sizeof(MyStruct).

If msg points to a string (not every char* does), then the size (not
length) of the string is given by strlen(msg)+1.

However, if msg points to a defined array or a dynamically allocated
block of memory, there is no standard method of determining the size
of the array/block from msg. Your program must keep track of that
itself.
Remove del for email
Sep 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.