NO

Author Topic: Writing your own sizeof()?  (Read 4824 times)

andre104

  • Guest
Writing your own sizeof()?
« on: July 04, 2009, 06:56:50 PM »
I'm curious how hard it is to implement something like sizeof()?

For example, here's a struct:
Code: [Select]
typedef struct {
int aa;
float bb;
char cc;
} foo;

sizeof(aa) + sizeof(bb) + sizeof(cc) = 4 + 4 + 1 = 9
sizeof(foo) would yield 12, because the compiler pads the struct

So my question is, how to write a variant of sizeof() that calculates the exact size (non padded) of structures?

JohnF

  • Guest
Re: Writing your own sizeof()?
« Reply #1 on: July 05, 2009, 09:23:57 AM »
The best is to look at what others have said.

Here

John

Seltsamuel

  • Guest
Re: Writing your own sizeof()?
« Reply #2 on: July 05, 2009, 03:18:08 PM »
Hi,

the simplest is to use:

#pragma pack(1)

typedef struct {
   int aa;
   float bb;
   char cc;
} foo;

.. your other structs..

#pragma pack()

This prevents that the compiler pads your structs. otherwise if he Pads it you must know the actual size including padding (if you want to copy the data and padding can occure on any member of the struct!) so sizeof is absolutely correct here.

Greetings

Seltsamuel
« Last Edit: July 06, 2009, 01:01:25 AM by Seltsamuel »

JohnF

  • Guest
Re: Writing your own sizeof()?
« Reply #3 on: July 07, 2009, 12:02:59 PM »
I'm curious how hard it is to implement something like sizeof()?

For example, here's a struct:
Code: [Select]
typedef struct {
int aa;
float bb;
char cc;
} foo;

sizeof(aa) + sizeof(bb) + sizeof(cc) = 4 + 4 + 1 = 9
sizeof(foo) would yield 12, because the compiler pads the struct

So my question is, how to write a variant of sizeof() that calculates the exact size (non padded) of structures?

You would have to use pack(1) as Seltsamuel said.

Code: [Select]
#include <stdio.h>

#pragma pack(push, 1)

typedef struct foo{
   int aa;
   float bb;
   char cc;
};

#pragma pack(pop)

int main(void)
{
struct foo st;
struct foo * pst, * pst1;
pst  = &st;
pst1 = &st + 1;
printf("Size Of structure is %d bytes\n", (int)pst1-(int)pst);
return 0;
}

John

Seltsamuel

  • Guest
Re: Writing your own sizeof()?
« Reply #4 on: July 07, 2009, 12:17:16 PM »
Hi,

hm yes push and pop are cleaner when you use different packing sizes and dont know what was set before.
In my sources i always set #pragma pack(1) to tell the compiler to align bytewise as i usually know how i want my structs to look and fill in paddings by myself. #pragma pack() tells the compiler to switch back to his defaults. the additional push John added tells the Compiler to remember the actual setting und the pop gets it back its like a stack.
Thanks JohnF for clarification.


Greetings

Seltsamuel

nicolas.sitbon

  • Guest
Re: Writing your own sizeof()?
« Reply #5 on: July 07, 2009, 12:53:27 PM »
Code: [Select]
#include <stdio.h>

typedef struct __attribute__ ((__packed__)) foo
{
   int aa;
   float bb;
   char cc;
}
foo;

int main(void)
{
   struct foo st;
   struct foo * pst  = &st;
   struct foo * pst1 = pst + 1;

   printf("Size of structure is %td bytes\n", pst1 - pst);

   return 0;
}