473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2214
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.hmissi ng
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(str uct abc)". This is the
same as "sizeof(*a) ", assuming you allocated the space for "a" with:

a = malloc(sizeof(s truct 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(str uct abc)". This is the
same as "sizeof(*a) ", assuming you allocated the space for "a" with:

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

"kris" <ra************ **@gmail.comwro te in message
news:11******** *************@i 38g2000prf.goog legroups.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(str uct abc)". This is the
same as "sizeof(*a) ", assuming you allocated the space for "a" with:

a = malloc(sizeof(s truct 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.re mcomp.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

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

Similar topics

5
5699
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 code: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
0
1299
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 FileInfo(ImageFilePath.PostedFile.FileName).Length is also quit easy. However, obtaining the Height and Width in Pixels is difficult. I need this information to...
4
1309
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 strictly ASP method. Any help will be appreciated, Thanks -- / Sean the Mc /
4
23164
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 may have occurred because all pooled connections were in use and max pool size was reached. Below is my code. Any help will be appreciated.
6
4995
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 class MyClass {
4
2182
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 { public byte MsgType; public uint Tag; public fixed byte ID; public fixed byte Val1;
11
4045
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 binary format. But what "server()" and "issuer()" return are strings, with the pairs separated by "/". The documentation at...
7
2751
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 gives is: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled...
2
12606
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 in use and max pool size was reached. Target Site:- MySql.Data.MySqlClient.Driver GetConnection() Error Source:- MySql.Data
0
7527
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.