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

obtaining the size of a structure


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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

Thanks
Krish

Aug 1 '07 #1
15 2203
kris wrote:
Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows
Standard headers such as <stdio.hmissing
struct abc
{
int j;
char k;
int i;
}*a;

main()
I'd prefer "int main(void)" and I think it may be required in C99
{
a->j=10;
using the value of a uninitialized pointer. Bad dog! No biscuit!
>
printf("size of structure is %d",sizeof(a));
That tells you the size of the pointer. "sizeof(*a)" would tell you the
size of the structure.
}
No "return" statement is poor style.
Aug 1 '07 #2
kris wrote:
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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
(That program won't work, but I assume it's just a copy&paste error?)

First, sizeof(a) will always return the number of bytes occupied by
pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.

Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));

The size of the struct is always the same. I don't understand what you
mean with "I need to get the size of structure such that it resembles
the chuck of memory that is actually being used".
Aug 1 '07 #3
Nikos Chantziaras wrote:
kris wrote:
>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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}
Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));
Nope - sizeof(*a) will return the sizeof the structure, even for an
uninitialized or NULL pointer...
Aug 1 '07 #4

"kris" <ra**************@gmail.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
>
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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
This is the size of a pointer to the structure.
'a' is of type 'struct abc *'
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
try sizeof(*a)

Why do you need this?

Note that the actual size of this
structure is implementation dependent; the compiler may
add padding between j and k, and between k and i, and
even after i. Different compilers may add different
amounts of padding.
--
Fred L. Kleinschmidt
Aug 1 '07 #5
kris wrote:
>
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;
a is a pointer of type struct abc*.
main()
As per the C Standard main must return an int. So declare it to do so.

int main(void)
{
a->j=10;
a is a pointer. You need to initialise it to point to a valid structure abc
object. Do;

struct abc foo;
a = &foo;
a->j = 10;
printf("size of structure is %d",sizeof(a));
The d format specifier is for an int argument. Use either lu or zu for a
size-t argument, which is the return type of the sizeof operator.

To calculate the size of the structure do:
sizeof *a;

Note the lack of parenthesis.
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).
No you don't. You get the size of the pointer a.
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
Do as above.

Aug 1 '07 #6
On Wed, 01 Aug 2007 07:09:35 -0700, kris wrote:
>
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;

main()
In C99, you need to specify the return type (int).
{
a->j=10;

printf("size of structure is %d",sizeof(a));
sizeof returns a size_t, which cannot be an int (as expected by
%d). See www.c-faq.com, question 12.9b.

In C89, you need to explicitly return a value (0 to indicate
success).
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).
No, you get the size of a pointer to it.
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
sizeof *a, i.e. sizeof(struct abc) is required to include the
padding.
(a is a pointer to struct abc, so *a is a struct abc.)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 1 '07 #7
On Wed, 01 Aug 2007 17:35:38 +0300, Nikos Chantziaras wrote:
kris wrote:
>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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

(That program won't work, but I assume it's just a copy&paste error?)

First, sizeof(a) will always return the number of bytes occupied by
pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.

Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:

a = malloc(sizeof(struct abc));

The size of the struct is always the same. I don't understand what you
mean with "I need to get the size of structure such that it resembles
the chuck of memory that is actually being used".
I think he means the size of the struct including the padding,
rather than just the sum of the sizes of its members.
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 1 '07 #8
kris wrote:
>
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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
Don't post multiple times. Usenet, to which Google Groups interfaces, can
sometimes be slow. Wait for a few hours before reposting. See you earlier
post for replies.

Aug 1 '07 #9
JT
Army1987 wrote:
Also, a compiler
isn't required to let you specify the alignment.
On Aug 1, 3:25 pm, jacob navia <ja...@jacob.remcomp.frwrote:
Ahh that is news to me. How would you do it then?
(just curious how you do it without a compiler)
I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.

Aug 1 '07 #10
JT wrote:
Army1987 wrote:
>Also, a compiler
isn't required to let you specify the alignment.

On Aug 1, 3:25 pm, jacob navia <ja...@jacob.remcomp.frwrote:
>Ahh that is news to me. How would you do it then?
(just curious how you do it without a compiler)

I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.
Ahh

OK, true.

I just did not understand the sentence
Aug 1 '07 #11
kris wrote:
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;

main()
{
a->j=10;
No space has been allocated for a to point to. Dereferencing a pointer
is meaningless, and a nice implementation would generate a runtime error.
>
printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).
You should never get to the printf(), the program already having blown up.
>
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
You allocated no memory to actually be used. There is no "chunk of
memory that is actually being used." Assuming you had allocated such
memory, your question would be meaningless. The sizeof an object is
(not merely "resembles") the size of the chunk of memory used for it.

>
Thanks
Krish
Aug 1 '07 #12
On Wed, 01 Aug 2007 17:49:59 +0200, jacob navia wrote:
JT wrote:
>Army1987 wrote:
>>Also, a compiler
isn't required to let you specify the alignment.

On Aug 1, 3:25 pm, jacob navia <ja...@jacob.remcomp.frwrote:
>>Ahh that is news to me. How would you do it then?
(just curious how you do it without a compiler)

I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.

Ahh

OK, true.

I just did not understand the sentence
How on Earth did you interpret it?
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 1 '07 #13
Army1987 wrote:
On Wed, 01 Aug 2007 17:49:59 +0200, jacob navia wrote:
>JT wrote:
>>Army1987 wrote:
Also, a compiler
isn't required to let you specify the alignment.
On Aug 1, 3:25 pm, jacob navia <ja...@jacob.remcomp.frwrote:
Ahh that is news to me. How would you do it then?
(just curious how you do it without a compiler)
I'm sure he meant
"a compiler doesn't have to let you specify the alignment", i.e.
"a compiler isn't required to let you specify the alignment".

And he would be right.
Ahh

OK, true.

I just did not understand the sentence
How on Earth did you interpret it?
I am ashamed to confess that I understood that no compiler
is required !!!

PARSE ERROR!

:-)
Aug 1 '07 #14
On Wed, 01 Aug 2007 15:35:21 +0100, Mark Bluemel
<ma**********@pobox.comwrote in comp.lang.c:
kris wrote:
Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows

Standard headers such as <stdio.hmissing
struct abc
{
int j;
char k;
int i;
}*a;

main()

I'd prefer "int main(void)" and I think it may be required in C99
The void in the parentheses is not, if you don't need the definition
to also serve as a prototype. Implicit int is gone, so either:

int main()

....or:

int main(void)

....are acceptable.
{
a->j=10;

using the value of a uninitialized pointer. Bad dog! No biscuit!
No, a is most certainly initialized. It's a null pointer.
printf("size of structure is %d",sizeof(a));

That tells you the size of the pointer. "sizeof(*a)" would tell you the
size of the structure.
}

No "return" statement is poor style.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Aug 1 '07 #15
kris wrote:
>
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;

main()
{
a->j=10;

printf("size of structure is %d",sizeof(a));
}

In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).

But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.

/* BEGIN new.c */

#include <stdio.h>

struct abc {
int j;
char k;
int i;
} *a;

int main(void)
{
printf("size of structure is %u.\n",
(unsigned)sizeof (struct abc));
printf("size of structure is %d.\n",
(int)((char *)(a + 1) - (char *)a));
return 0;
}

/* END new.c */
--
pete
Aug 1 '07 #16

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

Similar topics

5
by: Martin | last post by:
Dear Group Sorry for posting this here. I'm desperate for a solution to this problem and thought some of you might have come across it with .NET and SQL Server. Let's assume I've the following...
0
by: Lennart Hoglund | last post by:
Uploading an Image to a Server using the method described in the Knowledge Base article Q315832, works fine and smoothly. Obtaining the Image Size using; ImageSize.Text = New...
4
by: What-a-Tool | last post by:
Is there an ASP method for obtaining the size of the clients display area? Think I can think of a method(havn't tried it yet) combining JavaScripting and ASP, but I was wondering if there was a...
4
by: Guoqi Zheng | last post by:
Dear sir, I keep getting the following errors on one of my sites after clicking for many times. Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This...
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...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
11
by: John Nagle | last post by:
The Python SSL object offers two methods from obtaining the info from an SSL certificate, "server()" and "issuer()". The actual values in the certificate are a series of name/value pairs in ASN.1...
7
by: =?Utf-8?B?Sm9obiBTdGFnZ3M=?= | last post by:
Hello, Please read this all before giving an answer :) I'm doing some troubleshooting on a web application that my company wrote. It's written in asp.net 1.1. The error that the Event viewer...
2
by: anumsajeel | last post by:
Hi, Error Message:- error connecting: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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
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
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.