Results 1 to 6 of 6
Hi,
I following structure
typedef struct
{
UINT32 abc;
int (*f)(int a, intb);
char *ptr;
} my_struct;
main()
{
my_struct myParams;
sizeof(myParams);
}
Assume int is 4 bytes and char ...
- 07-02-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
sizeof struct with function pointers as a member
Hi,
I following structure
typedef struct
{
UINT32 abc;
int (*f)(int a, intb);
char *ptr;
} my_struct;
main()
{
my_struct myParams;
sizeof(myParams);
}
Assume int is 4 bytes and char is 1 byte. I know the size of structure without pointers inside. But I am not sure with pointers and function pointers. Is it 4+4+4(12) or something else??
Thanks in advance.
Johnny
- 07-02-2010 #2
Well the first thing I would do is find the sizeof(void*) which will be the size of your pointers.
Make mine Arch Linux
- 07-02-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 6
sizeof(void*) is 4. Same way sizeof(char*) is also 4.
The total size is 12 bytes = 4 for int + 4 for function ptrs +4 for char*.
No padding, as it is a character pointer that takes four bytes and not character that takes one byte and needs padding.
If it were int and char inside a structure, due to padding, it would be 8.
Thanks gerard.
- 07-02-2010 #4
Remember that all pointers are the same size, no matter what they point to. This is because a pointer is simply a number that refers to a location in memory, no matter what the object at that memory location is. On a 32-bit machine, you expect a pointer to be 32 bits, because it uses 32-bit integers. Similarly, on a 64-bit machine, pointers would be expected to be 64 bits.
So I agree: 12 bytes is the expected size for this struct.DISTRO=Arch
Registered Linux User #388732
- 07-04-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
For a 32-bit system, this should be 12 bytes. For a 64-bit system, it would be 20 bytes. Note that depending upon how you compile it, on a 64-bit system it might pad the structure so that the function pointer is aligned on a 64-bit boundary, resulting in a pad between abc and f of 4 more bytes and a structure size of 24 bytes. Caveate programmer! Test it out to see.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-04-2010 #6
The real question is, why do you think it is important to know this? Other than for the sake of academic interest, there is usually no reason to know. Let the compiler do its work, and trust that as long as IT knows, everything will work out.
If the structure is going to be transported and used cross-platform, then there is value in knowing how the structure is organized. Then, it has as much to do with the arrangement of the parts as it does with the size. Since your structure contains a pointer to a function, it is inherently not tranportable to any platform.
There is the C language #pragma that may allow you to control how the structure is arranged in memory, depending on the compiler.
--- rod.Stuff happens. Then stays happened.


Reply With Quote