Solved

Without using sizeof how do i calculte structure size

Posted on 2008-06-11
9
615 Views
Last Modified: 2012-05-05
Hi,
I have one structure having two int and a char. how do i calculate size of the structure?
thanks,
santanu
0
Comment
Question by:arijit_rebaca
  • 4
  • 2
  • 2
9 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21758843
>> I have one structure having two int and a char.
The size of these is platform dependent (their size isn't defined in the standard)

>> how do i calculate size of the structure?
At best you can approximate it, by adding together the (assumed) size of the types it aggregates but structs can also contain padding and unless you also know how this has been implemented you'll not necessarily get the right result. In other words the sum of the parts might be less than the whole.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21758851
sizeof is the easiest way :

        typedef struct Test {
            int i;
            char c;
        } Test;

        sizeof(Test);

Other methods will not be reliable due to padding that might be added to the struct.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21758858
Any reason you don't want to use sizeof ?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

Expert Comment

by:evilrix
ID: 21758859
0
 
LVL 10

Accepted Solution

by:
peetm earned 125 total points
ID: 21759291
#include <stdio.h>

int main(void)
{
    struct thing
    {
        int     a;
        char    b;
        float   c;
    };
   
    struct thing t;

    struct thing * ptr = &t;

    printf("%u\n", (char *)(ptr + 1) - (char *)ptr);

    return 0;
}
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21759435
>> (char *)(ptr + 1)

Hehe ... that's cheating lol ... But true : sizeof is not used explicitly.
0
 
LVL 10

Expert Comment

by:peetm
ID: 21759455
>>Hehe ... that's cheating lol ... But true : sizeof is not used explicitly.

:-)

And, one would not of course want to have a struct larger than ptrdiff_t, and, I'm not so sure about using 'u' either - still I reckon it's the kind of thing that someone's got in mind here.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21759491
>> still I reckon it's the kind of thing that someone's got in mind here.

There's a good chance of that :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Binary Bomb: Phase 4 func4 3 329
port mapped I/o query 10 184
Line meaning 9 85
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 29
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question