469,602 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

size of structure

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
4 2074
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
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
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
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.

Similar topics

22 posts views Thread by Wynand Winterbach | last post: by
18 posts views Thread by Anand Buddhdev | last post: by
7 posts views Thread by ANaiveProgrammer | last post: by
4 posts views Thread by Shayne H | last post: by
6 posts views Thread by Laurent | last post: by
8 posts views Thread by redefined.horizons | last post: by
15 posts views Thread by kris | last post: by
12 posts views Thread by Kislay | last post: by
1 post views Thread by =?Utf-8?B?VGhvbWFzUg==?= | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.