473,390 Members | 1,343 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,390 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 2314
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.