473,320 Members | 2,027 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,320 software developers and data experts.

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 2311
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
18
by: Anand Buddhdev | last post by:
Hi everyone, I'm a C newbie, so please be gentle. I have a program that defines the following things: typedef union { unsigned int I; unsigned char b; } dword;
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
4
by: Shayne H | last post by:
How can I create a structure that gets passed to an API call that will automatically set its size field? I tried the following: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
1
by: Abdul Samad | last post by:
Assalam mu Alikum, i understand the size of structure with no data types is zero. but why the size of structure variable is 1 Byte when the size of that structure is zeor? for example ...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
1
by: =?Utf-8?B?VGhvbWFzUg==?= | last post by:
Hi together, I have following little problem with a structure which I need for unmanaged code: Public Structure Info Public Header As HeaderInfo ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.